package com.cheng.blzb.ui.fragment.push.subscribe.source

import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.SourceEntity
import com.cheng.blzb.net.ApiFactory
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.viewmodel.BaseViewModel

class SourceSearchViewModel: BaseViewModel() {
    val hotLiveData = MutableLiveData<List<SourceEntity>>()
    val searchLiveData = MutableLiveData<List<SourceEntity>>()

    fun getHotSources() {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getHotSources()
            if (response.status) {
                hotLiveData.postValue(response.data.items)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun search(keyword: String) {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.searchSource(keyword)
            if (response.status) {
                searchLiveData.postValue(response.data.items)
            } else toast(response.message, true)
        }, {
            setError(it)
            L.d(it)
        })
    }
}