You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zero/app%2Fsrc%2Fmain%2Fjava%2Fc.../NoteViewModel.kt

163 lines
4.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 负责人:李琦 | 模块ViewModel层 | 功能:便签业务逻辑管理
// 创建时间2026年5月7日
package com.example.myapplication.viewmodel
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.example.myapplication.data.Note
import com.example.myapplication.data.NoteDatabase
import com.example.myapplication.data.NoteRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
/**
* 便签 ViewModel - 李琦负责
*
* 负责管理便签相关的业务逻辑和 UI 状态
* 作为 View 层和 Model 层之间的桥梁
*
* @param application 应用程序上下文
*/
class NoteViewModel(application: Application) : AndroidViewModel(application) {
// 数据仓库实例
private val repository: NoteRepository
// 所有便签列表的状态流
private val _allNotes = MutableStateFlow<List<Note>>(emptyList())
val allNotes: StateFlow<List<Note>> = _allNotes.asStateFlow()
// 当前编辑的便签
private val _currentNote = MutableStateFlow<Note?>(null)
val currentNote: StateFlow<Note?> = _currentNote.asStateFlow()
// 加载状态
private val _isLoading = MutableStateFlow(false)
val isLoading: StateFlow<Boolean> = _isLoading.asStateFlow()
init {
// 初始化数据仓库
val noteDao = NoteDatabase.getDatabase(application).noteDao()
repository = NoteRepository(noteDao)
// 开始监听数据库变化
observeNotes()
}
/**
* 监听便签数据变化
*
* 当数据库中的便签数据发生变化时,自动更新 StateFlow
*/
private fun observeNotes() {
viewModelScope.launch {
repository.getAllNotes().collect { notes ->
_allNotes.value = notes
}
}
}
/**
* 加载指定 ID 的便签
*
* @param noteId 便签 ID
*/
fun loadNote(noteId: Int) {
viewModelScope.launch {
repository.getNoteById(noteId).collect { note ->
_currentNote.value = note
}
}
}
/**
* 创建新便签
*
* @param title 便签标题
* @param content 便签内容
*/
fun createNote(title: String, content: String) {
viewModelScope.launch {
val note = Note(
title = title,
content = content,
createTime = System.currentTimeMillis(),
updateTime = System.currentTimeMillis()
)
repository.createNote(note)
}
}
/**
* 更新现有便签
*
* @param note 要更新的便签对象
*/
fun updateNote(note: Note) {
viewModelScope.launch {
val updatedNote = note.copy(updateTime = System.currentTimeMillis())
repository.updateNote(updatedNote)
}
}
/**
* 保存便签(创建或更新)
*
* @param title 便签标题
* @param content 便签内容
* @param noteId 如果是编辑现有便签,传入其 ID否则传 0 或 null
*/
fun saveNote(title: String, content: String, noteId: Int? = null) {
viewModelScope.launch {
_isLoading.value = true
try {
if (noteId != null && noteId > 0) {
// 更新现有便签
val existingNote = _currentNote.value
if (existingNote != null) {
updateNote(existingNote.copy(title = title, content = content))
}
} else {
// 创建新便签
createNote(title, content)
}
} finally {
_isLoading.value = false
}
}
}
/**
* 删除便签
*
* @param note 要删除的便签对象
*/
fun deleteNote(note: Note) {
viewModelScope.launch {
repository.deleteNote(note)
}
}
/**
* 根据 ID 删除便签
*
* @param noteId 要删除的便签 ID
*/
fun deleteNoteById(noteId: Int) {
viewModelScope.launch {
repository.deleteNoteById(noteId)
}
}
/**
* 清空当前编辑的便签
*/
fun clearCurrentNote() {
_currentNote.value = null
}
}