From 49cef9ed905933bb46e2713a91cb9c6d3fd59eb9 Mon Sep 17 00:00:00 2001 From: xkeyC <3334969096@qq.com> Date: Wed, 6 Nov 2024 19:39:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=8C=87=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ui/home/home_ui.dart | 31 ++- .../localization/localization_dialog_ui.dart | 100 +-------- .../localization/localization_ui_model.dart | 201 +++++++++++++----- 3 files changed, 179 insertions(+), 153 deletions(-) diff --git a/lib/ui/home/home_ui.dart b/lib/ui/home/home_ui.dart index f6faf9e..ca07845 100644 --- a/lib/ui/home/home_ui.dart +++ b/lib/ui/home/home_ui.dart @@ -802,8 +802,8 @@ class HomeUI extends HookConsumerWidget { context: context, builder: (context) => const HomeCountdownDialogUI()); } - _onMenuTap(BuildContext context, String key, HomeUIModelState homeState, - WidgetRef ref) async { + Future _onMenuTap(BuildContext context, String key, + HomeUIModelState homeState, WidgetRef ref) async { String gameInstallReqInfo = S.current.home_action_info_valid_install_location_required; switch (key) { @@ -868,15 +868,32 @@ class HomeUI extends HookConsumerWidget { Text("是否前往汉化管理安装?\n\n如已安装汉化,请卸载并在重新安装时打开社区输入法支持开关。")); if (userOK) { if (!context.mounted) return; - _onMenuTap(context, 'localization', homeState, ref); + () async { + await _onMenuTap(context, 'localization', homeState, ref); + final localizationState = ref.read(localizationUIModelProvider); + if (localizationState.installedCommunityInputMethodSupportVersion != null) { + await Future.delayed(Duration(milliseconds: 300)); + if (!context.mounted) return; + await _goInputMethod(context, model); + return; + } + }(); + + await Future.delayed(Duration(milliseconds: 300)); + final localizationModel = + ref.read(localizationUIModelProvider.notifier); + if (!context.mounted) return; + localizationModel.checkReinstall(context); } return; } - showDialog( + await _goInputMethod(context, model); + } + + Future _goInputMethod(BuildContext context, HomeUIModel model) async { + await showDialog( context: context, - builder: (BuildContext context) { - return InputMethodDialogUI(); - }, + builder: (context) => const InputMethodDialogUI(), ); } } diff --git a/lib/ui/home/localization/localization_dialog_ui.dart b/lib/ui/home/localization/localization_dialog_ui.dart index ec2ab76..7a5e9a3 100644 --- a/lib/ui/home/localization/localization_dialog_ui.dart +++ b/lib/ui/home/localization/localization_dialog_ui.dart @@ -209,8 +209,9 @@ class LocalizationDialogUI extends HookConsumerWidget { borderRadius: BorderRadius.circular(7), disable: tapDisabled, child: GestureDetector( - onTap: - tapDisabled ? null : () => doInsTall(context, model, item, state), + onTap: tapDisabled + ? null + : () => model.onRemoteInsTall(context, item, state), child: Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( @@ -423,101 +424,6 @@ class LocalizationDialogUI extends HookConsumerWidget { ); } - doInsTall( - BuildContext context, - LocalizationUIModel model, - MapEntry item, - LocalizationUIState state) async { - bool enableCommunityInputMethod = - state.communityInputMethodLanguageData != null; - final userOK = await showConfirmDialogs( - context, - "${item.value.info}", - Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - S.current.localization_info_version_number( - item.value.versionName ?? ""), - style: TextStyle(color: Colors.white.withOpacity(.6)), - ), - const SizedBox(height: 4), - Text( - S.current - .localization_info_channel(item.value.gameChannel ?? ""), - style: TextStyle(color: Colors.white.withOpacity(.6)), - ), - const SizedBox(height: 4), - Text( - S.current - .localization_info_update_time(item.value.updateAt ?? ""), - style: TextStyle(color: Colors.white.withOpacity(.6)), - ), - const SizedBox(height: 12), - ], - ), - const SizedBox(height: 12), - Container( - decoration: BoxDecoration( - color: FluentTheme.of(context).cardColor, - borderRadius: BorderRadius.circular(7)), - padding: const EdgeInsets.all(12), - child: Row( - children: [ - Expanded( - child: Text( - item.value.note ?? S.current.home_localization_msg_no_note, - style: const TextStyle(fontSize: 15), - ), - ), - ], - ), - ), - SizedBox(height: 12), - Row( - children: [ - Text( - "安装社区输入法支持", - ), - Spacer(), - StatefulBuilder( - builder: (BuildContext context, - void Function(void Function()) setState) { - return ToggleSwitch( - checked: enableCommunityInputMethod, - onChanged: state.communityInputMethodLanguageData == null - ? null - : (v) { - enableCommunityInputMethod = v; - setState(() {}); - }, - ); - }, - ) - ], - ) - ], - ), - confirm: S.current.localization_action_install, - cancel: S.current.home_action_cancel, - constraints: - BoxConstraints(maxWidth: MediaQuery.of(context).size.width * .45), - ); - if (userOK) { - if (!context.mounted) return; - dPrint("doRemoteInstall ${item.value} $enableCommunityInputMethod"); - model - .doRemoteInstall(context, item.value, - isEnableCommunityInputMethod: enableCommunityInputMethod) - ?.call(); - } - } - Widget makeToolsListContainer(BuildContext context, LocalizationUIModel model, LocalizationUIState state) { final toolsMenu = { diff --git a/lib/ui/home/localization/localization_ui_model.dart b/lib/ui/home/localization/localization_ui_model.dart index 2e8bfa0..5e35172 100644 --- a/lib/ui/home/localization/localization_ui_model.dart +++ b/lib/ui/home/localization/localization_ui_model.dart @@ -233,7 +233,7 @@ class LocalizationUIModel extends _$LocalizationUIModel { return path.split("\\").last; } - installFormString( + Future installFormString( StringBuffer globalIni, String versionName, { bool? advanced, @@ -309,56 +309,54 @@ class LocalizationUIModel extends _$LocalizationUIModel { return null; } - VoidCallback? doRemoteInstall(BuildContext context, ScLocalizationData value, - {bool isEnableCommunityInputMethod = false}) { - return () async { - AnalyticsApi.touch("install_localization"); + Future? doRemoteInstall(BuildContext context, ScLocalizationData value, + {bool isEnableCommunityInputMethod = false}) async { + AnalyticsApi.touch("install_localization"); - final savePath = - File("${_downloadDir.absolute.path}\\${value.versionName}.sclang"); - try { - state = state.copyWith(workingVersion: value.versionName!); - if (!await savePath.exists()) { - // download - await downloadLocalizationFile(savePath, value); - } else { - dPrint("use cache $savePath"); - } - - final communityInputMethodData = state.communityInputMethodLanguageData; - - String? communityInputMethodSupportData; - - if (isEnableCommunityInputMethod && communityInputMethodData != null) { - final str = await downloadOrGetCachedCommunityInputMethodSupportFile( - communityInputMethodData); - if (str.trim().isNotEmpty) { - communityInputMethodSupportData = str; - } - } - - await Future.delayed(const Duration(milliseconds: 300)); - // check file - final globalIni = await compute(readArchive, savePath.absolute.path); - if (globalIni.isEmpty) { - throw S.current.localization_info_corrupted_file; - } - await installFormString( - globalIni, - value.versionName ?? "", - communityInputMethodSupportData: communityInputMethodSupportData, - communityInputMethodVersion: isEnableCommunityInputMethod - ? communityInputMethodData?.version - : null, - ); - } catch (e) { - if (!context.mounted) return; - await showToast( - context, S.current.localization_info_installation_error(e)); - if (await savePath.exists()) await savePath.delete(); + final savePath = + File("${_downloadDir.absolute.path}\\${value.versionName}.sclang"); + try { + state = state.copyWith(workingVersion: value.versionName!); + if (!await savePath.exists()) { + // download + await downloadLocalizationFile(savePath, value); + } else { + dPrint("use cache $savePath"); } - state = state.copyWith(workingVersion: ""); - }; + + final communityInputMethodData = state.communityInputMethodLanguageData; + + String? communityInputMethodSupportData; + + if (isEnableCommunityInputMethod && communityInputMethodData != null) { + final str = await downloadOrGetCachedCommunityInputMethodSupportFile( + communityInputMethodData); + if (str.trim().isNotEmpty) { + communityInputMethodSupportData = str; + } + } + + await Future.delayed(const Duration(milliseconds: 300)); + // check file + final globalIni = await compute(readArchive, savePath.absolute.path); + if (globalIni.isEmpty) { + throw S.current.localization_info_corrupted_file; + } + await installFormString( + globalIni, + value.versionName ?? "", + communityInputMethodSupportData: communityInputMethodSupportData, + communityInputMethodVersion: isEnableCommunityInputMethod + ? communityInputMethodData?.version + : null, + ); + } catch (e) { + if (!context.mounted) return; + await showToast( + context, S.current.localization_info_installation_error(e)); + if (await savePath.exists()) await savePath.delete(); + } + state = state.copyWith(workingVersion: ""); } Future downloadOrGetCachedCommunityInputMethodSupportFile( @@ -566,4 +564,109 @@ class LocalizationUIModel extends _$LocalizationUIModel { Future onChangeGameInstallPath(String value) async { await _loadData(); } + + Future onRemoteInsTall( + BuildContext context, + MapEntry item, + LocalizationUIState state) async { + bool enableCommunityInputMethod = + state.communityInputMethodLanguageData != null; + final userOK = await showConfirmDialogs( + context, + "${item.value.info}", + Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + S.current.localization_info_version_number( + item.value.versionName ?? ""), + style: TextStyle(color: Colors.white.withOpacity(.6)), + ), + const SizedBox(height: 4), + Text( + S.current + .localization_info_channel(item.value.gameChannel ?? ""), + style: TextStyle(color: Colors.white.withOpacity(.6)), + ), + const SizedBox(height: 4), + Text( + S.current + .localization_info_update_time(item.value.updateAt ?? ""), + style: TextStyle(color: Colors.white.withOpacity(.6)), + ), + const SizedBox(height: 12), + ], + ), + const SizedBox(height: 12), + Container( + decoration: BoxDecoration( + color: FluentTheme.of(context).cardColor, + borderRadius: BorderRadius.circular(7)), + padding: const EdgeInsets.all(12), + child: Row( + children: [ + Expanded( + child: Text( + item.value.note ?? S.current.home_localization_msg_no_note, + style: const TextStyle(fontSize: 15), + ), + ), + ], + ), + ), + SizedBox(height: 12), + Row( + children: [ + Text( + "安装社区输入法支持", + ), + Spacer(), + StatefulBuilder( + builder: (BuildContext context, + void Function(void Function()) setState) { + return ToggleSwitch( + checked: enableCommunityInputMethod, + onChanged: state.communityInputMethodLanguageData == null + ? null + : (v) { + enableCommunityInputMethod = v; + setState(() {}); + }, + ); + }, + ) + ], + ) + ], + ), + confirm: S.current.localization_action_install, + cancel: S.current.home_action_cancel, + constraints: + BoxConstraints(maxWidth: MediaQuery.of(context).size.width * .45), + ); + if (userOK) { + if (!context.mounted) return; + dPrint("doRemoteInstall ${item.value} $enableCommunityInputMethod"); + await doRemoteInstall(context, item.value, + isEnableCommunityInputMethod: enableCommunityInputMethod); + } + } + + Future checkReinstall(BuildContext context) async { + final installedVersion = state.patchStatus?.value; + if (installedVersion == null) return; + final curData = state.apiLocalizationData; + if (curData == null) return; + if (curData.keys.contains(installedVersion)) { + final data = curData[installedVersion]; + if (data != null) { + await onRemoteInsTall(context, MapEntry(installedVersion, data), state); + } + } + } }