package com.module.backstage.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.module.backstage.repo.ErrorStatFragmentRepo
import com.module.database.room.entity.WarringBean
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

/**
 * HomeFragment 的 VM 层
 *
 * @author Qu Yunshuo
 * @since 2021/8/5 11:11 下午
 */
@HiltViewModel//@HiltViewModel 注解的作用是将 ViewModel 标记为可以由 Hilt 管理的依赖项。当你将 @HiltViewModel 注解添加到 ViewModel 类上时，Hilt 会自动处理 ViewModel 的创建和注入过程。这样，你就不需要手动调用 ViewModelProvider 来获取 ViewModel 实例了。
class ErrorStatFragmentVM @Inject constructor(private val mRepo: ErrorStatFragmentRepo) : BaseViewModel() {
    private val TAG = "ErrorStatFragmentVM"
    private var _allWarringData = MutableLiveData<MutableList<WarringBean>>()
    val allWarringData: MutableLiveData<MutableList<WarringBean>> get() = _allWarringData

    /**
     * 获得所有的报警信息
     */
     fun getAllWarringData() {
         viewModelScope.launch {

             val warringBeans = mRepo.getAllWarringData()
             if (warringBeans.size>0){
                 withContext(Dispatchers.Main) {
                     allWarringData.value = warringBeans
                 }
             }
         }
    }

    /**
     * 清除所有报警
     */
    fun removeAllWarringData(){
        viewModelScope.launch {
            val removeAllWarringData = mRepo.removeAllWarringData()
            Log.d(TAG, "removeAllWarringData: "+removeAllWarringData)
        }
    }


}