app/lib/ui/index_ui_model.dart

125 lines
3.6 KiB
Dart
Raw Normal View History

2023-10-09 09:32:07 +08:00
import 'dart:io';
2024-02-23 00:07:48 +08:00
import 'package:aria2/models/aria2GlobalStat.dart';
2023-10-30 20:39:31 +08:00
import 'package:starcitizen_doctor/api/analytics.dart';
2023-10-09 09:32:07 +08:00
import 'package:starcitizen_doctor/base/ui_model.dart';
2023-11-07 22:35:25 +08:00
import 'package:starcitizen_doctor/common/helper/system_helper.dart';
2024-02-23 00:07:48 +08:00
import 'package:starcitizen_doctor/common/io/aria2c.dart';
2023-10-09 09:32:07 +08:00
import 'package:starcitizen_doctor/global_ui_model.dart';
import 'package:starcitizen_doctor/ui/about/about_ui_model.dart';
2024-02-24 14:42:33 +08:00
import 'package:starcitizen_doctor/ui/home/downloads/downloads_ui.dart';
import 'package:starcitizen_doctor/ui/home/downloads/downloads_ui_model.dart';
2023-10-09 09:32:07 +08:00
import 'package:starcitizen_doctor/ui/home/home_ui_model.dart';
import 'package:starcitizen_doctor/ui/settings/settings_ui_model.dart';
import 'package:starcitizen_doctor/ui/tools/tools_ui_model.dart';
import 'package:url_launcher/url_launcher_string.dart';
2024-01-07 14:57:49 +08:00
import 'party_room/party_room_home_ui_model.dart';
2023-10-09 09:32:07 +08:00
class IndexUIModel extends BaseUIModel {
int curIndex = 0;
2024-02-23 00:07:48 +08:00
Aria2GlobalStat? aria2globalStat;
int get aria2TotalTaskNum => aria2globalStat == null
? 0
: ((aria2globalStat!.numActive ?? 0) +
(aria2globalStat!.numWaiting ?? 0));
2023-10-09 09:32:07 +08:00
@override
void initModel() {
2024-02-23 00:07:48 +08:00
_checkRuntime();
_listenAria2c();
2023-10-09 09:32:07 +08:00
Future.delayed(const Duration(milliseconds: 300))
2023-11-11 01:18:30 +08:00
.then((value) => globalUIModel.doCheckUpdate(context!));
2023-10-09 09:32:07 +08:00
super.initModel();
}
@override
BaseUIModel? onCreateChildUIModel(modelKey) {
switch (modelKey) {
case "home":
return HomeUIModel();
case "tools":
return ToolsUIModel();
case "settings":
return SettingUIModel();
case "about":
return AboutUIModel();
2024-01-07 14:57:49 +08:00
case "party":
return PartyRoomHomeUIModel();
2023-10-09 09:32:07 +08:00
}
return null;
}
void onIndexMenuTap(String value) {
final index = {
"首页": 0,
2024-01-07 14:57:49 +08:00
"大厅": 1,
"工具": 2,
"设置": 3,
"关于": 4,
2023-10-09 09:32:07 +08:00
};
curIndex = index[value] ?? 0;
switch (curIndex) {
case 0:
getCreatedChildUIModel("home")?.reloadData();
break;
case 1:
2024-01-07 14:57:49 +08:00
getCreatedChildUIModel("party")?.reloadData();
2023-10-09 09:32:07 +08:00
break;
2023-12-06 20:49:14 +08:00
case 2:
2024-01-07 14:57:49 +08:00
getCreatedChildUIModel("tools")?.reloadData();
break;
case 3:
2023-12-06 20:49:14 +08:00
getCreatedChildUIModel("settings")?.reloadData();
break;
2023-10-09 09:32:07 +08:00
}
notifyListeners();
}
2024-02-23 00:07:48 +08:00
Future<void> _checkRuntime() async {
2023-10-09 09:32:07 +08:00
Future<void> onError() async {
await showToast(context!, "运行环境出错,请检查系统环境变量 PATH");
await launchUrlString(
"https://answers.microsoft.com/zh-hans/windows/forum/all/%E7%B3%BB%E7%BB%9F%E7%8E%AF%E5%A2%83%E5%8F%98/b88369e6-2620-4a77-b07a-d0af50894a07");
2023-10-30 20:39:31 +08:00
await AnalyticsApi.touch("error_powershell");
2023-10-09 09:32:07 +08:00
exit(0);
}
try {
2023-12-06 20:49:14 +08:00
var result =
await Process.run(SystemHelper.powershellPath, ["echo", "ping"]);
2023-10-09 09:32:07 +08:00
if (result.stdout.toString().startsWith("ping")) {
dPrint("powershell check pass");
} else {
onError();
}
} catch (e) {
onError();
}
}
2024-02-23 00:07:48 +08:00
2024-02-24 14:42:33 +08:00
Future<void> goDownloader() async {
await BaseUIContainer(
uiCreate: () => DownloadsUI(),
modelCreate: () => DownloadsUIModel()).push(context!);
2024-02-23 00:07:48 +08:00
}
void _listenAria2c() async {
while (true) {
2024-02-24 17:41:44 +08:00
if (!mounted) return;
2024-02-23 00:07:48 +08:00
try {
2024-02-24 17:41:44 +08:00
if (Aria2cManager.isAvailable) {
final aria2c = Aria2cManager.getClient();
aria2globalStat = await aria2c.getGlobalStat();
notifyListeners();
}
2024-02-23 00:07:48 +08:00
} catch (e) {
dPrint("aria2globalStat update error:$e");
}
2024-02-24 17:41:44 +08:00
await Future.delayed(const Duration(seconds: 5));
2024-02-23 00:07:48 +08:00
}
}
2023-10-09 09:32:07 +08:00
}