Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.resmana.mealapp.home
- import android.util.Log
- import androidx.lifecycle.LiveData
- import androidx.lifecycle.MutableLiveData
- import androidx.lifecycle.ViewModel
- import com.bumptech.glide.Glide
- import com.resmana.mealapp.data.MealResponse
- import com.resmana.mealapp.data.MealsItem
- import com.resmana.mealapp.data.api.ApiConfig
- import retrofit2.Call
- import retrofit2.Callback
- import retrofit2.Response
- class MainViewModel : ViewModel() {
- private val _listMeal = MutableLiveData<List<MealsItem>>()
- private val listMeal: LiveData<List<MealsItem>> = _listMeal
- private val _loading = MutableLiveData<Boolean>()
- val isLoading : LiveData<Boolean> = _loading
- fun getMeals() : LiveData<List<MealsItem>>{
- return listMeal
- }
- fun getDetailMeals(meal: String?) : LiveData<List<MealsItem>>{
- return listMeal
- }
- companion object {
- private const val TAG = "MainViewModel"
- }
- fun setMeals(meal: String) {
- _loading.value = true
- val client = ApiConfig.getApiService().getMeal(meal)
- client.enqueue(object : Callback<MealResponse> {
- override fun onResponse(call: Call<MealResponse>, response: Response<MealResponse>) {
- _loading.value = false
- if (response.isSuccessful) {
- _listMeal.value = response.body()?.meals
- } else {
- Log.e(TAG, "onFailure: ${response.message()}")
- }
- }
- override fun onFailure(call: Call<MealResponse>, t: Throwable) {
- _loading.value = false
- Log.e(TAG, "onFailure: ${t.message.toString()}")
- }
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement