Skip to content

Commit 51df272

Browse files
authored
feat(bt-server): uninstall & version manager (#62)
* feat(bt-server): uninstall & version manager * fix: `bt-server` manager display overflow * fix(i18n): `settings.bt-server-subtitle` * fix(bt-server): version check error
1 parent 7687260 commit 51df272

File tree

6 files changed

+201
-106
lines changed

6 files changed

+201
-106
lines changed

assets/i18n/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"tmdb-key": "TMDB API Key",
7777
"tmdb-key-subtitle": "Get API Key for TMDB metadata",
7878
"bt-server": "BT Server",
79-
"bt-server-subtitle": "BT Server is a necessary component for online playback of BT seeds",
79+
"bt-server-subtitle": "BT Server is a necessary component for online playback of torrent",
8080
"bt-server-manager": "Manager",
8181
"upgrade": "Update Software",
8282
"upgrade-subtitle": "version: {version}",
@@ -202,6 +202,9 @@
202202
"running": "BT-Server is running",
203203
"stopped": "BT-Server is stopped",
204204
"version": "Version {version}",
205+
"remote-version": "Remote Version {version}",
206+
"stop": "Stop",
207+
"upgrade": "Upgrade",
205208
"start": "Start"
206209
}
207210
}

assets/i18n/zh.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@
191191
"running": "BT-Server 正在运行",
192192
"stopped": "BT-Server 已停止",
193193
"version": "版本 {version}",
194+
"remote-version": "远程版本 {version}",
195+
"stop": "停止",
196+
"upgrade": "升级",
194197
"start": "启动"
195198
}
196199
}

