简体   繁体   English

在Android中的模块之间共享ViewModel实例

[英]Share ViewModel instance between module in android

I am working on MVVM Architecture. 我正在研究MVVM体系结构。 I want to share an instance of view model between module in my android app. 我想在我的Android应用程序中的模块之间共享视图模型的实例。 when user complete the ride from app module I would like to access my chat module view model instance to perform some db operation ie clear Conversation Entity, etc. I am using Room Database with View Model. 当用户完成从应用程序模块的行驶时,我想访问我的聊天模块视图模型实例以执行一些数据库操作,即清除对话实体等。我正在使用带有视图模型的Room Database。 ChatActivityNew is an activity in chat module. ChatActivityNew是聊天模块中的活动。

App Module Booking Activity 应用模块预订活动

Dialogs.INSTANCE.showRideStatusDialog(mCurrentActivity, new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Dialogs.INSTANCE.dismissDialog();
        Dialogs.INSTANCE.showLoader(mCurrentActivity);
        ChatActivityNew.setMukamalListener(iMukamalCallback);
        dataRepository.requestEndRide(mCurrentActivity, driversDataHandler);
    }
}

Chat Module ChatActivityNew 聊天模块ChatActivityNew

/**
 * Set the Mukamal Callback listener & call onMukamal abstract
 * method which takes Message view model as a parameter.
 *
 * @param iMukamalCallback is a callback listener.
 */
public static void setMukamalListener(IMukamalCallback iMukamalCallback) {
    mukamalCallback = iMukamalCallback;
    mukamalCallback.onMukamal(mModel);
}

mModel is null because activity is not loaded yet and MessageViewModel is null so how can i access an instance of MessageViewModel. mModel为null,因为尚未加载活动,而MessageViewModel为null,因此如何访问MessageViewModel的实例。

I have followed the android developer documentation https://developer.android.com/topic/libraries/architecture/viewmodel 我遵循了android开发人员文档https://developer.android.com/topic/libraries/architecture/viewmodel

Any Help will will be highly appreciable. 任何帮助将非常可贵。

EDIT 编辑

Dialogs.INSTANCE.showRideStatusDialog(mCurrentActivity, new View.OnClickListener() {
    @Override
    public void onClick(View v) {
          Dialogs.INSTANCE.dismissDialog();
          Dialogs.INSTANCE.showLoader(mCurrentActivity);
          EventBus.getDefault().postSticky(
              new MessageEvent(com.example.chatmodule.utils.Constants.RIDE_COMPLETE)
          );
          dataRepository.requestEndRide(mCurrentActivity, driversDataHandler);
        }
    }

Chat Module Subscribe Method 聊天模块订阅方法

@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(MessageEvent event) {
        if (mModel != null && event.message.equalsIgnoreCase(RIDE_COMPLETE)){
            mModel.deleteConversation();
            mModel.deleteMessages();
        }
    }

Ideally, you would want to de-couple these view models as much as possible. 理想情况下,您希望尽可能地取消这些视图模型的耦合。 To achieve what your're trying to do, you'd need an Event Aggregator mechanism. 为了实现您想要做的事情,您需要一个事件聚集器机制。

In your example, your chat module would subscribe to the events of type "NowSave" and your App moudle would publish an event of type "NowSave" when it requires data to be saved. 在您的示例中,您的聊天模块将订阅“ NowSave”类型的事件,并且您的应用程序模块将在需要保存数据时发布“ NowSave”类型的事件。

In the Android world, EventBus looks like the popular library to use for such mechanism: http://greenrobot.org/eventbus/ 在Android世界中, EventBus看起来像是用于这种机制的流行库: http : EventBus

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM