Hi, I am creating tracking app, which draws walked...
# android
d
Hi, I am creating tracking app, which draws walked path into the map. App should work also with turned-off screen. I would like to use MVVM pattern. To ensure tracking with turned-off screen I am using foreground service, which implements LocationListener interface. Does anyone have any suggestions or examples how I should communicate between Service and viewModel to get updates liveData in viewModel on each localization change?
w
Don’t treat ViewModel as the destination for your data. Rather pull the state up, scope it to the application, and observe from the view model. For example with Dagger, I’d create a
@Singleton
class that receives the location, updates the path (e.g. appends new location to a list), and exposes that list’s updates somehow (via Rx, Flow, callbacks). Then inject the same instance to the service and view model, from the service push updates, and from the viewmodel listen to the changes
This way you can only listen to updates from the viewmodel when the view is actually visible