fix(app): 日记可见性修复 — 私密日记仅本地 + Web 端 ID 修复 + 分享按钮
问题修复: 1. Web端保存的日记看不到:createJournal 返回值未捕获,server ID 丢失导致 后续元素保存用错 ID。现在使用 saved.id 贯穿全部操作。 2. 管理端看不到新建日记:后端 list_journals 添加 is_private 过滤,admin/teacher 查看他人日记时排除私密日记。 3. RemoteJournalRepository 添加 onJournalChanged 变更通知流,HomeBloc 可自动刷新。 4. SyncEngine(native + web)enqueue 添加 is_private 防御性检查,私密日记不入队。 5. 编辑器 _persistState 条件入队:仅非私密日记同步到后端。 6. 分享流程改造:首次从私密变为公开时入队 create 操作上传。 7. 日记卡片添加可见性标签(仅自己可见/班级可见/公开),私密日记可点击分享。 8. 首页 _sharePrivateJournal 弹出 ShareBottomSheet 主动分享。
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
// 远程日记仓库 — 通过 API 客户端连接后端
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import '../models/journal_element.dart';
|
||||
import '../models/journal_entry.dart';
|
||||
import '../remote/api_client.dart';
|
||||
@@ -11,6 +13,10 @@ import 'journal_repository.dart';
|
||||
class RemoteJournalRepository implements JournalRepository {
|
||||
final ApiClient _api;
|
||||
|
||||
/// 变更通知流控制器 — 写操作成功后通知 UI 刷新
|
||||
final StreamController<void> _changeController =
|
||||
StreamController<void>.broadcast();
|
||||
|
||||
RemoteJournalRepository({required ApiClient api}) : _api = api;
|
||||
|
||||
@override
|
||||
@@ -78,7 +84,9 @@ class RemoteJournalRepository implements JournalRepository {
|
||||
json['date'] = entry.date.toIso8601String().substring(0, 10);
|
||||
final response = await _api.post('/diary/journals', data: json);
|
||||
final body = response.data as Map<String, dynamic>;
|
||||
return JournalEntry.fromJson(body['data'] as Map<String, dynamic>);
|
||||
final created = JournalEntry.fromJson(body['data'] as Map<String, dynamic>);
|
||||
_changeController.add(null); // 通知 UI 刷新列表
|
||||
return created;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -96,12 +104,14 @@ class RemoteJournalRepository implements JournalRepository {
|
||||
},
|
||||
);
|
||||
final body = response.data as Map<String, dynamic>;
|
||||
_changeController.add(null); // 通知 UI 刷新列表
|
||||
return JournalEntry.fromJson(body['data'] as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteJournal(String id) async {
|
||||
await _api.delete('/diary/journals/$id');
|
||||
_changeController.add(null); // 通知 UI 刷新列表
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -139,9 +149,9 @@ class RemoteJournalRepository implements JournalRepository {
|
||||
await _api.delete('/diary/elements/$elementId');
|
||||
}
|
||||
|
||||
/// 远程仓库不提供本地变更通知,返回空流
|
||||
/// 变更通知流 — 写操作后广播,供 HomeBloc 自动刷新
|
||||
@override
|
||||
Stream<void> get onJournalChanged => const Stream<void>.empty();
|
||||
Stream<void> get onJournalChanged => _changeController.stream;
|
||||
}
|
||||
|
||||
/// API 异常封装 — 后端返回非 2xx 状态码时抛出
|
||||
|
||||
@@ -143,7 +143,16 @@ class SyncEngine {
|
||||
/// update+update → update(使用最新数据)
|
||||
/// update+delete → delete(资源最终被删除)
|
||||
/// create+delete → 取消(资源从未存在)
|
||||
///
|
||||
/// 私密日记(is_private=true)不入队 — 仅保存在本地,不上传后端。
|
||||
void enqueue(PendingOperation operation) {
|
||||
// 防御性检查:私密日记不入队
|
||||
final isPrivate = operation.data['is_private'] as bool? ?? false;
|
||||
if (isPrivate) {
|
||||
debugPrint('SyncEngine.enqueue: 跳过私密日记 ${operation.id}');
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找队列中同一资源的最后一个操作
|
||||
PendingOperation? existing;
|
||||
for (final op in _pendingQueue) {
|
||||
|
||||
@@ -90,7 +90,15 @@ class SyncEngine {
|
||||
int get pendingCount => _pendingQueue.length;
|
||||
bool get isSyncing => _status == SyncStatus.syncing;
|
||||
|
||||
/// 入队待同步操作 — 私密日记(is_private=true)不入队
|
||||
void enqueue(PendingOperation operation) {
|
||||
// 防御性检查:私密日记仅保存在本地,不上传后端
|
||||
final isPrivate = operation.data['is_private'] as bool? ?? false;
|
||||
if (isPrivate) {
|
||||
debugPrint('SyncEngine.enqueue: 跳过私密日记 ${operation.id}');
|
||||
return;
|
||||
}
|
||||
|
||||
_pendingQueue.add(operation);
|
||||
if (_status == SyncStatus.idle) {
|
||||
_status = SyncStatus.paused;
|
||||
|
||||
Reference in New Issue
Block a user