package com.ys.bdtp.adm.mvvm.vm.task

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ys.bdtp.adm.mvvm.model.Data
import com.ys.bdtp.adm.mvvm.model.db.entity.task.ProjectEntity
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn

/**
 * TaskViewModel
 */
class TaskViewModel(private val repo: TaskRepository) : ViewModel() {
    /**
     * 获取项目实例
     *
     * @author bai
     * @param id 项目id
     */
    suspend fun getProject(id: String): ProjectEntity {
        return repo.getProject(id)
    }

    /**
     * 带状态 User Flow
     */
    val userStateFlow = repo.getUser()
        .stateIn(
            scope = viewModelScope,
            started = SharingStarted.WhileSubscribed(5000),
            initialValue = Data.loading()
        )

    suspend fun logout() {
        repo.logout()
    }
}