Abikayusri

DataSourceLocalUsers.kt

Sep 21st, 2020
817
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package abika.sinau.assignmentweek8.repository.users
  2.  
  3. import abika.sinau.assignmentweek8.data.database.user.UsersDao
  4. import abika.sinau.assignmentweek8.data.database.user.UsersDatabase
  5. import abika.sinau.assignmentweek8.data.database.user.UsersEntity
  6. import android.util.Log
  7. import androidx.lifecycle.LiveData
  8. import androidx.lifecycle.MutableLiveData
  9. import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
  10. import io.reactivex.rxjava3.core.Observable
  11. import io.reactivex.rxjava3.schedulers.Schedulers
  12.  
  13. /**
  14.  * Created by Abika Chairul Yusri on 21/09/2020
  15.  * Bismillahirrahmanirrahim
  16.  */
  17. class DataSourceLocalUsers(private val usersDao: UsersDao) {
  18.     private val TAG = "DataSourceLocalUsers"
  19.  
  20.     private fun addUsers(user: UsersEntity): LiveData<Long> {
  21.         val idUser = MutableLiveData<Long>()
  22.  
  23.         Observable.fromCallable { usersDao.insertDataUsers(user) }
  24.             .subscribeOn(Schedulers.io())
  25.             .observeOn(AndroidSchedulers.mainThread())
  26.             .subscribe({
  27.                 idUser.postValue(it)
  28.                 Log.d(TAG, "addUsers: $it")
  29.             }, {
  30.                 idUser.postValue(null)
  31.             })
  32.         return idUser
  33.     }
  34.  
  35. //    private fun loginUsers(email: String, password: String): LiveData<UsersEntity> {
  36.     private fun loginUsers(email: String): LiveData<UsersEntity> {
  37.  
  38.         val user = MutableLiveData<UsersEntity>()
  39.  
  40. //        usersDao.getSelectedDataUsers(email, password)
  41.         usersDao.getSelectedDataUsers(email)
  42.             .subscribeOn(Schedulers.io())
  43.             .observeOn(AndroidSchedulers.mainThread())
  44.             .subscribe({
  45.                 user.postValue(it)
  46.                 Log.d(TAG, "loginUsers: $it")
  47.             }, {
  48.                 user.postValue(null)
  49.                 Log.d(TAG, "loginUsers: ${it.message}")
  50.             })
  51.         return user
  52.     }
  53. }
RAW Paste Data