Android观察多个MutableLiveData

问题描述 投票:0回答:1

我有一个MVVM设置,其中有两个片段通过ViewModel进行交互。我的SourceFragment正在更新VM中的两个MutableLiveData LatLng。我的第二个是MapFragment,其中我使用两个LatLng LiveData绘制了一条路线。活动启动时,两个片段都被加载。 SourceFragment更新LatLng。我需要等待地图加载,然后再请求绘制路线,并且我有第三个MutableLiveData布尔值,一旦加载地图,我就对其进行更新。目前,我实现此目标的方法是通过为我的MutableLiveData中的三个MapFragment设置三个单独的viewModel观察器,并使用一组if语句来检查是否同时设置了所有三个MutableLiveData。我感觉可以使用MediatorLiveData之类的方法来更好地完成此操作,但是我还没有找到可以解决的示例。 MediatorLiveData或其他适用于此处的内容吗?

我的源代码片段

viewModel.setPickupLocation(pickupLatLng);
viewModel.setDestinationLatLng(destinationLatLng());

我的地图片段

@Override
public void onActivityCreated(@NonNull Bundle savedInstanceState) {
    Log.d(TAG, "onActivityCreated: called");
    super.onActivityCreated(savedInstanceState);

    getCustomerOrDriverViewModel();

}

private void initViewModels() { 

    viewModel.getPickupLocation().observe(getViewLifecycleOwner(), new Observer<LatLng>() {
        @Override
        public void onChanged(@NonNull LatLng pickupLocation) {
            Log.d(TAG, "onChanged: from onActivityCreated: called for getPickupLocation");

            if(viewModel.getMapReady().getValue() != null) {
                if(viewModel.getMapReady().getValue()) {
                    resetRoute();

                    setSingleMarker(pickupLocation);
                    if(viewModel.getDestinationLatLng().getValue() != null) {
                        plotRoute(viewModel.getDestinationLatLng().getValue(), pickupLocation);
                    }
                }
            }
        }
    });

    viewModel.getDestinationLatLng().observe(getViewLifecycleOwner(), new Observer<LatLng>() {
        @Override
        public void onChanged(@NonNull LatLng destinationLatLng) {
            Log.d(TAG, "onChanged: from onActivityCreated: called for getPickupLocation");
            if(viewModel.getMapReady().getValue() != null) {
                if(viewModel.getMapReady().getValue()) {
                    resetRoute();

                    if(viewModel.getPickupLocation().getValue() != null) {
                        plotRoute(destinationLatLng, viewModel.getPickupLocation().getValue());
                    }
                }
            }
        }
    });

    viewModel.getMapReady().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
        @Override
        public void onChanged(@NonNull Boolean ready) {
            Log.d(TAG, "onChanged: from onActivityCreated: called for getMapReady");
            if(ready) {
                if(viewModel.getPickupLocation().getValue() != null && viewModel.getDestinationLatLng().getValue() != null) {
                    Log.d(TAG, "onChanged: from onActivityCreated: called for getMapReady: plotting route");
                    resetRoute();
                    plotRoute(viewModel.getDestinationLatLng().getValue(), viewModel.getPickupLocation().getValue());
                }
            }
        }
    });
}

mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {
        viewModel.setMapReady(true);
    }
});

我的ViewModel

private MutableLiveData<LatLng> pickupLocation = new MutableLiveData<>();
private MutableLiveData<LatLng> destinationLatLng = new MutableLiveData<>();
private MutableLiveData<Boolean> mapReady = new MutableLiveData<>();

public void setPickupLocation(LatLng input) {
    Log.d(TAG, "setPickupLocation: ");
    pickupLocation.setValue(input);
}

public void setDestinationLatLng(LatLng input) {
    Log.d(TAG, "setPickupLocation: ");
    destinationLatLng.setValue(input);
}

public void setMapReady(Boolean input) {
    Log.d(TAG, "setPickupLocation: ");
    mapReady.setValue(input);
}

public MutableLiveData<LatLng> getPickupLocation() {
    Log.d(TAG, "getPickupLocation: ");
    return pickupLocation;
}

public MutableLiveData<LatLng> getDestinationLatLng() {
    Log.d(TAG, "getDestinationLatLng: ");
    return destinationLatLng;
}

public MutableLiveData<Boolean> getMapReady() {
    Log.d(TAG, "getMapReady: ");
    return mapReady;
}
android mvvm android-viewmodel mutablelivedata mediatorlivedata
1个回答
0
投票
然后创建一个辅助方法

private void setupData(){ if(variable1 == null || variable2 == null || variable3 == null){ return; } plotRoute(xxxxxx); }

然后在每个视图模型响应中调用setupData()代替三重嵌套的if语句检查

© www.soinside.com 2019 - 2024. All rights reserved.