feat(player): 点赞与评论改为按集
This commit is contained in:
1 parent
e6f34977f5
commit
83822a0cbb
25 files changed
+337
-69
No files matched your search
@@ -75,6 +75,7 @@ class CommentController extends ChangeNotifier {
|
||||
try {
|
||||
final page = await _commentRepository.page(
|
||||
dramaId: dramaId,
|
||||
episodeId: episodeId,
|
||||
size: commentPageSize,
|
||||
);
|
||||
_comments
|
||||
@@ -101,6 +102,7 @@ class CommentController extends ChangeNotifier {
|
||||
try {
|
||||
final page = await _commentRepository.page(
|
||||
dramaId: dramaId,
|
||||
episodeId: episodeId,
|
||||
cursor: cursor,
|
||||
size: commentPageSize,
|
||||
);
|
||||
@@ -128,6 +130,7 @@ class CommentController extends ChangeNotifier {
|
||||
try {
|
||||
final page = await _commentRepository.page(
|
||||
dramaId: dramaId,
|
||||
episodeId: episodeId,
|
||||
rootId: rootId,
|
||||
size: commentPageSize,
|
||||
);
|
||||
@@ -157,6 +160,7 @@ class CommentController extends ChangeNotifier {
|
||||
try {
|
||||
final page = await _commentRepository.page(
|
||||
dramaId: dramaId,
|
||||
episodeId: episodeId,
|
||||
rootId: rootId,
|
||||
cursor: cursor,
|
||||
size: commentPageSize,
|
||||
|
||||
@@ -17,6 +17,9 @@ class InteractionController extends ChangeNotifier {
|
||||
final Set<String> _likedDramaIds = {};
|
||||
final Set<String> _busyDramaIds = {};
|
||||
final Map<String, int> _likeCounts = {};
|
||||
final Set<String> _likedEpisodeIds = {};
|
||||
final Set<String> _busyEpisodeIds = {};
|
||||
final Map<String, int> _episodeLikeCounts = {};
|
||||
bool _disposed = false;
|
||||
|
||||
/// 当前会话内该剧是否已点赞。
|
||||
@@ -28,42 +31,75 @@ class InteractionController extends ChangeNotifier {
|
||||
/// 服务端最新点赞数,仅在点赞接口成功返回后可用。
|
||||
int? likeCount(String dramaId) => _likeCounts[dramaId];
|
||||
|
||||
/// 当前会话内该集是否已点赞。
|
||||
bool isEpisodeLiked(String episodeId) => _likedEpisodeIds.contains(episodeId);
|
||||
|
||||
/// 该集点赞请求是否进行中。
|
||||
bool isEpisodeBusy(String episodeId) => _busyEpisodeIds.contains(episodeId);
|
||||
|
||||
/// 该集服务端最新点赞数。
|
||||
int? episodeLikeCount(String episodeId) => _episodeLikeCounts[episodeId];
|
||||
|
||||
/// 乐观切换剧集点赞,失败时回滚并以服务端返回状态校准。
|
||||
Future<OperationResult<LikeToggleResult>> toggleDramaLike(
|
||||
String dramaId,
|
||||
) async {
|
||||
if (_busyDramaIds.contains(dramaId)) {
|
||||
Future<OperationResult<LikeToggleResult>> toggleDramaLike(String dramaId) =>
|
||||
_toggleLike(
|
||||
id: dramaId,
|
||||
targetType: InteractionTargetType.drama,
|
||||
likedIds: _likedDramaIds,
|
||||
busyIds: _busyDramaIds,
|
||||
counts: _likeCounts,
|
||||
);
|
||||
|
||||
/// 乐观切换分集点赞,失败时回滚并以服务端返回状态校准。
|
||||
Future<OperationResult<LikeToggleResult>> toggleEpisodeLike(
|
||||
String episodeId,
|
||||
) => _toggleLike(
|
||||
id: episodeId,
|
||||
targetType: InteractionTargetType.episode,
|
||||
likedIds: _likedEpisodeIds,
|
||||
busyIds: _busyEpisodeIds,
|
||||
counts: _episodeLikeCounts,
|
||||
);
|
||||
|
||||
Future<OperationResult<LikeToggleResult>> _toggleLike({
|
||||
required String id,
|
||||
required InteractionTargetType targetType,
|
||||
required Set<String> likedIds,
|
||||
required Set<String> busyIds,
|
||||
required Map<String, int> counts,
|
||||
}) async {
|
||||
if (busyIds.contains(id)) {
|
||||
return const OperationResult.failure(null);
|
||||
}
|
||||
final wasLiked = _likedDramaIds.contains(dramaId);
|
||||
_busyDramaIds.add(dramaId);
|
||||
final wasLiked = likedIds.contains(id);
|
||||
busyIds.add(id);
|
||||
if (wasLiked) {
|
||||
_likedDramaIds.remove(dramaId);
|
||||
likedIds.remove(id);
|
||||
} else {
|
||||
_likedDramaIds.add(dramaId);
|
||||
likedIds.add(id);
|
||||
}
|
||||
_notify();
|
||||
try {
|
||||
final result = await _repository.toggleLike(
|
||||
targetType: InteractionTargetType.drama,
|
||||
targetId: dramaId,
|
||||
targetType: targetType,
|
||||
targetId: id,
|
||||
);
|
||||
if (result.liked) {
|
||||
_likedDramaIds.add(dramaId);
|
||||
likedIds.add(id);
|
||||
} else {
|
||||
_likedDramaIds.remove(dramaId);
|
||||
likedIds.remove(id);
|
||||
}
|
||||
_likeCounts[dramaId] = result.count;
|
||||
counts[id] = result.count;
|
||||
return OperationResult.success(result);
|
||||
} catch (failure) {
|
||||
if (wasLiked) {
|
||||
_likedDramaIds.add(dramaId);
|
||||
likedIds.add(id);
|
||||
} else {
|
||||
_likedDramaIds.remove(dramaId);
|
||||
likedIds.remove(id);
|
||||
}
|
||||
return OperationResult.failure(_errorMessage(failure));
|
||||
} finally {
|
||||
_busyDramaIds.remove(dramaId);
|
||||
busyIds.remove(id);
|
||||
_notify();
|
||||
}
|
||||
}
|
||||
@@ -93,12 +129,18 @@ class InteractionController extends ChangeNotifier {
|
||||
void clear() {
|
||||
if (_likedDramaIds.isEmpty &&
|
||||
_busyDramaIds.isEmpty &&
|
||||
_likeCounts.isEmpty) {
|
||||
_likeCounts.isEmpty &&
|
||||
_likedEpisodeIds.isEmpty &&
|
||||
_busyEpisodeIds.isEmpty &&
|
||||
_episodeLikeCounts.isEmpty) {
|
||||
return;
|
||||
}
|
||||
_likedDramaIds.clear();
|
||||
_busyDramaIds.clear();
|
||||
_likeCounts.clear();
|
||||
_likedEpisodeIds.clear();
|
||||
_busyEpisodeIds.clear();
|
||||
_episodeLikeCounts.clear();
|
||||
_notify();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user