diff --git a/lib/api/analytics.dart b/lib/api/analytics.dart index 587f7d8..4c9f028 100644 --- a/lib/api/analytics.dart +++ b/lib/api/analytics.dart @@ -11,7 +11,7 @@ class AnalyticsApi { if (kDebugMode) return; dPrint("AnalyticsApi.touch === $key start"); try { - await _dio.post("${URLConf.xkeycApiUrl}/analytics/$key"); + await _dio.post("${URLConf.xkeycApiHome}/analytics/$key"); dPrint("AnalyticsApi.touch === $key over"); } catch (e) { dPrint("AnalyticsApi.touch === $key Error:$e"); diff --git a/lib/base/ui.dart b/lib/base/ui.dart index a57aba8..c57f140 100644 --- a/lib/base/ui.dart +++ b/lib/base/ui.dart @@ -23,7 +23,7 @@ class BaseUIContainer extends ConsumerStatefulWidget { ConsumerState createState() => uiCreate(); Future push(BuildContext context) { - return Navigator.push(context, makeRoute(context, this)); + return Navigator.push(context, makeRoute(context)); } // Future pushShowModalBottomSheet(BuildContext context) { @@ -37,11 +37,10 @@ class BaseUIContainer extends ConsumerStatefulWidget { // } /// 获取路由 - FluentPageRoute makeRoute( - BuildContext context, BaseUIContainer baseUIContainer) { + FluentPageRoute makeRoute(BuildContext context) { return MyPageRoute( builder: (BuildContext context) { - return baseUIContainer; + return this; }, ); } diff --git a/lib/common/conf/app_conf.dart b/lib/common/conf/app_conf.dart index 9216028..9f338b5 100644 --- a/lib/common/conf/app_conf.dart +++ b/lib/common/conf/app_conf.dart @@ -99,8 +99,6 @@ class AppConf { ); } }); - await checkUpdate(); - AnalyticsApi.touch("launch"); } static String getUpgradePath() { diff --git a/lib/common/conf/url_conf.dart b/lib/common/conf/url_conf.dart index 60c7ced..3d39e9a 100644 --- a/lib/common/conf/url_conf.dart +++ b/lib/common/conf/url_conf.dart @@ -1,28 +1,89 @@ +import 'package:dio/dio.dart'; +import 'package:starcitizen_doctor/base/ui_model.dart'; + class URLConf { - static const String giteaAttachmentsUrl = - "https://git.sctoolbox.sccsgo.com/SCToolBox/Release"; - static const String gitlabLocalizationUrl = - "https://git.sctoolbox.sccsgo.com/SCToolBox/LocalizationData"; - static const String apiRepoPath = - "https://git.sctoolbox.sccsgo.com/SCToolBox/api/raw/branch/main/"; + /// HOME API + static String gitApiHome = "https://git.sctoolbox.sccsgo.com"; + static String rssApiHome = "https://rss.sctoolbox.sccsgo.com"; + static const String xkeycApiHome = "https://sctoolbox.xkeyc.com"; - static const String gitlabApiPath = - "https://git.sctoolbox.sccsgo.com/api/v1/"; + static bool isUsingFallback = false; - static const String webTranslateHomeUrl = - "https://git.sctoolbox.sccsgo.com/SCToolBox/ScWeb_Chinese_Translate/raw/branch/main/json/locales"; + /// URLS + static String giteaAttachmentsUrl = "$gitApiHome/SCToolBox/Release"; + static String gitlabLocalizationUrl = + "$gitApiHome/SCToolBox/LocalizationData"; + static String apiRepoPath = "$gitApiHome/SCToolBox/api/raw/branch/main/"; - static const String xkeycApiUrl = "https://sctoolbox.xkeyc.com"; + static String gitlabApiPath = "https://$gitApiHome/api/v1/"; - static const _rssHomeUrl = "https://rss.sctoolbox.sccsgo.com"; + static String webTranslateHomeUrl = + "$gitApiHome/SCToolBox/ScWeb_Chinese_Translate/raw/branch/main/json/locales"; - static const rssVideoUrl = - "$_rssHomeUrl/bilibili/user/channel/27976358/290653"; + static String rssVideoUrl = + "$rssApiHome/bilibili/user/channel/27976358/290653"; - static const rssTextUrl1 = "$_rssHomeUrl/bilibili/user/article/40102960"; - static const rssTextUrl2 = - "$_rssHomeUrl/baidu/tieba/user/%E7%81%AC%E7%81%ACG%E7%81%AC%E7%81%AC&"; + static String rssTextUrl1 = "$rssApiHome/bilibili/user/article/40102960"; + static String rssTextUrl2 = + "$rssApiHome/baidu/tieba/user/%E7%81%AC%E7%81%ACG%E7%81%AC%E7%81%AC&"; static const feedbackUrl = "https://txc.qq.com/products/614843"; - static const devReleaseUrl = "https://git.sctoolbox.sccsgo.com/SCToolBox/Release/releases"; + + static const devReleaseUrl = + "https://git.sctoolbox.sccsgo.com/SCToolBox/Release/releases"; + + static const _gitApiList = [ + "https://git.sctoolbox.sccsgo.com", + "https://sctb-git.xkeyc.com" + ]; + + static const _rssApiList = [ + "https://rss.sctoolbox.sccsgo.com", + "https://rss.42kit.com" + ]; + + static checkHost() async { + final dio = Dio(BaseOptions(connectTimeout: const Duration(seconds: 5))); + bool hasAvailable = false; + // 寻找可用的 git API + for (var value in _gitApiList) { + try { + final resp = await dio.head(value); + if (resp.statusCode == 200) { + dPrint("[URLConf].checkHost passed $value"); + gitApiHome = value; + hasAvailable = true; + break; + } + isUsingFallback = true; + continue; + } catch (e) { + dPrint("[URLConf].checkHost $value Error= $e"); + isUsingFallback = true; + continue; + } + } + // 寻找可用的 RSS API + for (var value in _rssApiList) { + try { + final resp = await dio.head(value); + if (resp.statusCode == 200) { + rssApiHome = value; + hasAvailable = true; + dPrint("[URLConf].checkHost passed $value"); + break; + } + isUsingFallback = true; + continue; + } catch (e) { + dPrint("[URLConf].checkHost $value Error= $e"); + isUsingFallback = true; + continue; + } + } + + if (!hasAvailable) { + isUsingFallback = false; + } + } } diff --git a/lib/main.dart b/lib/main.dart index 9d3d445..6260195 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,11 +2,11 @@ import 'package:desktop_webview_window/desktop_webview_window.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:starcitizen_doctor/base/ui_model.dart'; import 'package:starcitizen_doctor/common/conf/app_conf.dart'; -import 'package:starcitizen_doctor/ui/index_ui_model.dart'; +import 'package:starcitizen_doctor/ui/splash_ui_model.dart'; import 'package:window_manager/window_manager.dart'; import 'global_ui_model.dart'; -import 'ui/index_ui.dart'; +import 'ui/splash_ui.dart'; void main(List args) async { if (runWebViewTitleBarWidget(args, @@ -53,7 +53,7 @@ class AppUI extends BaseUI { ))), debugShowCheckedModeBanner: false, home: BaseUIContainer( - uiCreate: () => IndexUI(), modelCreate: () => IndexUIModel()), + uiCreate: () => SplashUI(), modelCreate: () => SplashUIModel()), ); } diff --git a/lib/ui/home/home_ui_model.dart b/lib/ui/home/home_ui_model.dart index 944201f..c927d60 100644 --- a/lib/ui/home/home_ui_model.dart +++ b/lib/ui/home/home_ui_model.dart @@ -113,11 +113,7 @@ class HomeUIModel extends BaseUIModel { .data)); countdownFestivalListData = await Api.getFestivalCountdownList(); notifyListeners(); - try { - _loadRRS(); - } catch (e) { - dPrint("_loadRRS Error:$e"); - } + _loadRRS(); } catch (e) { dPrint(e); } @@ -139,6 +135,12 @@ class HomeUIModel extends BaseUIModel { appUpdateTimer = Timer.periodic(const Duration(minutes: 30), (timer) { _checkLocalizationUpdate(); }); + Future.delayed(const Duration(milliseconds: 100)).then((value) { + if (URLConf.isUsingFallback) { + if (!mounted) return; + showToast(context!, "因源服务器异常(机房故障或遭受攻击),当前正在使用备用线路,可能会出现访问速度下降,敬请谅解。"); + } + }); super.initModel(); } @@ -187,13 +189,17 @@ class HomeUIModel extends BaseUIModel { } Future _loadRRS() async { - final v = await RSSApi.getRssVideo(); - rssVideoItems = v; - notifyListeners(); - final t = await RSSApi.getRssText(); - rssTextItems = t; - notifyListeners(); - dPrint("RSS update Success !"); + try { + final v = await RSSApi.getRssVideo(); + rssVideoItems = v; + notifyListeners(); + final t = await RSSApi.getRssText(); + rssTextItems = t; + notifyListeners(); + dPrint("RSS update Success !"); + } catch (e) { + dPrint("_loadRRS Error:$e"); + } } VoidCallback? doCheck() { diff --git a/lib/ui/home/webview/webview.dart b/lib/ui/home/webview/webview.dart index 47f2a79..d07af8a 100644 --- a/lib/ui/home/webview/webview.dart +++ b/lib/ui/home/webview/webview.dart @@ -207,7 +207,7 @@ class WebViewModel { /// https://github.com/CxJuice/Uex_Chinese_Translate // get versions - const hostUrl = URLConf.webTranslateHomeUrl; + final hostUrl = URLConf.webTranslateHomeUrl; dPrint("AppWebLocalizationVersionsData === ${v.toJson()}"); localizationResource["zh-CN"] = await _getJson("$hostUrl/zh-CN-rsi.json", diff --git a/lib/ui/splash_ui.dart b/lib/ui/splash_ui.dart new file mode 100644 index 0000000..20dd7b6 --- /dev/null +++ b/lib/ui/splash_ui.dart @@ -0,0 +1,45 @@ +import 'package:starcitizen_doctor/base/ui.dart'; +import 'package:starcitizen_doctor/common/conf/app_conf.dart'; + +import 'splash_ui_model.dart'; + +class SplashUI extends BaseUI { + @override + Widget? buildBody(BuildContext context, SplashUIModel model) { + return makeDefaultPage(context, model, + content: Center( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Image.asset("assets/app_logo.png", width: 192, height: 192), + const SizedBox(height: 32), + const ProgressRing(), + const SizedBox(height: 32), + if (model.step == 0) const Text("正在检测可用性,这可能需要一点时间..."), + if (model.step == 1) const Text("正在检查更新..."), + if (model.step == 2) const Text("即将完成..."), + ], + ), + ), + automaticallyImplyLeading: false, + titleRow: Align( + alignment: AlignmentDirectional.centerStart, + child: Row( + children: [ + Image.asset( + "assets/app_logo_mini.png", + width: 20, + height: 20, + fit: BoxFit.cover, + ), + const SizedBox(width: 12), + const Text( + "SC汉化盒子 V${AppConf.appVersion} ${AppConf.isMSE ? "" : " Dev"}") + ], + ), + )); + } + + @override + String getUITitle(BuildContext context, SplashUIModel model) => ""; +} diff --git a/lib/ui/splash_ui_model.dart b/lib/ui/splash_ui_model.dart new file mode 100644 index 0000000..cca0e0d --- /dev/null +++ b/lib/ui/splash_ui_model.dart @@ -0,0 +1,34 @@ +import 'package:starcitizen_doctor/api/analytics.dart'; +import 'package:starcitizen_doctor/base/ui_model.dart'; +import 'package:starcitizen_doctor/common/conf/url_conf.dart'; +import 'package:starcitizen_doctor/ui/index_ui.dart'; +import 'package:starcitizen_doctor/ui/index_ui_model.dart'; + +import '../common/conf/app_conf.dart'; + +class SplashUIModel extends BaseUIModel { + int step = 0; + + @override + void initModel() { + _initApp(); + super.initModel(); + } + + Future _initApp() async { + AnalyticsApi.touch("launch"); + await URLConf.checkHost(); + step = 1; + notifyListeners(); + await AppConf.checkUpdate(); + step = 2; + notifyListeners(); + Future.delayed(const Duration(milliseconds: 300)); + Navigator.pushAndRemoveUntil( + context!, + BaseUIContainer( + uiCreate: () => IndexUI(), + modelCreate: () => IndexUIModel()).makeRoute(context!), + (route) => false); + } +}