Advertisement
uscode

Untitled

Jan 31st, 2021
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.71 KB | None | 0 0
  1. package com.resmana.mealapp.home
  2.  
  3. import android.util.Log
  4. import androidx.lifecycle.LiveData
  5. import androidx.lifecycle.MutableLiveData
  6. import androidx.lifecycle.ViewModel
  7. import com.bumptech.glide.Glide
  8. import com.resmana.mealapp.data.MealResponse
  9. import com.resmana.mealapp.data.MealsItem
  10. import com.resmana.mealapp.data.api.ApiConfig
  11. import retrofit2.Call
  12. import retrofit2.Callback
  13. import retrofit2.Response
  14.  
  15. class MainViewModel : ViewModel() {
  16.  
  17.     private val _listMeal = MutableLiveData<List<MealsItem>>()
  18.     private val listMeal: LiveData<List<MealsItem>> = _listMeal
  19.  
  20.     private val _loading = MutableLiveData<Boolean>()
  21.     val isLoading : LiveData<Boolean> = _loading
  22.  
  23.     fun getMeals() : LiveData<List<MealsItem>>{
  24.         return listMeal
  25.     }
  26.  
  27.     fun getDetailMeals(meal: String?) : LiveData<List<MealsItem>>{
  28.         return listMeal
  29.     }
  30.  
  31.     companion object {
  32.         private const val TAG = "MainViewModel"
  33.     }
  34.  
  35.     fun setMeals(meal: String) {
  36.         _loading.value = true
  37.         val client = ApiConfig.getApiService().getMeal(meal)
  38.         client.enqueue(object : Callback<MealResponse> {
  39.             override fun onResponse(call: Call<MealResponse>, response: Response<MealResponse>) {
  40.                 _loading.value = false
  41.                 if (response.isSuccessful) {
  42.                     _listMeal.value = response.body()?.meals
  43.                 } else {
  44.                     Log.e(TAG, "onFailure: ${response.message()}")
  45.                 }
  46.             }
  47.  
  48.             override fun onFailure(call: Call<MealResponse>, t: Throwable) {
  49.                _loading.value = false
  50.                 Log.e(TAG, "onFailure: ${t.message.toString()}")
  51.             }
  52.         })
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement