|
|
|
|
@ -0,0 +1,162 @@
|
|
|
|
|
// 负责人:李琦 | 模块: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
|
|
|
|
|
}
|
|
|
|
|
}
|