fix(player): 收藏与评论数实时刷新

This commit is contained in:
Zhou Weidong committed 2026-10-21 11:39:58 +08:00
1 parent 97bbced02c
commit 5e2b8d9e4d
11 files changed
+275 -29

No files matched your search

+21 -2
View File
@@ -35,6 +35,7 @@ class DramaDetailLogic extends ChangeNotifier {
int? continueIndex;
WatchProgress? progress;
String? progressError;
int _favoriteCountDelta = 0;
bool _disposed = false;
void notify() {
@@ -118,8 +119,26 @@ class DramaDetailLogic extends ChangeNotifier {
await loadContinue();
}
/// 收藏切换:乐观更新失败时会回滚,由调用方给出提示。
Future<bool> toggleFavorite() => favorites.toggle(drama.id, card: drama);
/// 详情页展示用的收藏数;没有服务端计数时继续保持缺省展示。
int? get adjustedFavoriteCount {
final base = drama.favoriteCount;
if (base == null) return null;
final adjusted = base + _favoriteCountDelta;
return adjusted < 0 ? 0 : adjusted;
}
/// 收藏切换:乐观状态由控制器负责回滚,这里只在成功后同步计数。
Future<bool> toggleFavorite() async {
final wasSaved = favorites.isSaved(drama.id);
final ok = await favorites.toggle(drama.id, card: drama);
if (!ok) return false;
final nowSaved = favorites.isSaved(drama.id);
if (nowSaved != wasSaved) {
_favoriteCountDelta += nowSaved ? 1 : -1;
notify();
}
return true;
}
@override
void dispose() {