Skip to content

Commit

Permalink
feat(bt-server): uninstall & version manager (#62)
Browse files Browse the repository at this point in the history
* feat(bt-server): uninstall & version manager

* fix: `bt-server` manager display overflow

* fix(i18n): `settings.bt-server-subtitle`

* fix(bt-server): version check error
  • Loading branch information
MiaoMint authored Sep 19, 2023
1 parent 7687260 commit 51df272
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 106 deletions.
5 changes: 4 additions & 1 deletion assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"tmdb-key": "TMDB API Key",
"tmdb-key-subtitle": "Get API Key for TMDB metadata",
"bt-server": "BT Server",
"bt-server-subtitle": "BT Server is a necessary component for online playback of BT seeds",
"bt-server-subtitle": "BT Server is a necessary component for online playback of torrent",
"bt-server-manager": "Manager",
"upgrade": "Update Software",
"upgrade-subtitle": "version: {version}",
Expand Down Expand Up @@ -202,6 +202,9 @@
"running": "BT-Server is running",
"stopped": "BT-Server is stopped",
"version": "Version {version}",
"remote-version": "Remote Version {version}",
"stop": "Stop",
"upgrade": "Upgrade",
"start": "Start"
}
}
3 changes: 3 additions & 0 deletions assets/i18n/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@
"running": "BT-Server 正在运行",
"stopped": "BT-Server 已停止",
"version": "版本 {version}",
"remote-version": "远程版本 {version}",
"stop": "停止",
"upgrade": "升级",
"start": "启动"
}
}
15 changes: 14 additions & 1 deletion lib/pages/bt_dialog/controller.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:flutter/scheduler.dart';
import 'package:get/get.dart';
import 'package:miru_app/pages/main/controller.dart';
Expand All @@ -9,6 +10,8 @@ class BTDialogController extends GetxController {
final isInstalled = false.obs;
final isDownloading = false.obs;
final progress = 0.0.obs;
final hasUpdate = false.obs;
final remoteVersion = "".obs;

final _mainController = Get.find<MainController>();
late final isRuning = _mainController.btServerisRunning;
Expand All @@ -17,25 +20,35 @@ class BTDialogController extends GetxController {
@override
void onInit() {
super.onInit();
ever(
isRuning,
(callback) {
hasUpdate.value = version.value != remoteVersion.value;
},
);
SchedulerBinding.instance.addPostFrameCallback((_) async {
isInstalled.value = await BTServerUtils.isInstalled();
remoteVersion.value = await BTServerUtils.getRemoteVersion();
});
}

downloadOrUpgradeServer(BuildContext context) async {
progress.value = 0;
BTServerUtils.stopServer();
isInstalled.value = false;
isDownloading.value = true;
try {
await BTServerUtils.downloadLatestBTServer(
onReceiveProgress: (p0, p1) {
progress.value = p0 / p1;
print(progress.value);
},
);
} catch (e) {
context.mounted &&
showPlatformSnackbar(
context: context,
content: e.toString(),
severity: fluent.InfoBarSeverity.error,
);
} finally {
isDownloading.value = false;
Expand Down
83 changes: 69 additions & 14 deletions lib/pages/bt_dialog/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,77 @@ class _BTDialogState extends State<BTDialog> {
Text("bt-server.stopped".i18n),
const SizedBox(height: 16),
if (c.isRuning.value)
Text(
FlutterI18n.translate(
context,
'bt-server.version',
translationParams: {
"version": c.version.value,
},
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Row(
children: [
Text(
FlutterI18n.translate(
context,
'bt-server.version',
translationParams: {
"version": c.version.value,
},
),
),
const SizedBox(width: 8),
if (c.hasUpdate.value)
Text(
FlutterI18n.translate(
context,
'bt-server.remote-version',
translationParams: {
"version": c.remoteVersion.value,
},
),
),
],
),
)
else
PlatformFilledButton(
child: Text("bt-server.start".i18n),
onPressed: () {
BTServerUtils.startServer();
},
),
Wrap(
children: [
if (c.isRuning.value) ...[
Padding(
padding: const EdgeInsets.only(right: 8),
child: PlatformFilledButton(
child: Text('bt-server.stop'.i18n),
onPressed: () {
BTServerUtils.stopServer();
},
),
),
// 升级按钮
if (c.hasUpdate.value)
Padding(
padding: const EdgeInsets.only(right: 8),
child: PlatformFilledButton(
child: Text("bt-server.upgrade".i18n),
onPressed: () {
c.downloadOrUpgradeServer(context);
},
),
),
] else
Padding(
padding: const EdgeInsets.only(right: 8),
child: PlatformFilledButton(
child: Text("bt-server.start".i18n),
onPressed: () {
BTServerUtils.startServer();
},
),
),
if (c.isInstalled.value)
// 卸载
PlatformFilledButton(
child: Text("common.uninstall".i18n),
onPressed: () async {
await BTServerUtils.uninstall();
c.isInstalled.value = false;
},
),
],
),
],
);
});
Expand Down
37 changes: 29 additions & 8 deletions lib/utils/bt_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ class BTServerUtils {
final res = dio.get(url);
final remoteVersion = (await res).data["tag_name"] as String;
debugPrint("最新版本: $remoteVersion");
late String structure;
late String arch;
late String platform;
if (Platform.isAndroid) {
final supportedAbis = androidDeviceInfo.supportedAbis;
if (supportedAbis.contains("armeabi-v7a")) {
structure = "arm";
arch = "arm";
}
if (supportedAbis.contains("x86_64")) {
structure = "amd64";
arch = "amd64";
}
if (supportedAbis.contains("arm64-v8a")) {
structure = "arm64";
arch = "arm64";
}
platform = "android";
}
if (Platform.isWindows) {
structure = "amd64.exe";
arch = "amd64.exe";
platform = "windows";
}

debugPrint("下载 bt-server $remoteVersion $platform $structure");
debugPrint("下载 bt-server $remoteVersion $platform $arch");

final downloadUrl =
"https://github.com/miru-project/bt-server/releases/download/$remoteVersion/bt-server-$remoteVersion-$platform-$structure";
"https://github.com/miru-project/bt-server/releases/download/$remoteVersion/bt-server-$remoteVersion-$platform-$arch";

final savePath = await MiruDirectory.getDirectory;
await dio.download(
Expand All @@ -75,7 +75,7 @@ class BTServerUtils {

try {
if (Platform.isWindows) {
await Process.run(
_process = await Process.start(
btServerPath,
[],
workingDirectory: savePath,
Expand Down Expand Up @@ -122,6 +122,27 @@ class BTServerUtils {
});
}

// 检查更新
static Future<String> getRemoteVersion() async {
try {
const url =
"https://api.github.com/repos/miru-project/bt-server/releases/latest";
final res = Dio().get(url);
final remoteVersion = (await res).data["tag_name"] as String;
return remoteVersion.replaceFirst("v", '');
} catch (e) {
return Get.find<MainController>().btServerVersion.value;
}
}

// 卸载 bt-server
static Future<void> uninstall() async {
stopServer();
final savePath = await MiruDirectory.getDirectory;
final btServerPath = path.join(savePath, _getBTServerFilename());
await File(btServerPath).delete();
}

static Future<bool> isInstalled() async {
final savePath = await MiruDirectory.getDirectory;
final btServerPath = path.join(savePath, _getBTServerFilename());
Expand Down
Loading

0 comments on commit 51df272

Please sign in to comment.