feat(app): 展示真实点赞与评论计数

This commit is contained in:
Zhou Weidong committed 2026-10-19 23:56:12 +08:00
1 parent 7b927c794e
commit dfc650456a
9 files changed
+123 -8

No files matched your search

@@ -36,12 +36,17 @@ class CommentController extends ChangeNotifier {
bool loadingMore = false;
bool submitting = false;
bool hasMore = false;
bool _loaded = false;
String? nextCursor;
String? error;
/// 当前已加载的顶层评论。
List<DramaComment> get comments => List.unmodifiable(_comments);
/// 全部分页加载完后返回精确的顶层评论数,否则返回 null。
int? get exactCount =>
_loaded && !loading && !loadingMore && !hasMore ? _comments.length : null;
/// 某一楼层已加载的回复。
List<DramaComment> repliesFor(String rootId) =>
List.unmodifiable(_replies[rootId] ?? const []);
@@ -77,6 +82,7 @@ class CommentController extends ChangeNotifier {
..addAll(page.comments);
nextCursor = page.nextCursor;
hasMore = page.hasMore;
_loaded = true;
} catch (failure) {
error = _errorMessage(failure) ?? 'commentsLoadFailed';
} finally {
@@ -8,14 +8,15 @@ import '../repositories/interaction_repository.dart';
/// 剧集点赞与举报的会话状态控制器。
///
/// 剧集契约暂未下发初始点赞状态与计数,本批只维护当前会话内的 liked 状态,
/// 不展示数字,也不伪造初始值。
/// 剧集契约暂未下发初始点赞状态与计数,因此仅在点赞接口成功返回后展示
/// 服务端最新计数,不伪造初始值。
class InteractionController extends ChangeNotifier {
InteractionController(this._repository);
final InteractionRepository _repository;
final Set<String> _likedDramaIds = {};
final Set<String> _busyDramaIds = {};
final Map<String, int> _likeCounts = {};
bool _disposed = false;
/// 当前会话内该剧是否已点赞。
@@ -24,6 +25,9 @@ class InteractionController extends ChangeNotifier {
/// 点赞请求是否进行中,用于防连点。
bool isBusy(String dramaId) => _busyDramaIds.contains(dramaId);
/// 服务端最新点赞数,仅在点赞接口成功返回后可用。
int? likeCount(String dramaId) => _likeCounts[dramaId];
/// 乐观切换剧集点赞,失败时回滚并以服务端返回状态校准。
Future<OperationResult<LikeToggleResult>> toggleDramaLike(
String dramaId,
@@ -49,6 +53,7 @@ class InteractionController extends ChangeNotifier {
} else {
_likedDramaIds.remove(dramaId);
}
_likeCounts[dramaId] = result.count;
return OperationResult.success(result);
} catch (failure) {
if (wasLiked) {
@@ -86,9 +91,14 @@ class InteractionController extends ChangeNotifier {
/// 登录账号变化时清理会话内状态,避免跨账号串联。
void clear() {
if (_likedDramaIds.isEmpty && _busyDramaIds.isEmpty) return;
if (_likedDramaIds.isEmpty &&
_busyDramaIds.isEmpty &&
_likeCounts.isEmpty) {
return;
}
_likedDramaIds.clear();
_busyDramaIds.clear();
_likeCounts.clear();
_notify();
}