49 lines
1.4 KiB
Kotlin
49 lines
1.4 KiB
Kotlin
|
|
package com.taskttl.data.viewmodel
|
||
|
|
|
||
|
|
import androidx.lifecycle.ViewModel
|
||
|
|
import androidx.lifecycle.viewModelScope
|
||
|
|
import com.taskttl.data.repository.CategoryRepository
|
||
|
|
import com.taskttl.data.repository.OnboardingRepository
|
||
|
|
import com.taskttl.data.state.OnboardingEvent
|
||
|
|
import kotlinx.coroutines.channels.Channel
|
||
|
|
import kotlinx.coroutines.flow.Flow
|
||
|
|
import kotlinx.coroutines.flow.receiveAsFlow
|
||
|
|
import kotlinx.coroutines.launch
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 入职视图模型
|
||
|
|
* @author admin
|
||
|
|
* @date 2025/10/05
|
||
|
|
* @constructor 创建[OnboardingViewModel]
|
||
|
|
* @param [onboardingRepository] 引导存储库
|
||
|
|
* @param [categoryRepository] 类别存储库
|
||
|
|
*/
|
||
|
|
class OnboardingViewModel(
|
||
|
|
private val onboardingRepository: OnboardingRepository,
|
||
|
|
private val categoryRepository: CategoryRepository
|
||
|
|
) : ViewModel() {
|
||
|
|
|
||
|
|
private val _events = Channel<OnboardingEvent>()
|
||
|
|
val events: Flow<OnboardingEvent> = _events.receiveAsFlow()
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 发送事件 - 提供统一的事件发送机制
|
||
|
|
* @param event 事件
|
||
|
|
*/
|
||
|
|
fun sendEvent(event: OnboardingEvent) {
|
||
|
|
viewModelScope.launch {
|
||
|
|
_events.trySend(event)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 标记引导完成
|
||
|
|
*/
|
||
|
|
fun markOnboardingCompleted() {
|
||
|
|
viewModelScope.launch {
|
||
|
|
categoryRepository.initializeDefaultCategories()
|
||
|
|
onboardingRepository.markLaunched()
|
||
|
|
_events.trySend(OnboardingEvent.NavMain)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|