package com.img.rabbit.viewmodel
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

open class BaseViewModel : ViewModel() {
    var isShowMsg = mutableStateOf(false)
    var msgContent = mutableStateOf("")
    val isLoading = mutableStateOf(false)

    fun mLaunch(block: suspend () -> Unit) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                block()
            } catch (e: Exception) {
                withContext(Dispatchers.Main){
                    isShowMsg.value = true
                    msgContent.value = "接口请求失败"
                }
            }
        }
    }
}