fix(app): 日记保存逻辑修复 — EditorPage 改为 StatefulWidget + 更新合并编辑器状态
根因分析: 1. EditorPage 是 StatelessWidget,savedJournalId 作为 build() 局部变量 每次重建都重置为 null,导致每次自动保存都走新建而非更新分支 2. 更新分支直接 repo.updateJournal(existing),没有把编辑器当前 状态(标题/心情/标签)合并到已有日记中 修复: - EditorPage 改为 StatefulWidget,_savedJournalId 存储在 State 中 - 更新分支用 existing.copyWith() 合并编辑器当前状态后保存
This commit is contained in:
@@ -37,12 +37,26 @@ import '../widgets/brush_panel.dart';
|
||||
import '../widgets/dot_grid_painter.dart';
|
||||
|
||||
/// 手账编辑器页面
|
||||
class EditorPage extends StatelessWidget {
|
||||
class EditorPage extends StatefulWidget {
|
||||
final String? journalId;
|
||||
final String? templateId;
|
||||
|
||||
const EditorPage({super.key, this.journalId, this.templateId});
|
||||
|
||||
@override
|
||||
State<EditorPage> createState() => _EditorPageState();
|
||||
}
|
||||
|
||||
class _EditorPageState extends State<EditorPage> {
|
||||
/// 跟踪已保存的日记 ID — 新建日记首次保存后赋值
|
||||
String? _savedJournalId;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_savedJournalId = widget.journalId;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// 从 Provider 树获取 JournalRepository(IsarJournalRepository)
|
||||
@@ -50,10 +64,6 @@ class EditorPage extends StatelessWidget {
|
||||
// 从 Provider 树获取 SyncEngine(同步到后端)
|
||||
final syncEngine = context.read<SyncEngine>();
|
||||
|
||||
// 可变闭包变量:跟踪已保存的日记 ID
|
||||
// 新建日记首次保存后赋值,后续自动更新使用此 ID
|
||||
String? savedJournalId = journalId;
|
||||
|
||||
return BlocProvider(
|
||||
create: (_) => EditorBloc(
|
||||
onSave: (state) async {
|
||||
@@ -66,7 +76,7 @@ class EditorPage extends StatelessWidget {
|
||||
}
|
||||
|
||||
await _persistState(
|
||||
repo, state, (id) => savedJournalId = id, savedJournalId,
|
||||
repo, state, (id) => _savedJournalId = id, _savedJournalId,
|
||||
syncEngine: syncEngine,
|
||||
authorId: authorId,
|
||||
);
|
||||
@@ -76,12 +86,12 @@ class EditorPage extends StatelessWidget {
|
||||
},
|
||||
),
|
||||
child: _EditorView(
|
||||
journalId: journalId,
|
||||
templateId: templateId,
|
||||
savedJournalId: savedJournalId,
|
||||
journalId: widget.journalId,
|
||||
templateId: widget.templateId,
|
||||
savedJournalId: _savedJournalId,
|
||||
repo: repo,
|
||||
onSaveComplete: () {
|
||||
_showShareSheetAndNavigate(context, repo, savedJournalId);
|
||||
_showShareSheetAndNavigate(context, repo, _savedJournalId);
|
||||
},
|
||||
),
|
||||
);
|
||||
@@ -137,7 +147,14 @@ class EditorPage extends StatelessWidget {
|
||||
// --- 更新已有日记 ---
|
||||
final existing = await repo.getJournal(savedJournalId);
|
||||
if (existing != null) {
|
||||
await repo.updateJournal(existing);
|
||||
// 将编辑器当前状态合并到已有日记中
|
||||
final updated = existing.copyWith(
|
||||
title: state.title.isNotEmpty ? state.title : existing.title,
|
||||
mood: state.selectedMood,
|
||||
tags: state.tags.isNotEmpty ? state.tags : existing.tags,
|
||||
updatedAt: now,
|
||||
);
|
||||
await repo.updateJournal(updated);
|
||||
|
||||
// 入队 SyncEngine 等待同步到后端
|
||||
syncEngine.enqueue(PendingOperation(
|
||||
|
||||
Reference in New Issue
Block a user