Skip to content

Commit

Permalink
Code review [#25] P15, P16 - enum refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
E-D-W-I-N committed May 16, 2021
1 parent 3a6c5fe commit 87239b0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() } }
)
}
}
Expand All @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class MapViewModel(
private val eventChannel = Channel<ActionState>(Channel.BUFFERED)
val eventsFlow = eventChannel.receiveAsFlow()

var isLocationNotChecked: Boolean = true

fun getFusedLocation() = viewModelScope.launch {
_uiState.value = MapUiState.Loading
val location = getFusedLocationUseCase(Unit).single()
Expand Down
40 changes: 23 additions & 17 deletions data/src/main/java/com/edwin/data/entity/util/Converter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
private val String.asEnumValueName: String
get() {
return this.replace(' ', '_').uppercase()
}

inline fun <reified T : Enum<T>> 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
)
13 changes: 13 additions & 0 deletions data/src/main/java/com/edwin/data/entity/util/WeatherParameters.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.edwin.data.entity.util

enum class WeatherParameters {
Temp,
Humidity,
Speed,
Deg,
Pressure, ;

fun toLowerCase(): String {
return name.lowercase()
}
}

0 comments on commit 87239b0

Please sign in to comment.