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

import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.SubscriptionEntity
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 SubscribeViewModel: BaseViewModel() {
    val subscribeLiveData = MutableLiveData<List<SubscriptionEntity>>()
    val deleteLiveData = MutableLiveData<Any>()

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

    fun deleteSubscription(id: String) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.deleteSubscription(id)
            if (response.status) {
                deleteLiveData.postValue(Any())
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }
}