简体   繁体   English

MVVM - 我应该在哪里调用用户位置?

[英]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.

相关问题 我应该在MVVM中的哪里放置下载器类? - Where should i put downloader classes in MVVM? 我应该在哪里调用 Rest API 片段 - Where should I call Rest API in fragment 我应该在哪里调用 MediaPlayer.start(); - Where should I call MediaPlayer.start(); 我在哪里应该在Android中调用Interface? - Where should I call Interface in android? 泽西岛2.22:我应该在哪里定义REST资源的位置? - Jersey 2.22: Where should I define the location of REST resources? 如果这些项目是通过弹出窗口 window 的用户输入添加的,我应该在哪里调用从 ListView 中删除项目的方法? - Where should I call a method to remove items from a ListView if those items were added through user input from a Popup window? 在Spring MVC Hibernate应用程序中,应该将用户图像保存在哪里? - In a Spring MVC Hibernate application, where should I save user images? 我应该在哪里调用更新映射函数?我需要在异步任务之后调用它? - Where should I call my update map function?I need to call it after my Async Task? .txt文件的位置应该在哪里? - Where should my .txt file location be? 我想将图标从一个位置点移动到另一点,图标应在Android中将一个位置移动到另一个位置 - I want to move icon from one location point to another point where icon should move one location to other in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM