I need to share two variables between two repositories. I've already tried many different solutions, but none worked. In short: first repository contain user last known location (geographical latitude and longitude) second repository calling the api with specified endpoints, such as geographical latitude and longitude. The thing is I consantly receiving those values with equal to 0,0. Could you give me some hint how I should do it? One thing that I observerd is that, the whole program runs immediatly, while the location repository need a couple of seconds to actuall get the latitude and longitude. So the program just constantly calling the api with endpoints equal to 0,0 as I meantioned above.
Location repository
public class LocationRepository {
private double mLatitude, mLongitude;
public void test(Application application) {
FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(application.getApplicationContext());
{
if (ContextCompat.checkSelfPermission(
application.getApplicationContext(), Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(application.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(application.getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
client.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
mLongitude = location.getLongitude();
mLatitude = location.getLatitude();
}
});
}
}
}
}
Forecast repository
public MutableLiveData<ForecastModel> testCall() {
MutableLiveData<ForecastModel> data = new MutableLiveData<>();
mApi.test(mLatitude, mLongitude, "metric", API_KEY).enqueue(new Callback<ForecastModel>() {
@Override
public void onResponse(Call<ForecastModel> call, Response<ForecastModel> response) {
if (!response.isSuccessful()) {
Log.i(TAG, "onResponse: " + response.code());
}
data.setValue(response.body());
}
@Override
public void onFailure(Call<ForecastModel> call, Throwable t) {
Log.i(TAG, "onFailure: " + t.getMessage());
}
});
return data;
}
ViewModel
private ForecastRepository mForecastRepository;
private LocationRepository mLocationRepository;
private MutableLiveData<ForecastModel> mForecastData;
public ForecastViewModel(@NonNull Application application) {
super(application);
mLocationRepository = new LocationRepository();
mLocationRepository.test(application);
mForecastRepository = new ForecastRepository();
mForecastData = mForecastRepository.testCall();
}