Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

fix: add episode comments loading guard (#2038)

Predidit9df49bf74777
diff --git a/lib/pages/player/episode_comments_sheet.dart b/lib/pages/player/episode_comments_sheet.dart
index c4d908af..fe5dc794 100644
--- a/lib/pages/player/episode_comments_sheet.dart
+++ b/lib/pages/player/episode_comments_sheet.dart
@@ -39,6 +39,7 @@ class _EpisodeCommentsSheetState extends State<EpisodeCommentsSheet> {
bool commentsIsEmpty = false;
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
+ int _loadCommentsRequestId = 0;
/// episode input by [showEpisodeSelection]
int ep = 0;
@@ -49,24 +50,28 @@ class _EpisodeCommentsSheetState extends State<EpisodeCommentsSheet> {
}
Future<void> loadComments(int episode) async {
+ final int requestId = ++_loadCommentsRequestId;
commentsQueryTimeout = false;
commentsIsEmpty = false;
try {
await videoPageController.queryBangumiEpisodeCommentsByID(
videoPageController.bangumiItem.id, episode);
+ if (!mounted || requestId != _loadCommentsRequestId) {
+ return;
+ }
if (videoPageController.episodeCommentsList.isEmpty && mounted) {
setState(() {
commentsIsEmpty = true;
});
}
} catch (e) {
- if (mounted) {
+ if (mounted && requestId == _loadCommentsRequestId) {
setState(() {
commentsQueryTimeout = true;
});
}
}
- if (mounted) {
+ if (mounted && requestId == _loadCommentsRequestId) {
setState(() {});
}
}
diff --git a/lib/pages/video/video_controller.dart b/lib/pages/video/video_controller.dart
index 5078aa99..82a3c73c 100644
--- a/lib/pages/video/video_controller.dart
+++ b/lib/pages/video/video_controller.dart
@@ -53,6 +53,8 @@ abstract class _VideoPageController with Store {
@observable
bool isCommentsAscending = false;
+ int _episodeCommentsRequestId = 0;
+
/// 桌面画中画状态,Android 画中画状态不需要单独维护,进入画中画后会直接切换到系统的全局播放器界面
@observable
bool isPip = false;
@@ -247,7 +249,8 @@ abstract class _VideoPageController with Store {
referer: '',
currentRoad: currentRoad,
coverUrl: bangumiItem.images['large'],
- bangumiName: bangumiItem.nameCn.isNotEmpty ? bangumiItem.nameCn : bangumiItem.name,
+ bangumiName:
+ bangumiItem.nameCn.isNotEmpty ? bangumiItem.nameCn : bangumiItem.name,
);
final playerController = Modular.get<PlayerController>();
@@ -309,7 +312,9 @@ abstract class _VideoPageController with Store {
referer: currentPlugin.referer,
currentRoad: currentRoad,
coverUrl: bangumiItem.images['large'],
- bangumiName: bangumiItem.nameCn.isNotEmpty ? bangumiItem.nameCn : bangumiItem.name,
+ bangumiName: bangumiItem.nameCn.isNotEmpty
+ ? bangumiItem.nameCn
+ : bangumiItem.name,
);
final playerController = Modular.get<PlayerController>();
@@ -338,18 +343,24 @@ abstract class _VideoPageController with Store {
}
Future<void> queryBangumiEpisodeCommentsByID(int id, int episode) async {
- episodeCommentsList.clear();
- episodeInfo = await BangumiHTTP.getBangumiEpisodeByID(id, episode);
+ final int requestId = ++_episodeCommentsRequestId;
+ final EpisodeInfo latestEpisodeInfo =
+ await BangumiHTTP.getBangumiEpisodeByID(id, episode);
final value =
- await BangumiHTTP.getBangumiCommentsByEpisodeID(episodeInfo.id);
- episodeCommentsList.addAll(value.commentList);
+ await BangumiHTTP.getBangumiCommentsByEpisodeID(latestEpisodeInfo.id);
+ if (requestId != _episodeCommentsRequestId) {
+ return;
+ }
+ episodeInfo = latestEpisodeInfo;
+ final commentsList = value.commentList;
if (!isCommentsAscending) {
- episodeCommentsList
+ commentsList
.sort((a, b) => b.comment.createdAt.compareTo(a.comment.createdAt));
} else {
- episodeCommentsList
+ commentsList
.sort((a, b) => a.comment.createdAt.compareTo(b.comment.createdAt));
}
+ episodeCommentsList = ObservableList.of(commentsList);
KazumiLogger().i(
'VideoPageController: loaded comments list length ${episodeCommentsList.length}');
}