From 87239b0e2b333a2a9d28a99385a1dbbc32d99521 Mon Sep 17 00:00:00 2001 From: E-D-W-I-N Date: Mon, 17 May 2021 00:51:30 +0700 Subject: [PATCH] Code review [#25] P15, P16 - enum refactoring --- .../presentation/map/MapFragment.kt | 38 +++++++++--------- .../presentation/map/MapViewModel.kt | 2 + .../com/edwin/data/entity/util/Converter.kt | 40 +++++++++++-------- .../data/entity/util/WeatherParameters.kt | 13 ++++++ 4 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 data/src/main/java/com/edwin/data/entity/util/WeatherParameters.kt diff --git a/app/src/main/java/com/edwin/weatherapp/presentation/map/MapFragment.kt b/app/src/main/java/com/edwin/weatherapp/presentation/map/MapFragment.kt index 7a7dfc2..ebb8062 100644 --- a/app/src/main/java/com/edwin/weatherapp/presentation/map/MapFragment.kt +++ b/app/src/main/java/com/edwin/weatherapp/presentation/map/MapFragment.kt @@ -34,8 +34,7 @@ class MapFragment : Fragment(R.layout.map_fragment), OnMapReadyCallback { companion object { private const val LOCATION_PERMISSION = Manifest.permission.ACCESS_COARSE_LOCATION - private const val ON_START_MAP_ZOOM = 4F - private const val ON_CLICK_MAP_ZOOM = 8F + private const val MAP_ZOOM = 8F } private val requestPermissionLauncher = registerForActivityResult( @@ -63,7 +62,7 @@ class MapFragment : Fragment(R.layout.map_fragment), OnMapReadyCallback { is MapViewModel.MapUiState.CurrentLocationLoaded -> { binding.progressBar.visibility = View.GONE val latLng = LatLng(state.location.latitude, state.location.longitude) - moveCameraToCurrentLocation(latLng) + moveCameraToLocation(latLng) } is MapViewModel.MapUiState.AddressLoaded -> { binding.progressBar.visibility = View.GONE @@ -92,7 +91,7 @@ class MapFragment : Fragment(R.layout.map_fragment), OnMapReadyCallback { R.string.action_dismiss, R.string.action_retry, { binding.banner.dismiss() }, - { viewModel.getFusedLocation() } + { binding.banner.dismiss().also { viewModel.getFusedLocation() } } ) } } @@ -104,33 +103,32 @@ class MapFragment : Fragment(R.layout.map_fragment), OnMapReadyCallback { override fun onMapReady(googleMap: GoogleMap) { map = googleMap setupObservers() - checkPermissions() + if (viewModel.isLocationNotChecked) { + checkPermissions() + viewModel.isLocationNotChecked = false + } map.setOnMapClickListener { latLng -> - map.clear() - map.addMarker { - position(latLng) - } - val cameraPosition = if (googleMap.cameraPosition.zoom > ON_CLICK_MAP_ZOOM) { - CameraUpdateFactory.newLatLng(latLng) - } else { - CameraUpdateFactory.newLatLngZoom(latLng, ON_CLICK_MAP_ZOOM) - } - map.animateCamera(cameraPosition) + moveCameraToLocation(latLng) viewModel.getAddress(latLng.latitude, latLng.longitude) } } - private fun moveCameraToCurrentLocation(latLng: LatLng) { - val cameraPosition = CameraUpdateFactory.newLatLngZoom(latLng, ON_START_MAP_ZOOM) - map.animateCamera(cameraPosition) + private fun moveCameraToLocation(latLng: LatLng) { + val cameraPosition = if (map.cameraPosition.zoom > MAP_ZOOM) { + CameraUpdateFactory.newLatLng(latLng) + } else { + CameraUpdateFactory.newLatLngZoom(latLng, MAP_ZOOM) + } + map.clear() map.addMarker { position(latLng) - title(getString(R.string.marker_title)) - icon(requireContext(), R.drawable.ic_my_location) } + map.animateCamera(cameraPosition) } private fun setupShowWeatherWindow(address: Address) = with(binding) { + val latLng = LatLng(address.latitude, address.longitude) + moveCameraToLocation(latLng) cityName.text = address.locality cityLatlng.text = getString(R.string.cityLatlng, address.latitude, address.longitude) closeWindowButton.setOnClickListener { showWeatherWindow.animateOut() } diff --git a/app/src/main/java/com/edwin/weatherapp/presentation/map/MapViewModel.kt b/app/src/main/java/com/edwin/weatherapp/presentation/map/MapViewModel.kt index 2377533..2909ab9 100644 --- a/app/src/main/java/com/edwin/weatherapp/presentation/map/MapViewModel.kt +++ b/app/src/main/java/com/edwin/weatherapp/presentation/map/MapViewModel.kt @@ -21,6 +21,8 @@ class MapViewModel( private val eventChannel = Channel(Channel.BUFFERED) val eventsFlow = eventChannel.receiveAsFlow() + var isLocationNotChecked: Boolean = true + fun getFusedLocation() = viewModelScope.launch { _uiState.value = MapUiState.Loading val location = getFusedLocationUseCase(Unit).single() diff --git a/data/src/main/java/com/edwin/data/entity/util/Converter.kt b/data/src/main/java/com/edwin/data/entity/util/Converter.kt index 34525e2..6d3dc62 100644 --- a/data/src/main/java/com/edwin/data/entity/util/Converter.kt +++ b/data/src/main/java/com/edwin/data/entity/util/Converter.kt @@ -8,26 +8,32 @@ import kotlin.math.roundToInt fun WeatherDTO.toDomain(): WeatherDetails { return WeatherDetails( - main["temp"]?.roundToInt() ?: 0, + main[WeatherParameters.Temp.toLowerCase()]?.roundToInt() ?: 0, weather.first().icon, toBriefWeatherInfo(weather.first().description), weather.first().description, - main["humidity"]?.toInt() ?: 0, - wind["speed"] ?: 0.0, - wind["deg"]?.toInt()?.let { WindDirection.degreeToDirection(it) } ?: WindDirection.NORTH, - main["pressure"] ?: 0.0 + main[WeatherParameters.Humidity.toLowerCase()]?.toInt() ?: 0, + wind[WeatherParameters.Speed.toLowerCase()] ?: 0.0, + wind[WeatherParameters.Deg.toLowerCase()]?.toInt() + ?.let { WindDirection.degreeToDirection(it) } ?: WindDirection.NORTH, + main[WeatherParameters.Pressure.toLowerCase()] ?: 0.0 ) } -private fun toBriefWeatherInfo(description: String) = when (description) { - "scattered clouds" -> BriefWeatherInfo.SCATTERED_CLOUDS - "clear sky" -> BriefWeatherInfo.CLEAR_SKY - "rain" -> BriefWeatherInfo.RAIN - "thunderstorm" -> BriefWeatherInfo.THUNDERSTORM - "few clouds" -> BriefWeatherInfo.FEW_CLOUDS - "broken clouds" -> BriefWeatherInfo.BROKEN_CLOUDS - "shower rain" -> BriefWeatherInfo.SHOWER_RAIN - "snow" -> BriefWeatherInfo.SNOW - "mist" -> BriefWeatherInfo.MIST - else -> BriefWeatherInfo.DEFAULT -} \ No newline at end of file +private val String.asEnumValueName: String + get() { + return this.replace(' ', '_').uppercase() + } + +inline fun > valueOf(type: String, default: T): T { + return try { + java.lang.Enum.valueOf(T::class.java, type) + } catch (e: IllegalArgumentException) { + default + } +} + +private fun toBriefWeatherInfo(description: String) = valueOf( + description.asEnumValueName, + BriefWeatherInfo.DEFAULT +) \ No newline at end of file diff --git a/data/src/main/java/com/edwin/data/entity/util/WeatherParameters.kt b/data/src/main/java/com/edwin/data/entity/util/WeatherParameters.kt new file mode 100644 index 0000000..a1cac06 --- /dev/null +++ b/data/src/main/java/com/edwin/data/entity/util/WeatherParameters.kt @@ -0,0 +1,13 @@ +package com.edwin.data.entity.util + +enum class WeatherParameters { + Temp, + Humidity, + Speed, + Deg, + Pressure, ; + + fun toLowerCase(): String { + return name.lowercase() + } +} \ No newline at end of file