Skip to content

Commit

Permalink
fix: torrent playback error when bt-server is not installed (#82)
Browse files Browse the repository at this point in the history
* fix: torrent playback error when bt-server is not installed

* fix: error on Android
  • Loading branch information
MiaoMint authored Sep 26, 2023
1 parent ff15dfb commit 61e1b65
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 20 deletions.
24 changes: 12 additions & 12 deletions lib/pages/detail/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -165,7 +165,7 @@ class DetailPageController extends GetxController {
));
} else {
data = await fluent.showDialog(
context: cuurentContext,
context: currentContext,
builder: (context) => TMDBBinding(title: detail!.title),
);
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/extension_repo/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand Down
36 changes: 33 additions & 3 deletions lib/pages/watch/video_controller.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// ignore_for_file: use_build_context_synchronously

import 'dart:async';
import 'dart:convert';
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';
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';
Expand All @@ -23,6 +26,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;
import 'package:crypto/crypto.dart';

class VideoPlayerController extends GetxController {
Expand Down Expand Up @@ -66,6 +70,8 @@ class VideoPlayerController extends GetxController {

String _torrenHash = "";

// 复制当前 context

@override
void onInit() {
if (Platform.isAndroid) {
Expand Down Expand Up @@ -117,7 +123,7 @@ class VideoPlayerController extends GetxController {
Message(
Text(
FlutterI18n.translate(
cuurentContext,
currentContext,
"video.subtitle-change",
translationParams: {"title": value.files.first.name},
),
Expand All @@ -134,7 +140,7 @@ class VideoPlayerController extends GetxController {
Message(
Text(
FlutterI18n.translate(
cuurentContext,
currentContext,
"video.subtitle-change",
translationParams: {"title": subtitles[callback].title},
),
Expand Down Expand Up @@ -184,6 +190,11 @@ class VideoPlayerController extends GetxController {
}

play() async {
// 如果已经 delete 当前 controller
if (!Get.isRegistered<VideoPlayerController>(tag: title)) {
return;
}

try {
subtitles.clear();
selectedSubtitle.value = -1;
Expand Down Expand Up @@ -233,6 +244,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()),
Expand Down
2 changes: 1 addition & 1 deletion lib/router/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import 'package:miru_app/pages/settings/view.dart';
final rootNavigatorKey = GlobalKey<NavigatorState>();
final _shellNavigatorKey = GlobalKey<NavigatorState>();

BuildContext get cuurentContext {
BuildContext get currentContext {
if (Platform.isAndroid) {
return Get.context!;
}
Expand Down
11 changes: 10 additions & 1 deletion lib/utils/bt_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class BTServerUtils {
Get.find<BTDialogController>().isInstalled.value = false;
}
}
rethrow;
throw StartServerException('Start bt-server failed');
}
checkServer();
}
Expand Down Expand Up @@ -157,3 +157,12 @@ class BTServerUtils {
return "btserver";
}
}

class StartServerException implements Exception {
final String message;
StartServerException(this.message);
@override
String toString() {
return message;
}
}
4 changes: 2 additions & 2 deletions lib/utils/layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

// 是否是平板
Expand Down

0 comments on commit 61e1b65

Please sign in to comment.