Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 396
clzola Avatar asked May 30 '18 08:05

clzola


People also ask

How do you encapsulate the live data?

To encapsulate your app's data, you use both MutableLiveData and LiveData objects. MutableLiveData vs. LiveData : Data in a MutableLiveData object can be changed, as the name implies.

What is mediator live data?

MediatorLiveData is a subclass of MutableLiveData that can observe other LiveData objects and react to OnChanged events from them.


2 Answers

You can use my helper method:

val profile = MutableLiveData<ProfileData>()  val user = MutableLiveData<CurrentUser>()  val title = profile.combineWith(user) { profile, user ->     "${profile.job} ${user.name}" }  fun <T, K, R> LiveData<T>.combineWith(     liveData: LiveData<K>,     block: (T?, K?) -> R ): LiveData<R> {     val result = MediatorLiveData<R>()     result.addSource(this) {         result.value = block(this.value, liveData.value)     }     result.addSource(liveData) {         result.value = block(this.value, liveData.value)     }     return result } 
like image 104
Marek Kondracki Avatar answered Oct 05 '22 23:10

Marek Kondracki


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

like image 35
guness Avatar answered Oct 05 '22 21:10

guness