Last active
April 24, 2020 06:48
-
-
Save NinoDLC/1e29ee9af730431db0dcfdd01f80b06d to your computer and use it in GitHub Desktop.
Permission and UI widget manipulation in MVVM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapFragment : SupportMapFragment() { | |
private lateinit var mapViewModel: MapViewModel | |
private lateinit var googleMap: GoogleMap | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
mapViewModel.viewActionLiveData.observe( | |
this, | |
Observer { viewAction: ViewAction -> | |
when (viewAction) { | |
is ViewAction.EnableMapLocationOptions -> setUiForLocationEnabled(viewAction.mapLocationOptionsModel) | |
is ViewAction.AskPermission -> ActivityCompat.requestPermissions(requireActivity(), viewAction.permissions, 0) | |
}.exhaustive | |
} | |
) | |
/* ... */ | |
getMapAsync { | |
googleMap = it | |
/* ... */ | |
} | |
} | |
/* ... */ | |
private fun setUiForLocationEnabled(mapLocationOptionsModel: MapLocationOptionsModel) { | |
googleMap.isMyLocationEnabled = mapLocationOptionsModel.isMyLocationEnabled | |
googleMap.uiSettings.isMyLocationButtonEnabled = mapLocationOptionsModel.isMyLocationButtonEnabled | |
} | |
override fun onResume() { | |
super.onResume() | |
mapViewModel.refreshPermissions() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapViewModel( | |
private val permissionChecker: PermissionChecker | |
/* ... */ | |
) : ViewModel() { | |
private val _viewActionLiveData = SingleLiveEvent<ViewAction>() | |
val viewActionLiveData: LiveData<ViewAction> = _viewActionLiveData | |
/* ... */ | |
fun refreshPermissions() { | |
if (!permissionChecker.hasLocationPermission()) { | |
_viewActionLiveData.value = ViewAction.AskPermission(arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION)) | |
} | |
} | |
sealed class ViewAction { | |
data class EnableMapLocationOptions(val mapLocationOptionsModel: MapLocationOptionsModel) : ViewAction() | |
class AskPermission(val permissions: Array<String>) : ViewAction() | |
} | |
data class MapLocationOptionsModel( | |
val isMyLocationEnabled: Boolean, | |
val isMyLocationButtonEnabled: Boolean | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PermissionChecker(private val application: Application) { | |
fun hasLocationPermission(): Boolean = | |
ContextCompat.checkSelfPermission(application, android.Manifest.permission.ACCESS_FINE_LOCATION) == PERMISSION_GRANTED | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment