Advertisement
eveeeeef21

BelajarMediatorLive

Jun 29th, 2025 (edited)
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.64 KB | Source Code | 0 0
  1. package com.example.storyapp.ui.login
  2.  
  3. import androidx.lifecycle.LiveData
  4. import androidx.lifecycle.MediatorLiveData
  5. import androidx.lifecycle.ViewModel
  6. import com.example.storyapp.data.response.auth.LoginResult
  7. import com.example.storyapp.di.Repository
  8. import com.example.storyapp.utils.ResultState
  9.  
  10. class LoginViewModel(private val repository: Repository) : ViewModel() {
  11.  
  12.     fun performLogin2(email: String, password: String): LiveData<ResultState<LoginResult>> =
  13.         repository.login(email, password)
  14.  
  15.     // 1. Terapkan enkapsulasi. ViewModel memiliki state-nya sendiri.
  16.     private val _loginResult = MediatorLiveData<ResultState<LoginResult>>()
  17.     val loginResult: LiveData<ResultState<LoginResult>> = _loginResult
  18.  
  19.     // 2. Buat fungsi yang dipanggil UI untuk *memulai* proses login.
  20.     // Fungsi ini tidak mengembalikan apa-apa (Unit).
  21.     fun performLogin(email: String, password: String) {
  22.         // Panggil fungsi repository yang mengembalikan LiveData
  23.         val resultLiveData = repository.login(email, password)
  24.  
  25.         // 3. Tempelkan (source) LiveData dari repository ke LiveData internal ViewModel.
  26.         _loginResult.addSource(resultLiveData) { result ->
  27.             // 4. Update nilai LiveData internal dengan hasil dari repository.
  28.             _loginResult.value = result
  29.             // Hapus source setelah mendapatkan hasil agar tidak ada duplikasi observer
  30.             _loginResult.removeSource(resultLiveData)
  31.         }
  32.     }
  33. }
  34.  
  35.  
  36.  
  37. // di kelas Repository
  38. fun login(
  39.         email: String,
  40.         password: String
  41.     ): LiveData<ResultState<LoginResult>> = liveData {
  42.         emit(ResultState.Loading)
  43.         try {
  44.             val response = apiService.login(email, password)
  45.             if (!response.error) {
  46.                 val loginResult = response.loginResult
  47.                 userPreference.saveSession(loginResult) // Menyimpan data login ke sesi
  48.                 emit(ResultState.Success(loginResult))
  49.             } else {
  50.                 emit(ResultState.Error(ErrorType.ApiError(response.message)))
  51.             }
  52.         } catch (e: HttpException) {
  53.             if (e.code() == 401) {
  54.                 emit(ResultState.Error(ErrorType.ResourceError(R.string.error_wrong_credentials)))
  55.             } else {
  56.                 val errorBody = e.response()?.errorBody()?.string()
  57.                 val errorResponse = Gson().fromJson(errorBody, LoginResponse::class.java)
  58.                 emit(ResultState.Error(ErrorType.ApiError(errorResponse.message)))
  59.             }
  60.         } catch (e: Exception) {
  61.             emit(ResultState.Error(ErrorType.ResourceError(R.string.login_failed)))
  62.         }
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement