Check location permissions with MVVM in Android

androidmvvmpermissions

I need to request runtime location permissions for the FusedLocationProviderClient api and, once I have them, obtain Location objects and feed them to an algorithm that will make a query to an endpoint. The results of such a query will be used to update my map View.
As of now, I'm using MVVM, so I have an Activity showing the Map, and its ViewModel implementing LiveData objects to update the View, and a Repository class to perform network operations.
MVVM by Android

My doubts are essentially about where to ask for runtime permissions (I'm using EasyPermissions library). I know that I have to implement permission checks methods in the Activity, however I find myself at odds with how to proceed then: do I have to also implement a listener for location updates in the Activity? If so, how do I then send that data to the repo for the query (the View will then be updated with Observer pattern)? In a way, it would be more convenient for me to ask for permissions in the repo since that's where I'll immediately use the location objects.

Best Answer

As you are dealing with location, you should probably use the Lifecycle library, main use case of this library is for your use-case only.

https://developer.android.com/topic/libraries/architecture/lifecycle

And for the Permissions I would ask in activity and use Observer pattern to send the location updates to the view model.

Related Topic