package com.swago.home.innerhome

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.swago.baseswago.http.SwagoException
import com.swago.baseswago.inter.ApiManager
import com.swago.baseswago.model.ConfigModel
import com.swago.baseswago.model.MomentModel
import com.swago.baseswago.model.home.BannerModel
import com.swago.baseswago.util.BaseViewModel
import com.swago.baseswago.util.SwagoInfo
import kotlinx.coroutines.async

/**
 *@date 2021/11/28 10:35
 *description:
 */
class HomeVm(application: Application) : BaseViewModel(application) {

    val homeListLiveData by lazy {
        MutableLiveData<List<MomentModel>?>()
    }


    fun getHomeList(type: Int, page: Int) {
        requestData2 {
            requestData {
                val data = ApiManager.homeApi.getHomeList(type, page)
                homeListLiveData.value = data.list
            }

            requestError {
                if (it is SwagoException){
                    homeListLiveData.value = null
                }
            }
        }
    }


    val hotListLiveData by lazy {
        MutableLiveData<List<MomentModel>?>()
    }

    val bannerListLiveData by lazy {
        MutableLiveData<List<BannerModel>?>()
    }

    fun getHotList(type: Int, page: Int) {
        requestData2 {

            requestData {
                val data = ApiManager.homeApi.getHomeList(type, page)
                val dataBanner = ApiManager.homeApi.getHomeBanner()
                bannerListLiveData.value = dataBanner
                hotListLiveData.value = data.list

            }

            requestError {
                if (it is SwagoException){
                    hotListLiveData.value = null
                    bannerListLiveData.value = null
                }
            }
        }
    }

}