[英]I can't share variables between two repositories using mvvm
我需要在兩個存儲庫之間共享兩個變量。 我已經嘗試了許多不同的解決方案,但都沒有奏效。 簡而言之:第一個存儲庫包含用戶最后已知的位置(地理緯度和經度)第二個存儲庫調用 api 並具有指定的端點,例如地理緯度和經度。 問題是我不斷收到等於 0,0 的值。 你能給我一些提示我應該怎么做嗎? 我觀察到的一件事是,整個程序立即運行,而位置存儲庫需要幾秒鍾才能實際獲得緯度和經度。 所以程序只是不斷地調用 api 端點等於 0,0,正如我上面的意思。
位置存儲庫
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();
}
});
}
}
}
}
預測存儲庫
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;
}
視圖模型
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();
}
您可以在 ViewModel 和 Transformation.switchMap() 中使用兩個 livadata 來跟蹤更改。 每次 mLocationData 更改時,mForcastData 也會更新。 如果你不知道 switchMap 是如何工作的,你可以檢查一下如何以及在哪里使用 Transformations.switchMap? .
像這樣更改您的 ViewModel 和 LocationRepository。
視圖模型
public class ForecastViewModel extends AndroidViewModel {
private ForcastRepository mForecastRepository;
private LocationRepostiory mLocationRepository;
private LiveData<ForecastModel> mForecastData;
private LiveData<LocationModel> mLocationData;
public ForecastViewModel(@NonNull Application application) {
super(application);
mLocationRepository = new LocationRepostiory();
mLocationRepository.test(application);
mLocationData = mLocationRepository.getMutableLiveData();
mForecastRepository = new ForcastRepository();
mForecastData = Transformations.switchMap(mLocationData, location->
mForecastRepository.testCall(location.getLatitude(), location.getLongitude()));
}
public LiveData<ForecastModel> getmForecastData() {
return mForecastData;
}
}
位置存儲庫
class LocationRepostiory {
private LocationModel mLocation;
private MutableLiveData<LocationModel> mutableLiveData = new MutableLiveData();
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(location -> {
mLocation = new LocationModel(location.getLongitude(),location.getLatitude());
mutableLiveData.setValue(mLocation);
});
}
}
}
public MutableLiveData<LocationModel> getMutableLiveData() {
return mutableLiveData;
}
}
預測庫
public MutableLiveData<ForecastModel> testCall(double mLatitude, double mLongitude ) {
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;
}
位置模型
class LocationModel {
private Double longitude, latitude;
public LocationModel(Double longitude, Double latitude) {
this.longitude = longitude;
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public Double getLatitude() {
return latitude;
}
}
如果您不想制作額外的 LocationModel class ,那么您也可以將您的位置數據作為 List 或 Arraylist 發送。 只是我更喜歡這個。
編輯:我已經糾正了一些錯誤。 現在代碼正在運行。 Logcat from testCall() Method in ForcastRepository D/TAG: testCall: 90.3993212,23.7793183
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.