I want to make the app to jogging with MVVM pattern, but I don't have idea where to put LocationListener. How to correctly use LocationListener together with MVVM? Repository? For the testing I've created something like this. It's only test to check the operation and it works fine, but this not MVVM.
class GPSLocationListener(private var activity: MainActivity) : LocationListener {
lateinit var location: Location
override fun onLocationChanged(location: Location?) {
val speed : Double = (location!!.speed * 3600 / 1000).toDouble()
activity.updateUI(speed)
this.location = location
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onProviderEnabled(provider: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onProviderDisabled(provider: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
passing view(main activity) reference to GPSLocationListener
is a wrong approach because it might be a reason for memory leak(take a look at this about memory leak to understand better)
on the other hand listening device's location is tied to Android platform so it should be handled in activity
or fragment
if you'll be performing heavy operation with the result of location request you should use view model along with RxJava or Coroutines and then you should listen your data(wrapped in LiveData) in your view(activity-fragment)