lib/pages/bt_dialog/controller.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:fluent_ui/fluent_ui.dart' as fluent;
23
import 'package:flutter/scheduler.dart';
34
import 'package:get/get.dart';
45
import 'package:miru_app/pages/main/controller.dart';
@@ -9,6 +10,8 @@ class BTDialogController extends GetxController {
910
final isInstalled = false.obs;
1011
final isDownloading = false.obs;
1112
final progress = 0.0.obs;
13+
final hasUpdate = false.obs;
14+
final remoteVersion = "".obs;
1215

1316
final _mainController = Get.find<MainController>();
1417
late final isRuning = _mainController.btServerisRunning;
@@ -17,25 +20,35 @@ class BTDialogController extends GetxController {
1720
@override
1821
void onInit() {
1922
super.onInit();
23+
ever(
24+
isRuning,
25+
(callback) {
26+
hasUpdate.value = version.value != remoteVersion.value;
27+
},
28+
);
2029
SchedulerBinding.instance.addPostFrameCallback((_) async {
2130
isInstalled.value = await BTServerUtils.isInstalled();
31+
remoteVersion.value = await BTServerUtils.getRemoteVersion();
2232
});
2333
}
2434

2535
downloadOrUpgradeServer(BuildContext context) async {
36+
progress.value = 0;
37+
BTServerUtils.stopServer();
38+
isInstalled.value = false;
2639
isDownloading.value = true;
2740
try {
2841
await BTServerUtils.downloadLatestBTServer(
2942
onReceiveProgress: (p0, p1) {
3043
progress.value = p0 / p1;
31-
print(progress.value);
3244
},
3345
);
3446
} catch (e) {
3547
context.mounted &&
3648
showPlatformSnackbar(
3749
context: context,
3850
content: e.toString(),
51+
severity: fluent.InfoBarSeverity.error,
3952
);
4053
} finally {
4154
isDownloading.value = false;

lib/pages/bt_dialog/view.dart

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,77 @@ class _BTDialogState extends State<BTDialog> {
5454
Text("bt-server.stopped".i18n),
5555
const SizedBox(height: 16),
5656
if (c.isRuning.value)
57-
Text(
58-
FlutterI18n.translate(
59-
context,
60-
'bt-server.version',
61-
translationParams: {
62-
"version": c.version.value,
63-
},
57+
Padding(
58+
padding: const EdgeInsets.only(bottom: 16),
59+
child: Row(
60+
children: [
61+
Text(
62+
FlutterI18n.translate(
63+
context,
64+
'bt-server.version',
65+
translationParams: {
66+
"version": c.version.value,
67+
},
68+
),
69+
),
70+
const SizedBox(width: 8),
71+
if (c.hasUpdate.value)
72+
Text(
73+
FlutterI18n.translate(
74+
context,
75+
'bt-server.remote-version',
76+
translationParams: {
77+
"version": c.remoteVersion.value,
78+
},
79+
),
80+
),
81+
],
6482
),
65-
)
66-
else
67-
PlatformFilledButton(
68-
child: Text("bt-server.start".i18n),
69-
onPressed: () {
70-
BTServerUtils.startServer();
71-
},
7283
),
84+
Wrap(
85+
children: [
86+
if (c.isRuning.value) ...[
87+
Padding(
88+
padding: const EdgeInsets.only(right: 8),
89+
child: PlatformFilledButton(
90+
child: Text('bt-server.stop'.i18n),
91+
onPressed: () {
92+
BTServerUtils.stopServer();
93+
},
94+
),
95+
),
96+
// 升级按钮
97+
if (c.hasUpdate.value)
98+
Padding(
99+
padding: const EdgeInsets.only(right: 8),
100+
child: PlatformFilledButton(
101+
child: Text("bt-server.upgrade".i18n),
102+
onPressed: () {
103+
c.downloadOrUpgradeServer(context);
104+
},
105+
),
106+
),
107+
] else
108+
Padding(
109+
padding: const EdgeInsets.only(right: 8),
110+
child: PlatformFilledButton(
111+
child: Text("bt-server.start".i18n),
112+
onPressed: () {
113+
BTServerUtils.startServer();
114+
},
115+
),
116+
),
117+
if (c.isInstalled.value)
118+
// 卸载
119+
PlatformFilledButton(
120+
child: Text("common.uninstall".i18n),
121+
onPressed: () async {
122+
await BTServerUtils.uninstall();
123+
c.isInstalled.value = false;
124+
},
125+
),
126+
],
127+
),
73128
],
74129
);
75130
});

lib/utils/bt_server.dart

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,30 @@ class BTServerUtils {
2828
final res = dio.get(url);
2929
final remoteVersion = (await res).data["tag_name"] as String;
3030
debugPrint("最新版本: $remoteVersion");
31-
late String structure;
31+
late String arch;
3232
late String platform;
3333
if (Platform.isAndroid) {
3434
final supportedAbis = androidDeviceInfo.supportedAbis;
3535
if (supportedAbis.contains("armeabi-v7a")) {
36-
structure = "arm";
36+
arch = "arm";
3737
}
3838
if (supportedAbis.contains("x86_64")) {
39-
structure = "amd64";
39+
arch = "amd64";
4040
}
4141
if (supportedAbis.contains("arm64-v8a")) {
42-
structure = "arm64";
42+
arch = "arm64";
4343
}
4444
platform = "android";
4545
}
4646
if (Platform.isWindows) {
47-
structure = "amd64.exe";
47+
arch = "amd64.exe";
4848
platform = "windows";
4949
}
5050

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

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

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

7676
try {
7777
if (Platform.isWindows) {
78-
await Process.run(
78+
_process = await Process.start(
7979
btServerPath,
8080
[],
8181
workingDirectory: savePath,
@@ -122,6 +122,27 @@ class BTServerUtils {
122122
});
123123
}
124124

125+
// 检查更新
126+
static Future<String> getRemoteVersion() async {
127+
try {
128+
const url =
129+
"https://api.github.com/repos/miru-project/bt-server/releases/latest";
130+
final res = Dio().get(url);
131+
final remoteVersion = (await res).data["tag_name"] as String;
132+
return remoteVersion.replaceFirst("v", '');
133+
} catch (e) {
134+
return Get.find<MainController>().btServerVersion.value;
135+
}
136+
}
137+
138+
// 卸载 bt-server
139+
static Future<void> uninstall() async {
140+
stopServer();
141+
final savePath = await MiruDirectory.getDirectory;
142+
final btServerPath = path.join(savePath, _getBTServerFilename());
143+
await File(btServerPath).delete();
144+
}
145+
125146
static Future<bool> isInstalled() async {
126147
final savePath = await MiruDirectory.getDirectory;
127148
final btServerPath = path.join(savePath, _getBTServerFilename());

0 commit comments

Comments
 (0)