基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。
refactor: distinguish empty state from error and improve error propagation
Predidit23dce8af61ce
diff --git a/lib/pages/info/character_page.dart b/lib/pages/info/character_page.dartindex fceb620d..0f5cef3e 100644--- a/lib/pages/info/character_page.dart+++ b/lib/pages/info/character_page.dart@@ -21,6 +21,7 @@ class _CharacterPageState extends State<CharacterPage> {bool loadingCharacter = true;List<CharacterCommentItem> commentsList = [];bool loadingComments = true;+ bool commentsError = false;Future<void> loadCharacter() async {setState(() {@@ -40,11 +41,19 @@ class _CharacterPageState extends State<CharacterPage> {Future<void> loadComments() async {setState(() {loadingComments = true;+ commentsError = false;});- await BangumiHTTP.getCharacterCommentsByCharacterID(widget.characterID)- .then((value) {+ try {+ final value =+ await BangumiHTTP.getCharacterCommentsByCharacterID(widget.characterID);commentsList = value.commentList;- });+ } catch (e) {+ if (mounted) {+ setState(() {+ commentsError = true;+ });+ }+ }if (mounted) {setState(() {loadingComments = false;@@ -239,7 +248,7 @@ class _CharacterPageState extends State<CharacterPage> {),);}- if (commentsList.isEmpty) {+ if (commentsError) {return SliverFillRemaining(child: GeneralErrorWidget(errMsg: '什么都没有找到 (´;ω;`)',@@ -254,6 +263,13 @@ class _CharacterPageState extends State<CharacterPage> {),);}+ if (commentsList.isEmpty) {+ return const SliverFillRemaining(+ child: Center(+ child: Text('什么都没有找到 (´;ω;`)'),+ ),+ );+ }return SliverList(delegate: SliverChildBuilderDelegate((context, index) {diff --git a/lib/pages/info/info_page.dart b/lib/pages/info/info_page.dartindex 82d52f54..d2fb81c5 100644--- a/lib/pages/info/info_page.dart+++ b/lib/pages/info/info_page.dart@@ -42,9 +42,12 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {bool commentsIsLoading = false;bool charactersIsLoading = false;bool commentsQueryTimeout = false;+ bool commentsIsEmpty = false;bool charactersQueryTimeout = false;+ bool charactersIsEmpty = false;bool staffIsLoading = false;bool staffQueryTimeout = false;+ bool staffIsEmpty = false;final inputBangumiIten = Modular.args.data as BangumiItem;@@ -53,22 +56,28 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {setState(() {charactersIsLoading = true;charactersQueryTimeout = false;+ charactersIsEmpty = false;});- infoController- .queryBangumiCharactersByID(infoController.bangumiItem.id)- .then((_) {- if (infoController.characterList.isEmpty && mounted) {+ try {+ await infoController+ .queryBangumiCharactersByID(infoController.bangumiItem.id);+ if (mounted) {setState(() {charactersIsLoading = false;- charactersQueryTimeout = true;+ if (infoController.characterList.isEmpty) {+ charactersIsEmpty = true;+ }});}- if (infoController.characterList.isNotEmpty && mounted) {+ } catch (e) {+ KazumiLogger().e('InfoPage: failed to load characters', error: e);+ if (mounted) {setState(() {charactersIsLoading = false;+ charactersQueryTimeout = true;});}- });+ }}Future<void> loadStaff() async {@@ -76,22 +85,27 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {setState(() {staffIsLoading = true;staffQueryTimeout = false;+ staffIsEmpty = false;});- infoController- .queryBangumiStaffsByID(infoController.bangumiItem.id)- .then((_) {- if (infoController.staffList.isEmpty && mounted) {+ try {+ await infoController.queryBangumiStaffsByID(infoController.bangumiItem.id);+ if (mounted) {setState(() {staffIsLoading = false;- staffQueryTimeout = true;+ if (infoController.staffList.isEmpty) {+ staffIsEmpty = true;+ }});}- if (infoController.staffList.isNotEmpty && mounted) {+ } catch (e) {+ KazumiLogger().e('InfoPage: failed to load staff', error: e);+ if (mounted) {setState(() {staffIsLoading = false;+ staffQueryTimeout = true;});}- });+ }}Future<void> loadMoreComments({int offset = 0}) async {@@ -99,22 +113,29 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {setState(() {commentsIsLoading = true;commentsQueryTimeout = false;+ commentsIsEmpty = false;});- infoController- .queryBangumiCommentsByID(infoController.bangumiItem.id, offset: offset)- .then((_) {- if (infoController.commentsList.isEmpty && mounted) {+ try {+ await infoController.queryBangumiCommentsByID(+ infoController.bangumiItem.id,+ offset: offset);+ if (mounted) {setState(() {commentsIsLoading = false;- commentsQueryTimeout = true;+ if (infoController.commentsList.isEmpty) {+ commentsIsEmpty = true;+ }});}- if (infoController.commentsList.isNotEmpty && mounted) {+ } catch (e) {+ KazumiLogger().e('InfoPage: failed to load comments', error: e);+ if (mounted) {setState(() {commentsIsLoading = false;+ commentsQueryTimeout = true;});}- });+ }}@override@@ -141,15 +162,23 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {int index = infoTabController.index;if (index == 1 &&infoController.commentsList.isEmpty &&- !commentsIsLoading) {+ !commentsIsLoading &&+ !commentsIsEmpty &&+ !commentsQueryTimeout) {loadMoreComments();}if (index == 2 &&infoController.characterList.isEmpty &&- !charactersIsLoading) {+ !charactersIsLoading &&+ !charactersIsEmpty &&+ !charactersQueryTimeout) {loadCharacters();}- if (index == 4 && infoController.staffList.isEmpty && !staffIsLoading) {+ if (index == 4 &&+ infoController.staffList.isEmpty &&+ !staffIsLoading &&+ !staffIsEmpty &&+ !staffQueryTimeout) {loadStaff();}});@@ -343,8 +372,11 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {tabController: infoTabController,bangumiItem: infoController.bangumiItem,commentsQueryTimeout: commentsQueryTimeout,+ commentsIsEmpty: commentsIsEmpty,charactersQueryTimeout: charactersQueryTimeout,+ charactersIsEmpty: charactersIsEmpty,staffQueryTimeout: staffQueryTimeout,+ staffIsEmpty: staffIsEmpty,loadMoreComments: loadMoreComments,loadCharacters: loadCharacters,loadStaff: loadStaff,diff --git a/lib/pages/info/info_tabview.dart b/lib/pages/info/info_tabview.dartindex b7527f6e..f47ac398 100644--- a/lib/pages/info/info_tabview.dart+++ b/lib/pages/info/info_tabview.dart@@ -16,8 +16,11 @@ class InfoTabView extends StatefulWidget {const InfoTabView({super.key,required this.commentsQueryTimeout,+ required this.commentsIsEmpty,required this.charactersQueryTimeout,+ required this.charactersIsEmpty,required this.staffQueryTimeout,+ required this.staffIsEmpty,required this.tabController,required this.loadMoreComments,required this.loadCharacters,@@ -30,8 +33,11 @@ class InfoTabView extends StatefulWidget {});final bool commentsQueryTimeout;+ final bool commentsIsEmpty;final bool charactersQueryTimeout;+ final bool charactersIsEmpty;final bool staffQueryTimeout;+ final bool staffIsEmpty;final TabController tabController;final Future<void> Function({int offset}) loadMoreComments;final Future<void> Function() loadCharacters;@@ -280,6 +286,13 @@ class _InfoTabViewState extends State<InfoTabView>),);}+ if (widget.commentsIsEmpty) {+ return const SliverFillRemaining(+ child: Center(+ child: Text('什么都没有找到 (´;ω;`)'),+ ),+ );+ }return SliverList.builder(itemCount: 4,itemBuilder: (context, _) {@@ -356,6 +369,13 @@ class _InfoTabViewState extends State<InfoTabView>),);}+ if (widget.staffIsEmpty) {+ return const SliverFillRemaining(+ child: Center(+ child: Text('什么都没有找到 (´;ω;`)'),+ ),+ );+ }return SliverList.builder(itemCount: 8,itemBuilder: (context, _) {@@ -431,6 +451,13 @@ class _InfoTabViewState extends State<InfoTabView>),);}+ if (widget.charactersIsEmpty) {+ return const SliverFillRemaining(+ child: Center(+ child: Text('什么都没有找到 (´;ω;`)'),+ ),+ );+ }return SliverList.builder(itemCount: 4,itemBuilder: (context, _) {diff --git a/lib/pages/player/episode_comments_sheet.dart b/lib/pages/player/episode_comments_sheet.dartindex 92ad7f76..abf0b2b7 100644--- a/lib/pages/player/episode_comments_sheet.dart+++ b/lib/pages/player/episode_comments_sheet.dart@@ -5,6 +5,7 @@ import 'package:flutter_mobx/flutter_mobx.dart';import 'package:flutter_modular/flutter_modular.dart';import 'package:kazumi/bean/dialog/dialog_helper.dart';import 'package:kazumi/bean/card/episode_comments_card.dart';+import 'package:kazumi/bean/widget/error_widget.dart';import 'package:kazumi/pages/video/video_controller.dart';class EpisodeInfo extends InheritedWidget {@@ -33,6 +34,7 @@ class _EpisodeCommentsSheetState extends State<EpisodeCommentsSheet> {final VideoPageController videoPageController =Modular.get<VideoPageController>();bool commentsQueryTimeout = false;+ bool commentsIsEmpty = false;final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =GlobalKey<RefreshIndicatorState>();@@ -46,16 +48,22 @@ class _EpisodeCommentsSheetState extends State<EpisodeCommentsSheet> {Future<void> loadComments(int episode) async {commentsQueryTimeout = false;- await videoPageController- .queryBangumiEpisodeCommentsByID(- videoPageController.bangumiItem.id, episode)- .then((_) {+ commentsIsEmpty = false;+ try {+ await videoPageController.queryBangumiEpisodeCommentsByID(+ videoPageController.bangumiItem.id, episode);if (videoPageController.episodeCommentsList.isEmpty && mounted) {+ setState(() {+ commentsIsEmpty = true;+ });+ }+ } catch (e) {+ if (mounted) {setState(() {commentsQueryTimeout = true;});}- });+ }if (mounted) {setState(() {});}@@ -100,9 +108,24 @@ class _EpisodeCommentsSheetState extends State<EpisodeCommentsSheet> {padding: const EdgeInsets.fromLTRB(4, 0, 4, 4),sliver: Observer(builder: (context) {if (commentsQueryTimeout) {+ return SliverFillRemaining(+ child: GeneralErrorWidget(+ errMsg: '评论获取失败',+ actions: [+ GeneralErrorButton(+ onPressed: () {+ _refreshIndicatorKey.currentState?.show();+ },+ text: '重试',+ ),+ ],+ ),+ );+ }+ if (commentsIsEmpty) {return const SliverFillRemaining(child: Center(- child: Text('空空如也'),+ child: Text('什么都没有找到 (´;ω;`)'),),);}diff --git a/lib/pages/video/video_controller.dart b/lib/pages/video/video_controller.dartindex 21b41e2d..5078aa99 100644--- a/lib/pages/video/video_controller.dart+++ b/lib/pages/video/video_controller.dart@@ -340,10 +340,9 @@ abstract class _VideoPageController with Store {Future<void> queryBangumiEpisodeCommentsByID(int id, int episode) async {episodeCommentsList.clear();episodeInfo = await BangumiHTTP.getBangumiEpisodeByID(id, episode);- await BangumiHTTP.getBangumiCommentsByEpisodeID(episodeInfo.id)- .then((value) {- episodeCommentsList.addAll(value.commentList);- });+ final value =+ await BangumiHTTP.getBangumiCommentsByEpisodeID(episodeInfo.id);+ episodeCommentsList.addAll(value.commentList);if (!isCommentsAscending) {episodeCommentsList.sort((a, b) => b.comment.createdAt.compareTo(a.comment.createdAt));diff --git a/lib/request/bangumi.dart b/lib/request/bangumi.dartindex 5eed0468..63bce7f2 100644--- a/lib/request/bangumi.dart+++ b/lib/request/bangumi.dart@@ -237,83 +237,56 @@ class BangumiHTTP {static Future<CommentResponse> getBangumiCommentsByID(int id,{int offset = 0}) async {- CommentResponse commentResponse = CommentResponse.fromTemplate();- try {- final res = await Request().get(- Api.formatUrl(Api.bangumiAPINextDomain + Api.bangumiCommentsByIDNext,- [id, 20, offset]),- );- final jsonData = res.data;- commentResponse = CommentResponse.fromJson(jsonData);- } catch (e) {- KazumiLogger().e('Network: resolve bangumi comments failed', error: e);- }- return commentResponse;+ final res = await Request().get(+ Api.formatUrl(Api.bangumiAPINextDomain + Api.bangumiCommentsByIDNext,+ [id, 20, offset]),+ extra: {'customError': ''},+ shouldRethrow: true,+ );+ return CommentResponse.fromJson(res.data);}static Future<EpisodeCommentResponse> getBangumiCommentsByEpisodeID(int id) async {- EpisodeCommentResponse commentResponse =- EpisodeCommentResponse.fromTemplate();- try {- final res = await Request().get(- Api.formatUrl(- Api.bangumiAPINextDomain + Api.bangumiEpisodeCommentsByIDNext,- [id]),- );- final jsonData = res.data;- commentResponse = EpisodeCommentResponse.fromJson(jsonData);- } catch (e) {- KazumiLogger().e('Network: resolve bangumi episode comments failed', error: e);- }- return commentResponse;+ final res = await Request().get(+ Api.formatUrl(+ Api.bangumiAPINextDomain + Api.bangumiEpisodeCommentsByIDNext,+ [id]),+ extra: {'customError': ''},+ shouldRethrow: true,+ );+ return EpisodeCommentResponse.fromJson(res.data);}static Future<CharacterCommentResponse> getCharacterCommentsByCharacterID(int id) async {- CharacterCommentResponse commentResponse =- CharacterCommentResponse.fromTemplate();- try {- final res = await Request().get(- Api.formatUrl(- Api.bangumiAPINextDomain + Api.bangumiCharacterCommentsByIDNext,- [id]),- );- final jsonData = res.data;- commentResponse = CharacterCommentResponse.fromJson(jsonData);- } catch (e) {- KazumiLogger().e('Network: resolve bangumi character comments failed', error: e);- }- return commentResponse;+ final res = await Request().get(+ Api.formatUrl(+ Api.bangumiAPINextDomain + Api.bangumiCharacterCommentsByIDNext,+ [id]),+ extra: {'customError': ''},+ shouldRethrow: true,+ );+ return CharacterCommentResponse.fromJson(res.data);}static Future<StaffResponse> getBangumiStaffByID(int id) async {- StaffResponse staffResponse = StaffResponse.fromTemplate();- try {- final res = await Request().get(- Api.formatUrl(- Api.bangumiAPINextDomain + Api.bangumiStaffByIDNext, [id]),- );- final jsonData = res.data;- staffResponse = StaffResponse.fromJson(jsonData);- } catch (e) {- KazumiLogger().e('Network: resolve bangumi staff failed', error: e);- }- return staffResponse;+ final res = await Request().get(+ Api.formatUrl(+ Api.bangumiAPINextDomain + Api.bangumiStaffByIDNext, [id]),+ extra: {'customError': ''},+ shouldRethrow: true,+ );+ return StaffResponse.fromJson(res.data);}static Future<CharactersResponse> getCharatersByBangumiID(int id) async {- CharactersResponse charactersResponse = CharactersResponse.fromTemplate();- try {- final res = await Request().get(- Api.formatUrl(Api.bangumiAPIDomain + Api.bangumiCharacterByID, [id]),- );- final jsonData = res.data;- charactersResponse = CharactersResponse.fromJson(jsonData);- } catch (e) {- KazumiLogger().e('Network: resolve bangumi characters failed', error: e);- }- return charactersResponse;+ final res = await Request().get(+ Api.formatUrl(Api.bangumiAPIDomain + Api.bangumiCharacterByID, [id]),+ extra: {'customError': ''},+ shouldRethrow: true,+ );+ return CharactersResponse.fromJson(res.data);}static Future<CharacterFullItem> getCharacterByCharacterID(int id) async {