Advertisement
eveeeeef21

MainViewModel

Jan 19th, 2024
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.76 KB | Source Code | 0 0
  1. package com.ahmadrd.githubuser.viewmodel
  2.  
  3. import android.util.Log
  4. import androidx.lifecycle.LiveData
  5. import androidx.lifecycle.MutableLiveData
  6. import androidx.lifecycle.ViewModel
  7. import com.ahmadrd.githubuser.data.response.ItemsItem
  8. import com.ahmadrd.githubuser.data.response.ResponseUserGithub
  9. import com.ahmadrd.githubuser.data.retrofit.ApiConfig
  10. import retrofit2.Call
  11. import retrofit2.Callback
  12. import retrofit2.Response
  13.  
  14. class MainViewModel : ViewModel() {
  15.  
  16.     // Encapsulation Variable
  17.     private val _userList = MutableLiveData<List<ItemsItem>>()
  18.     val userList : LiveData<List<ItemsItem>> = _userList
  19.  
  20.     private val _isLoading = MutableLiveData<Boolean>()
  21.     val isLoading: LiveData<Boolean> = _isLoading
  22.  
  23.     private val _searchUser= MutableLiveData<ResponseUserGithub>()
  24.     val searchUser: LiveData<ResponseUserGithub> = _searchUser
  25.  
  26.     companion object{
  27.         private const val TAG = "MainViewModel"
  28. //        private const val SEARCH_USER = "Fikri"
  29.     }
  30.  
  31.     init {
  32.         getUser()
  33.     }
  34.  
  35.     fun getUser(){
  36.         _isLoading.value = true
  37.         val client = ApiConfig.getApiService().getUsers()
  38.         client.enqueue(object : Callback<List<ItemsItem>>{
  39.             override fun onResponse(call: Call<List<ItemsItem>>, response: Response<List<ItemsItem>>) {
  40.                 _isLoading.value = false
  41.                 if (response.isSuccessful) {
  42.                     val responseBody = response.body()
  43.                     if (responseBody != null) {
  44.                         _userList.postValue(responseBody)
  45.                     }
  46.                 }else {
  47.                     Log.e(TAG, "onFailure Pertama getUser: ${response.message()}")
  48.                 }
  49.             }
  50.  
  51.             override fun onFailure(call: Call<List<ItemsItem>>, t: Throwable) {
  52.                 _isLoading.value = false
  53.                 Log.e(TAG, "onFailure Kedua getUser: ${t.message}")
  54.             }
  55.         })
  56.     }
  57.  
  58.     fun getSearchUser(query: String) {
  59.         _isLoading.value = true
  60.         val client = ApiConfig.getApiService().searchUser(query)
  61.         client.enqueue(object : Callback<ResponseUserGithub>{
  62.             override fun onResponse(call: Call<ResponseUserGithub>, response: Response<ResponseUserGithub>) {
  63.                 _isLoading.value = false
  64.                 if (response.isSuccessful){
  65.                     _userList.value = response.body()?.items
  66.                 }else {
  67.                     Log.e(TAG, "onFailure Pertama getSearchUser: ${response.message()}")
  68.                 }
  69.             }
  70.  
  71.             override fun onFailure(call: Call<ResponseUserGithub>, t: Throwable) {
  72.                 _isLoading.value = false
  73.                 Log.e(TAG, "onFailure Kedua getSearchUser: ${t.message}")
  74.             }
  75.         })
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement