Search code examples
androidobserver-patternandroid-roomandroid-architecture-componentsandroid-livedata

How to combine two live data one after the other?


I have next use case: User comes to registration form, enters name, email and password and clicks on register button. After that system needs to check if email is taken or not and based on that show error message or create new user...

I am trying to do that using Room, ViewModel and LiveData. This is some project that on which I try to learn these components and I do not have remote api, I will store everything in local database

So I have these classes:

  • RegisterActivity
  • RegisterViewModel
  • User
  • UsersDAO
  • UsersRepository
  • UsersRegistrationService

So the idea that I have is that there will be listener attached to register button which will call RegisterViewModel::register() method.

class RegisterViewModel extends ViewModel {

    //...

    public void register() {
        validationErrorMessage.setValue(null);
        if(!validateInput())
            return;
        registrationService.performRegistration(name.get(), email.get(), password.get());
    }

    //...

}

So that is the basic idea, I also want for performRegistration to return to me newly created user.

The thing that bothers me the most is I do not know how to implement performRegistration function in the service

class UsersRegistrationService {
    private UsersRepository usersRepo;

    //...

    public LiveData<RegistrationResponse<Parent>>  performRegistration(String name, String email, String password) {
         // 1. check if email exists using repository
         // 2. if user exists return RegistrationResponse.error("Email is taken") 
         // 3. if user does not exists create new user and return RegistrationResponse(newUser)
    }
}

As I understand, methods that are in UsersRepository should return LiveData because UsersDAO is returning LiveData

@Dao
abstract class UsersDAO { 
    @Query("SELECT * FROM users WHERE email = :email LIMIT 1")
    abstract LiveData<User> getUserByEmail(String email);
}

class UsersRepository {
    //...
    public LiveData<User> findUserByEmail(String email) {
        return this.usersDAO.getUserByEmail(email);
    }
}

So my problem is how to implement performRegistration() function and how to pass value back to view model and then how to change activity from RegisterActivity to MainActivity...


Solution

  • With the help of MediatorLiveData, you can combine results from multiple sources. Here an example of how would I combine two sources:

    class CombinedLiveData<T, K, S>(source1: LiveData<T>, source2: LiveData<K>, private val combine: (data1: T?, data2: K?) -> S) : MediatorLiveData<S>() {
    
        private var data1: T? = null
        private var data2: K? = null
    
        init {
            super.addSource(source1) {
                data1 = it
                value = combine(data1, data2)
            }
            super.addSource(source2) {
                data2 = it
                value = combine(data1, data2)
            }
        }
    
        override fun <S : Any?> addSource(source: LiveData<S>, onChanged: Observer<in S>) {
            throw UnsupportedOperationException()
        }
    
        override fun <T : Any?> removeSource(toRemove: LiveData<T>) {
            throw UnsupportedOperationException()
        }
    }
    

    here is the gist for above, in case it is updated on the future: https://gist.github.com/guness/0a96d80bc1fb969fa70a5448aa34c215