1

I am new to MVVM architecture and i just want to know how to communicate between repository class and the UI (activity/fragment) class.I came across with live data which is doing this job for updating same entities from both (remote and room database).

For example : 1) If i have entity named User. I can save and the observe it using live data like below : (from android developers website).

    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) But how can we do this in other API'S like (login) which does not need live data but i just want to show or hide progress dialog depend on the network success or error messages.

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
        });
}
Mohamed Niyaz
  • 228
  • 1
  • 9

1 Answers1

0

You need to make isVerifiedUser() return a liveData which you can observe inside the viewModel related to that UI(activity/fragment).

1. Inside Repository:

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. Activity:

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

More Infos:

LiveData

ViewModel

Guide to app architecture MVVM

Yassin Ajdi
  • 1,540
  • 14
  • 13
  • Thanks for your answer . Can you please explain it so that i can able to understand it clearly. – Mohamed Niyaz Dec 19 '18 at 11:07
  • @MohamedNiyaz please read the resources I have added. you need to understand them in order to use this architecture. – Yassin Ajdi Dec 19 '18 at 12:55
  • Thanks for the links @Yassin Ajdi. I have gone through the above and understand the architecture as much as clear. But i have a doubt in your repository code why you are passing the state ? and instead of a int how can i pass the object variable. – Mohamed Niyaz Dec 24 '18 at 10:59
  • @MohamedNiyaz you can pass whatever object you want as parameters of that method. what's important is the return value it must be a LiveData object so you can observe it inside ViewModel – Yassin Ajdi Dec 27 '18 at 14:50