簡體   English   中英

如何從MVVM體系結構中的Web服務的存儲庫類與活動/片段進行通信

[英]how to communicate to activity/fragment from repository class for web service in MVVM architecture

我是MVVM architecture新手,我只想知道如何在repository classUI (activity/fragment) class之間進行通信。我遇到了實時數據,該數據可以same entities from both (remote and room database)更新same entities from both (remote and room database)

例如:1)如果我有一個名為User的實體。 我可以使用以下實時數據保存並觀察它:(來自android開發人員網站)。

    public class UserRepository {
    private final Webservice webservice;
    private final UserDao userDao;
    private final Executor executor;

    @Inject
    public UserRepository(Webservice webservice, UserDao userDao, Executor executor) {
        this.webservice = webservice;
        this.userDao = userDao;
        this.executor = executor;
    }

    public LiveData<User> getUser(String userId) {
        refreshUser(userId);
        // Returns a LiveData object directly from the database.
        return userDao.load(userId);
    }

    private void refreshUser(final String userId) {
        // Runs in a background thread.
        executor.execute(() -> {
            // Check if user data was fetched recently.
            boolean userExists = userDao.hasUser(FRESH_TIMEOUT);
            if (!userExists) {
                // Refreshes the data.
                Response<User> response = webservice.getUser(userId).execute();

                // Check for errors here.

                // Updates the database. The LiveData object automatically
                // refreshes, so we don't need to do anything else here.
                userDao.save(response.body());
            }
        });
    }
}

2)但是如何在其他API(例如,登錄)中做到這一點,這些API不需要實時數據,但我只想顯示或隱藏進度對話框,這取決於網絡成功或錯誤消息。

public void isVerifiedUser(int userId){
      executor.execute(() -> {
        // making request to server for verifying user

        Response<User> response = webservice.getVerifyUser(userId).execute();

          // how to update the UI like for success or error.
          //update the progress dialog also in UI class
        });
}

您需要使isVerifiedUser()返回一個liveData,您可以在與該UI(活動/片段)相關的viewModel中觀察該數據。

1.內部存儲庫:

public LiveData<State> isVerifiedUser(int userId){

    MutableLiveData<State> isVerified = new MutableLiveData();

    executor.execute(() -> {           
        Response<User> response = webservice.getVerifyUser(userId).execute();
        // Update state here.
        isVerified.postValue(valueHere)
    });

    return isVerified;
}

2. ViewModel:

 public ViewModel(final Repository repository) {
        //observe userId and trigger isVerifiedUser when userId value is changed
        stateLiveData = Transformations.map(userId, new Function<>() {
            @Override
            public RepoMoviesResult apply(Integer userId) {
                return repository.isVerifiedUser(userId);
            }
        });
    }

3.活動:

viewModel.getStateLiveData ().observe(this, new Observer<>() {
    @Override
    public void onChanged(State state) {
         //do something here
    }
});

更多信息:

實時數據

視圖模型

MVVM應用程序架構指南

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM