[英]MVVM - where should I call for user location?
currently, I'm building an app which will show for the user recent forecast.目前,我正在构建一个应用程序,它将显示用户最近的预测。 For make an api call, I have to provide longitude and latitude as parameter.
为了进行 api 调用,我必须提供经度和纬度作为参数。 For now, I have written some code about getting langitude, and longitude, but I don't receive proper data.
目前,我已经编写了一些关于获取经度和经度的代码,但我没有收到正确的数据。 As I written those methods in MainActivity, at first, the longitude, and latitude is equal to 0.0, after about two seconds it managing to get proper data.
当我在 MainActivity 中编写这些方法时,起初,经度和纬度等于 0.0,大约两秒钟后它设法获取正确的数据。 Should I freeze app until the locationManage will get proper data, or should I call those methods somewhere else?
我应该冻结应用程序直到 locationManage 获得正确的数据,还是应该在其他地方调用这些方法? Should I call them in repository?
我应该在存储库中调用它们吗?
Checking permissions检查权限
if (ContextCompat.checkSelfPermission(
getApplicationContext(), Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{
Manifest.permission.INTERNET, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
}, 1);
}
}
}
onLocationChanged onLocationChanged
@Override
public void onLocationChanged(@NonNull Location location) {
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
Log.i(TAG, "onLocationChanged: "+mLatitude+" "+mLongitude);
}
In repository在存储库中
public static double mLatitude = MainActivity.mLatitude;
public static double mLongitude = MainActivity.mLongitude;
public ForecastRepository(){
mApi = RetrofitBuilder.makeCall();
}
public MutableLiveData<ForecastModel> testCall() {
MutableLiveData<ForecastModel> data = new MutableLiveData<>();
//TODO temporary values such as latitude/longitude in api call
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());
}
Log.i(TAG, "onResponse: successful "+mLatitude+" "+mLongitude);
data.setValue(response.body());
}
@Override
public void onFailure(Call<ForecastModel> call, Throwable t) {
Log.i(TAG, "onFailure: "+t.getMessage());
}
});
return data;
}
}
The correct way would be to have a LocationRepository
where you do all fetching in a background thread
of the device location.正确的方法是拥有一个
LocationRepository
,您可以在其中在设备位置的background thread
中进行所有获取。
Create a MutableLiveData
in ViewModel where you will assign the location.在 ViewModel 中创建一个
MutableLiveData
,您将在其中分配位置。
Then update its value using postValue() or setValue().然后使用 postValue() 或 setValue() 更新其值。
Write a public getter for this MutableLiveData.为此 MutableLiveData 编写一个公共 getter。 This repository will be your location API.
此存储库将是您的位置 API。
Then your ViewModel should call the method
of your repository and assign its result to a LiveData variable of the ViewModel.然后,您的 ViewModel 应该
call the method
并将其结果分配给 ViewModel 的 LiveData 变量。
Then in your Activity observe
the liveData of your ViewModel.然后在您的 Activity 中
observe
您的 ViewModel 的 liveData。
And make your API Call.并拨打您的 API 电话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.