From 7bb467ec08cc6fe616c260d5894e838b65420fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=B2=AB=E8=8A=B1=E7=81=ABzzz?= Date: Sat, 30 Dec 2023 19:55:51 +0800 Subject: [PATCH] =?UTF-8?q?[*]=E6=8F=90=E5=8F=96=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81=E4=B8=BAUIModel=20=E6=9B=B4?= =?UTF-8?q?=E5=A5=BD=E7=9A=84Frpc=E4=B8=8B=E8=BD=BD=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/controller/dsetting.dart | 47 +++----------------- lib/io/frpcManagerStorage.dart | 34 ++++++++------ lib/prefs/SettingPrefs.dart | 9 ++-- lib/ui/model/FrpcDownloadTip.dart | 73 +++++++++++++++++++++++++++++++ lib/ui/setting/frpcmanager.dart | 64 --------------------------- 5 files changed, 106 insertions(+), 121 deletions(-) create mode 100644 lib/ui/model/FrpcDownloadTip.dart diff --git a/lib/controller/dsetting.dart b/lib/controller/dsetting.dart index 3106b303..fb956b4a 100644 --- a/lib/controller/dsetting.dart +++ b/lib/controller/dsetting.dart @@ -7,6 +7,7 @@ import 'package:nyalcf/io/frpcManagerStorage.dart'; import 'package:nyalcf/model/FrpcConfig.dart'; import 'package:nyalcf/prefs/SettingPrefs.dart'; import 'package:nyalcf/ui/model/FrpcDownloadDialog.dart'; +import 'package:nyalcf/ui/model/FrpcDownloadTip.dart'; import 'package:nyalcf/util/CPUArch.dart'; import 'package:nyalcf/util/frpc/Archive.dart'; import 'package:package_info_plus/package_info_plus.dart'; @@ -26,6 +27,7 @@ class DSettingController extends GetxController { var frpc_download_arch = 0.obs; var frpc_download_progress = 0.0.obs; var frpc_download_show = [].obs; + var _frpc_downloaded_versions = []; dynamic frpc_download_cancel = false; var frpc_version = ''.obs; @@ -36,7 +38,6 @@ class DSettingController extends GetxController { var github_proxy = ''.obs; - var _frpc_downloaded_versions = []; var cpu_arch = ''.obs; load() async { @@ -55,46 +56,9 @@ class DSettingController extends GetxController { void _load_tip() async { _frpc_downloaded_versions = await FrpcManagerStorage.downloadedVersions; if (_frpc_downloaded_versions.length == 0) { - frpc_download_tip.value = Container( - child: Card( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - ListTile( - leading: Icon(Icons.warning), - title: Text('尚未安装任何版本的 Frpc'), - ), - Container( - margin: EdgeInsets.only(left: 20.0, right: 20.0, bottom: 20.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text('无法启动隧道了...呜呜...'), - Container( - margin: EdgeInsets.only(top: 10.0), - child: Row( - children: [ - ElevatedButton( - onPressed: () async { - showDialog( - context: context, - builder: (context) { - return FrpcDownloadDialogX(context: context) - .build(); - }); - }, - child: Text('下载'), - ), - ], - ), - ), - ], - ), - ) - ], - ), - ), - ); + frpc_download_tip.value = FrpcDownloadTip.tip(context: context); + } else { + frpc_download_tip.value = await FrpcDownloadTip.downloaded(context: context); } } @@ -143,6 +107,7 @@ class DSettingController extends GetxController { if (!Platform.isWindows) { await FrpcManagerStorage.setRunPermission(); } + _load_tip(); } else { Get.snackbar( '解压 Frpc 时发生错误..呜呜..', diff --git a/lib/io/frpcManagerStorage.dart b/lib/io/frpcManagerStorage.dart index 32e792c5..a09ab764 100644 --- a/lib/io/frpcManagerStorage.dart +++ b/lib/io/frpcManagerStorage.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'dart:io'; import 'package:nyalcf/model/FrpcConfig.dart'; +import 'package:nyalcf/prefs/SettingPrefs.dart'; import 'package:nyalcf/util/FileIO.dart'; class FrpcManagerStorage { @@ -11,7 +12,7 @@ class FrpcManagerStorage { return '${await _s_path}/frpc'; } - static Future get _info async { + static Future read() async { try { final String result = await File('${await _path}/info.json').readAsString(encoding: utf8); @@ -21,19 +22,26 @@ class FrpcManagerStorage { } } + static Future get _info async => SettingPrefs.getFrpcInfo(); + static void init() { _path.then((path) { if (!Directory(path).existsSync()) Directory(path).createSync(); final infoF = File('${path}/info.json'); if (!infoF.existsSync()) { - Map json = Map(); - json['settings']['frpc_version'] = ''; - json['settings']['github_proxy'] = []; - json['lists']['frpc_downloaded_versions'] = []; - json['lists']['github_proxies'] = [ - 'https://mirror.ghproxy.com/', - 'https://ghproxy.com/', - ]; + Map json = { + 'settings': { + 'frpc_version': '', + 'github_proxy': [], + }, + 'lists': { + 'frpc_downloaded_versions': [], + 'github_proxies': [ + 'https://mirror.ghproxy.com/', + 'https://ghproxy.com/', + ], + }, + }; infoF.writeAsStringSync(jsonEncode(json)); } }); @@ -63,25 +71,25 @@ class FrpcManagerStorage { /// 获取正在使用的版本 static Future get usingVersion async { - final version = (await _info)?.frpc_version ?? ''; + final version = (await _info).frpc_version; return version; } /// 自定义GitHub代理列表 static Future> get proxies async { - final url = (await _info)?.github_proxies ?? []; + final url = (await _info).github_proxies; return url; } /// GitHub代理 static Future get proxyUrl async { - final url = (await _info)?.github_proxy ?? ''; + final url = (await _info).github_proxy; return url; } /// 获取已安装版本列表 static Future> get downloadedVersions async { - final versions = (await _info)?.frpc_downloaded_versions ?? []; + final versions = (await _info).frpc_downloaded_versions; print(versions); return versions; } diff --git a/lib/prefs/SettingPrefs.dart b/lib/prefs/SettingPrefs.dart index 6dc1ef79..6e95f0c0 100644 --- a/lib/prefs/SettingPrefs.dart +++ b/lib/prefs/SettingPrefs.dart @@ -1,3 +1,4 @@ +import 'package:nyalcf/io/frpcManagerStorage.dart'; import 'package:nyalcf/model/FrpcConfig.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -13,10 +14,10 @@ class SettingPrefs { static Future setFrpcDownloadedVersionsInfo(String version) async { SharedPreferences prefs = await SharedPreferences.getInstance(); - final newlist = (await getFrpcInfo()).lists['frpc_downloaded_versions'] ?? []; + final newlist = + (await getFrpcInfo()).lists['frpc_downloaded_versions'] ?? []; newlist.add(version); - prefs.setStringList( - 'list@frpc_downloaded_versions', newlist); + prefs.setStringList('list@frpc_downloaded_versions', newlist); } static Future getFrpcInfo() async { @@ -37,4 +38,6 @@ class SettingPrefs { return FrpcConfig(settings: settings, lists: lists); } + + static Future refresh() async => { if (await FrpcManagerStorage.read() != null) setFrpcInfo((await FrpcManagerStorage.read())!)}; } diff --git a/lib/ui/model/FrpcDownloadTip.dart b/lib/ui/model/FrpcDownloadTip.dart new file mode 100644 index 00000000..ffd23e51 --- /dev/null +++ b/lib/ui/model/FrpcDownloadTip.dart @@ -0,0 +1,73 @@ +import 'package:flutter/material.dart'; +import 'package:nyalcf/prefs/SettingPrefs.dart'; + +import 'FrpcDownloadDialog.dart'; + +class FrpcDownloadTip { + + static Container tip({required context}) => Container( + child: Card( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + ListTile( + leading: Icon(Icons.warning), + title: Text('尚未安装任何版本的 Frpc'), + ), + Container( + margin: EdgeInsets.only(left: 20.0, right: 20.0, bottom: 20.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text('无法启动隧道了...呜呜...'), + Container( + margin: EdgeInsets.only(top: 10.0), + child: Row( + children: [ + ElevatedButton( + onPressed: () async { + showDialog( + context: context, + builder: (context) { + return FrpcDownloadDialogX(context: context) + .build(); + }); + }, + child: Text('下载'), + ), + ], + ), + ), + ], + ), + ) + ], + ), + ), + ); + + static Future downloaded({required context}) async => Container( + child: Card( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + ListTile( + leading: Icon(Icons.check_circle), + title: Text('可用的 Frpc 版本已安装!素晴らしい!'), + ), + Container( + margin: EdgeInsets.only(left: 20.0, right: 20.0, bottom: 20.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text('已安装版本列表:'), + Text((await SettingPrefs.getFrpcInfo()).lists['frpc_downloaded_versions'].toString()), + ], + ), + ) + ], + ), + ), + ); + +} \ No newline at end of file diff --git a/lib/ui/setting/frpcmanager.dart b/lib/ui/setting/frpcmanager.dart index b3703488..7a2cfc06 100644 --- a/lib/ui/setting/frpcmanager.dart +++ b/lib/ui/setting/frpcmanager.dart @@ -11,70 +11,6 @@ class FrpcManagerSX { child: ListView( children: [ Obx(() => ds_c.frpc_download_tip.value), - - /* - Card( - child: Row( - children: [ - Expanded( - child: ListTile( - leading: Icon(Icons.cabin), - title: Text('Frpc版本'), - ), - ), - Container( - width: 80, - margin: EdgeInsets.only(right: 25.0), - child: Obx( - () => DropdownButton( - value: ds_c.frpc_version_value.value, - items: ds_c.frpc_version_widgets, - onChanged: (v) { - ds_c.frpc_version_value.value = v; - }, - ), - ), - ) - ], - ), - ), - Card( - child: Column( - children: [ - Row( - children: [ - Expanded( - child: ListTile( - leading: Icon(Icons.cabin), - title: Text('GitHub下载源代理设置'), - ), - ), - Container( - width: 80, - margin: EdgeInsets.only(right: 25.0), - child: Obx( - () => DropdownButton( - value: ds_c.github_proxy_url_value.value, - items: ds_c.github_proxy_widgets, - onChanged: (v) { - ds_c.github_proxy_url_value.value = v; - }, - ), - ), - ) - ], - ), - Obx(() => Text( - '当前代理:${ds_c.github_proxy_url}', - style: TextStyle( - color: Colors.grey, - fontStyle: FontStyle.italic, - ), - )), - ElevatedButton(onPressed: null, child: Text('保存')) - ], - ), - ),*/ ], ), );