From a6fbe678c17368a687d3f4acf78f9b72559af76c Mon Sep 17 00:00:00 2001 From: MiaoMint <1981324730@qq.com> Date: Tue, 26 Sep 2023 16:10:06 +0800 Subject: [PATCH 1/2] fix: torrent playback error when bt-server is not installed --- lib/pages/detail/controller.dart | 24 ++++++++--------- lib/pages/extension_repo/controller.dart | 2 +- lib/pages/watch/video_controller.dart | 34 ++++++++++++++++++++++-- lib/router/router.dart | 2 +- lib/utils/bt_server.dart | 11 +++++++- lib/utils/layout.dart | 4 +-- 6 files changed, 58 insertions(+), 19 deletions(-) diff --git a/lib/pages/detail/controller.dart b/lib/pages/detail/controller.dart index bd4fcb3b..cdcd194b 100644 --- a/lib/pages/detail/controller.dart +++ b/lib/pages/detail/controller.dart @@ -151,7 +151,7 @@ class DetailPageController extends GetxController { // 判断是否有 key if (MiruStorage.getSetting(SettingKey.tmdbKay) == "") { showPlatformSnackbar( - context: cuurentContext, + context: currentContext, content: 'detail.tmdb-key-missing'.i18n, severity: fluent.InfoBarSeverity.error, ); @@ -165,7 +165,7 @@ class DetailPageController extends GetxController { )); } else { data = await fluent.showDialog( - context: cuurentContext, + context: currentContext, builder: (context) => TMDBBinding(title: detail!.title), ); } @@ -198,21 +198,21 @@ class DetailPageController extends GetxController { // 弹出错误信息 if (runtime.value == null) { final content = FlutterI18n.translate( - cuurentContext, + currentContext, 'common.extension-missing', translationParams: { 'package': package, }, ); showPlatformSnackbar( - context: cuurentContext, + context: currentContext, content: content, severity: fluent.InfoBarSeverity.error, ); throw content; } else { showPlatformSnackbar( - context: cuurentContext, + context: currentContext, title: 'detail.get-lastest-data-error'.i18n, content: e.toString().split('\n')[0], severity: fluent.InfoBarSeverity.error, @@ -293,7 +293,7 @@ class DetailPageController extends GetxController { ); } catch (e) { showPlatformSnackbar( - context: cuurentContext, + context: currentContext, content: e.toString().split('\n')[0], severity: fluent.InfoBarSeverity.error, ); @@ -311,9 +311,9 @@ class DetailPageController extends GetxController { ) async { if (runtime.value == null) { showPlatformSnackbar( - context: cuurentContext, + context: currentContext, content: FlutterI18n.translate( - cuurentContext, + currentContext, 'common.extension-missing', translationParams: { 'package': package, @@ -329,9 +329,9 @@ class DetailPageController extends GetxController { if (player != 'built-in') { showPlatformSnackbar( - context: cuurentContext, + context: currentContext, content: FlutterI18n.translate( - cuurentContext, + currentContext, 'external-player-launching', translationParams: { 'player': player, @@ -344,7 +344,7 @@ class DetailPageController extends GetxController { as ExtensionBangumiWatch; } catch (e) { showPlatformSnackbar( - context: cuurentContext, + context: currentContext, content: e.toString().split('\n')[0], severity: fluent.InfoBarSeverity.error, ); @@ -359,7 +359,7 @@ class DetailPageController extends GetxController { return; } catch (e) { showPlatformSnackbar( - context: cuurentContext, + context: currentContext, content: e.toString().split('\n')[0], severity: fluent.InfoBarSeverity.error, ); diff --git a/lib/pages/extension_repo/controller.dart b/lib/pages/extension_repo/controller.dart index 74d23507..b8fff285 100644 --- a/lib/pages/extension_repo/controller.dart +++ b/lib/pages/extension_repo/controller.dart @@ -52,7 +52,7 @@ class ExtensionRepoPageController extends GetxController { if (Platform.isAndroid && extensions.isEmpty) { // ignore: use_build_context_synchronously showPlatformSnackbar( - context: cuurentContext, + context: currentContext, content: 'extension-repo.empty'.i18n, ); } diff --git a/lib/pages/watch/video_controller.dart b/lib/pages/watch/video_controller.dart index e5a68a6d..8a20434a 100644 --- a/lib/pages/watch/video_controller.dart +++ b/lib/pages/watch/video_controller.dart @@ -1,3 +1,5 @@ +// ignore_for_file: use_build_context_synchronously + import 'dart:async'; import 'dart:io'; @@ -11,6 +13,7 @@ import 'package:media_kit/media_kit.dart'; import 'package:media_kit_video/media_kit_video.dart'; import 'package:miru_app/api/bt_server.dart'; import 'package:miru_app/models/index.dart'; +import 'package:miru_app/pages/bt_dialog/view.dart'; import 'package:miru_app/pages/home/controller.dart'; import 'package:miru_app/pages/main/controller.dart'; import 'package:miru_app/router/router.dart'; @@ -22,6 +25,7 @@ import 'package:miru_app/utils/layout.dart'; import 'package:miru_app/utils/miru_directory.dart'; import 'package:window_manager/window_manager.dart'; import 'package:path/path.dart' as path; +import 'package:fluent_ui/fluent_ui.dart' as fluent; class VideoPlayerController extends GetxController { final String title; @@ -64,6 +68,8 @@ class VideoPlayerController extends GetxController { String _torrenHash = ""; + // 复制当前 context + @override void onInit() { if (Platform.isAndroid) { @@ -115,7 +121,7 @@ class VideoPlayerController extends GetxController { Message( Text( FlutterI18n.translate( - cuurentContext, + currentContext, "video.subtitle-change", translationParams: {"title": value.files.first.name}, ), @@ -132,7 +138,7 @@ class VideoPlayerController extends GetxController { Message( Text( FlutterI18n.translate( - cuurentContext, + currentContext, "video.subtitle-change", translationParams: {"title": subtitles[callback].title}, ), @@ -182,6 +188,11 @@ class VideoPlayerController extends GetxController { } play() async { + // 如果已经 delete 当前 controller + if (!Get.isRegistered(tag: title)) { + return; + } + try { subtitles.clear(); selectedSubtitle.value = -1; @@ -231,6 +242,25 @@ class VideoPlayerController extends GetxController { } subtitles.addAll(watchData.subtitles ?? []); } catch (e) { + if (e is StartServerException) { + if (Platform.isAndroid) { + await showDialog( + context: currentContext, + builder: (context) => const BTDialog(), + ); + } else { + await fluent.showDialog( + context: currentContext, + builder: (context) => const BTDialog(), + ); + } + + // 延时 3 秒再重试 + await Future.delayed(const Duration(seconds: 3)); + + play(); + return; + } sendMessage( Message( Text(e.toString()), diff --git a/lib/router/router.dart b/lib/router/router.dart index 15efa3ee..3f87192d 100644 --- a/lib/router/router.dart +++ b/lib/router/router.dart @@ -19,7 +19,7 @@ import 'package:miru_app/pages/settings/view.dart'; final rootNavigatorKey = GlobalKey(); final _shellNavigatorKey = GlobalKey(); -BuildContext get cuurentContext { +BuildContext get currentContext { if (Platform.isAndroid) { return Get.context!; } diff --git a/lib/utils/bt_server.dart b/lib/utils/bt_server.dart index d8e2b574..5e46f0df 100644 --- a/lib/utils/bt_server.dart +++ b/lib/utils/bt_server.dart @@ -97,7 +97,7 @@ class BTServerUtils { Get.find().isInstalled.value = false; } } - rethrow; + throw StartServerException('Start bt-server failed'); } checkServer(); } @@ -157,3 +157,12 @@ class BTServerUtils { return "btserver"; } } + +class StartServerException implements Exception { + final String message; + StartServerException(this.message); + @override + String toString() { + return message; + } +} diff --git a/lib/utils/layout.dart b/lib/utils/layout.dart index 13444230..c02e00aa 100644 --- a/lib/utils/layout.dart +++ b/lib/utils/layout.dart @@ -6,12 +6,12 @@ class LayoutUtils { // 获取当前宽度 static double get getWidth { - return MediaQuery.of(cuurentContext).size.width; + return MediaQuery.of(currentContext).size.width; } // 获取当前高度 static double get getHeight { - return MediaQuery.of(cuurentContext).size.height; + return MediaQuery.of(currentContext).size.height; } // 是否是平板 From ee5e7921f80498cdf8296c11ecfcea3e0311d6b3 Mon Sep 17 00:00:00 2001 From: MiaoMint <1981324730@qq.com> Date: Tue, 26 Sep 2023 16:28:52 +0800 Subject: [PATCH 2/2] fix: error on Android --- lib/pages/watch/video_controller.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pages/watch/video_controller.dart b/lib/pages/watch/video_controller.dart index 8a20434a..0c3951b7 100644 --- a/lib/pages/watch/video_controller.dart +++ b/lib/pages/watch/video_controller.dart @@ -3,9 +3,9 @@ import 'dart:async'; import 'dart:io'; +import 'package:flutter/material.dart'; import 'package:dio/dio.dart'; import 'package:file_picker/file_picker.dart'; -import 'package:fluent_ui/fluent_ui.dart'; import 'package:flutter/services.dart'; import 'package:flutter_i18n/flutter_i18n.dart'; import 'package:get/get.dart';