Skip to content

Commit

Permalink
feat(bt-server): uninstall & version manager
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoMint committed Sep 7, 2023
1 parent a4e7715 commit 9a6cc3d
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 100 deletions.
3 changes: 3 additions & 0 deletions assets/i18n/en.json
Original file line number Diff line number Diff line change
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();
},
),
Row(
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
19 changes: 18 additions & 1 deletion lib/utils/bt_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,23 @@ class BTServerUtils {
});
}

// 检查更新
static Future<String> getRemoteVersion() async {
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", '');
}

// 卸载 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 9a6cc3d

Please sign in to comment.