Consider this if you do not wanna use Dependency Injection.
This is a minimal example leaving lots of stuff open for improvements!
In this example we not gonna pass down the Context
as this can lead to memory leaks (if not thoroughly handled) but creating the FusedLocationProviderClient
in the Activity and passing it to the ViewModel and the Repository.
Note: Avoid passing Android related things like Context
, View
's etc down to your ViewModel and lower layers. It will make testing and refactorings easier, because you less depend on Android.
In your Activity you get your ViewModel and you create your FusedLocationProviderClient
:
public class YourActivity : Activity() {
private FusedLocationProviderClient locationProvider = LocationServices.getFusedLocationProviderClient(this.applicationContext);
// Either you pass FusedLocationProviderClient via constructor to the ViewModel, or via a setter later
private YourViewModel viewModel = // ...
override void onCreate() {
super.onCreate();
// if you wanna set the locationProvider via setter
viewModel.setLocationProvider(locationProvider); // this should not leak when the activity finishes as there is no reference to the Activity or its view
}
}
Then in your ViewModel you either get the locationProvider
via Constructor or a setter:
public class YourViewModel extends ViewModel {
// You can either create a new LocationRepository in the viewModel,
// or you can pass an instance via Constructor (which I recommend for better testability)
// or you pass it via a setter
private LocationRepository locationRepository;
public YourViewModel(FusedLocationProviderClient locationProvider) {
locationRepository = new LocationRepository(locationProvider);
}
// alternatively using a setter
public void setFusedLocationProviderClient(FusedLocationProviderClient locationProvider) {
locationRepository = new LocationRepository(locationProvider);
}
// this function calls your repository
public void foo() {
// ensure LocationRepository is initialized or you will get a Nullpointer here,
// or check if (locationRepository != null) { ... } before accessing it
localRepository.test()
}
@Override
public void onCleared() {
// do cleanup
// stop ongoing work (in your repository)
// unregister listeners (in your repository)
}
}
public class LocationRepository {
private FusedLocationProviderClient locationProvider;
public LocationRepository(FusedLocationProviderClient locationProvider) {
this.locationProvider = locationProvider;
}
public void test() {
// ... now you can use FusedLocationProviderClient (without the need of having a context
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…