app/lib/ui/index_ui_model.dart
2024-02-24 17:41:44 +08:00

125 lines
3.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:io';
import 'package:aria2/models/aria2GlobalStat.dart';
import 'package:starcitizen_doctor/api/analytics.dart';
import 'package:starcitizen_doctor/base/ui_model.dart';
import 'package:starcitizen_doctor/common/helper/system_helper.dart';
import 'package:starcitizen_doctor/common/io/aria2c.dart';
import 'package:starcitizen_doctor/global_ui_model.dart';
import 'package:starcitizen_doctor/ui/about/about_ui_model.dart';
import 'package:starcitizen_doctor/ui/home/downloads/downloads_ui.dart';
import 'package:starcitizen_doctor/ui/home/downloads/downloads_ui_model.dart';
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';
import 'party_room/party_room_home_ui_model.dart';
class IndexUIModel extends BaseUIModel {
int curIndex = 0;
Aria2GlobalStat? aria2globalStat;
int get aria2TotalTaskNum => aria2globalStat == null
? 0
: ((aria2globalStat!.numActive ?? 0) +
(aria2globalStat!.numWaiting ?? 0));
@override
void initModel() {
_checkRuntime();
_listenAria2c();
Future.delayed(const Duration(milliseconds: 300))
.then((value) => globalUIModel.doCheckUpdate(context!));
super.initModel();
}
@override
BaseUIModel? onCreateChildUIModel(modelKey) {
switch (modelKey) {
case "home":
return HomeUIModel();
case "tools":
return ToolsUIModel();
case "settings":
return SettingUIModel();
case "about":
return AboutUIModel();
case "party":
return PartyRoomHomeUIModel();
}
return null;
}
void onIndexMenuTap(String value) {
final index = {
"首页": 0,
"大厅": 1,
"工具": 2,
"设置": 3,
"关于": 4,
};
curIndex = index[value] ?? 0;
switch (curIndex) {
case 0:
getCreatedChildUIModel("home")?.reloadData();
break;
case 1:
getCreatedChildUIModel("party")?.reloadData();
break;
case 2:
getCreatedChildUIModel("tools")?.reloadData();
break;
case 3:
getCreatedChildUIModel("settings")?.reloadData();
break;
}
notifyListeners();
}
Future<void> _checkRuntime() async {
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");
await AnalyticsApi.touch("error_powershell");
exit(0);
}
try {
var result =
await Process.run(SystemHelper.powershellPath, ["echo", "ping"]);
if (result.stdout.toString().startsWith("ping")) {
dPrint("powershell check pass");
} else {
onError();
}
} catch (e) {
onError();
}
}
Future<void> goDownloader() async {
await BaseUIContainer(
uiCreate: () => DownloadsUI(),
modelCreate: () => DownloadsUIModel()).push(context!);
}
void _listenAria2c() async {
while (true) {
if (!mounted) return;
try {
if (Aria2cManager.isAvailable) {
final aria2c = Aria2cManager.getClient();
aria2globalStat = await aria2c.getGlobalStat();
notifyListeners();
}
} catch (e) {
dPrint("aria2globalStat update error:$e");
}
await Future.delayed(const Duration(seconds: 5));
}
}
}