From 1681e2407bf50baa1e578c18dc7a67b31b21dcb3 Mon Sep 17 00:00:00 2001 From: xkeyC <3334969096@qq.com> Date: Wed, 6 Nov 2024 20:15:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=AE=9A=E4=B9=89=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E9=AB=98=E7=BA=A7=E6=B1=89=E5=8C=96=20?= =?UTF-8?q?=E7=A4=BE=E5=8C=BA=E8=BE=93=E5=85=A5=E6=B3=95=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../advanced_localization_ui.dart | 11 ++- .../advanced_localization_ui_model.dart | 94 ++++++++++++++++++- .../localization/localization_dialog_ui.dart | 7 +- .../localization_form_file_dialog_ui.dart | 35 ++++++- .../localization/localization_ui_model.dart | 35 ++++--- 5 files changed, 153 insertions(+), 29 deletions(-) diff --git a/lib/ui/home/localization/advanced_localization_ui.dart b/lib/ui/home/localization/advanced_localization_ui.dart index 1ab010e..06c7276 100644 --- a/lib/ui/home/localization/advanced_localization_ui.dart +++ b/lib/ui/home/localization/advanced_localization_ui.dart @@ -28,10 +28,11 @@ class AdvancedLocalizationUI extends HookConsumerWidget { onSwitchFile() async { final sb = await showDialog( context: context, - builder: (BuildContext context) => const LocalizationFromFileDialogUI(), + builder: (BuildContext context) => + const LocalizationFromFileDialogUI(isInAdvancedMode: true), ); - if (sb is StringBuffer) { - model.setCustomizeGlobalIni(sb.toString()); + if (sb is (StringBuffer, bool)) { + model.setCustomizeGlobalIni(sb.$1.toString()); } } @@ -106,8 +107,8 @@ class AdvancedLocalizationUI extends HookConsumerWidget { child: Text(S.current .home_localization_advanced_action_install), ), - onPressed: () async { - await model.doInstall().unwrap(context: context); + onPressed: () { + model.onInstall(context); }), const SizedBox(width: 12), ], diff --git a/lib/ui/home/localization/advanced_localization_ui_model.dart b/lib/ui/home/localization/advanced_localization_ui_model.dart index 127d4de..e2d447b 100644 --- a/lib/ui/home/localization/advanced_localization_ui_model.dart +++ b/lib/ui/home/localization/advanced_localization_ui_model.dart @@ -1,15 +1,24 @@ import 'dart:async'; import 'dart:io'; +import 'package:fluent_ui/fluent_ui.dart'; import 'package:flutter/foundation.dart'; +import 'package:flutter/src/widgets/framework.dart'; +import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; +import 'package:re_editor/re_editor.dart'; +import 'package:re_highlight/languages/ini.dart'; +import 'package:re_highlight/styles/vs2015.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:starcitizen_doctor/api/analytics.dart'; +import 'package:starcitizen_doctor/common/utils/base_utils.dart'; import 'package:starcitizen_doctor/common/utils/log.dart'; import 'package:starcitizen_doctor/common/utils/provider.dart'; import 'package:starcitizen_doctor/data/app_advanced_localization_data.dart'; import 'package:starcitizen_doctor/data/sc_localization_data.dart'; import 'package:starcitizen_doctor/provider/unp4kc.dart'; +import 'package:starcitizen_doctor/widgets/widgets.dart'; import '../home_ui_model.dart'; import 'advanced_localization_ui.json.dart'; @@ -285,7 +294,7 @@ class AdvancedLocalizationUIModel extends _$AdvancedLocalizationUIModel { state = state.copyWith(classMap: classMap); } - Future doInstall() async { + Future doInstall({bool isEnableCommunityInputMethod = false}) async { AnalyticsApi.touch("advanced_localization_apply"); state = state.copyWith( workingText: @@ -302,10 +311,91 @@ class AdvancedLocalizationUIModel extends _$AdvancedLocalizationUIModel { workingText: S.current.home_localization_advanced_msg_gen_localization_install); final localizationUIModel = ref.read(localizationUIModelProvider.notifier); + await localizationUIModel.installFormString( globalIni, state.apiLocalizationData?.versionName ?? "-", - advanced: true); + advanced: true, + isEnableCommunityInputMethod: isEnableCommunityInputMethod); state = state.copyWith(workingText: ""); return true; } + + // ignore: avoid_build_context_in_providers + Future onInstall(BuildContext context) async { + var isEnableCommunityInputMethod = true; + final userOK = await showConfirmDialogs(context, "确认安装高级汉化?", HookConsumer( + builder: (BuildContext context, WidgetRef ref, Widget? child) { + final globalIni = useState(null); + final enableCommunityInputMethod = useState(true); + final localizationState = ref.read(localizationUIModelProvider); + useEffect(() { + () async { + final classMap = state.classMap!; + final g = StringBuffer(); + for (var item in classMap.values) { + for (var kv in item.valuesMap.entries) { + g.write("${kv.key}=${kv.value}\n"); + await Future.delayed(Duration.zero); + } + } + globalIni.value = g; + }(); + return null; + }, const []); + return Column( + children: [ + Expanded( + child: Container( + decoration: BoxDecoration( + color: FluentTheme.of(context).cardColor, + borderRadius: BorderRadius.circular(7), + ), + child: globalIni.value == null + ? makeLoading(context) + : CodeEditor( + readOnly: true, + controller: CodeLineEditingController.fromText( + globalIni.value!.toString()), + style: CodeEditorStyle( + codeTheme: CodeHighlightTheme( + languages: { + 'ini': CodeHighlightThemeMode(mode: langIni) + }, + theme: vs2015Theme, + ), + ), + ), + ), + ), + SizedBox(height: 16), + Row( + children: [ + Text( + "安装社区输入法支持", + ), + Spacer(), + ToggleSwitch( + checked: enableCommunityInputMethod.value, + onChanged: + localizationState.communityInputMethodLanguageData == null + ? null + : (v) { + isEnableCommunityInputMethod = v; + enableCommunityInputMethod.value = v; + }, + ) + ], + ) + ], + ); + }, + ), + constraints: BoxConstraints( + maxWidth: MediaQuery.of(context).size.width * .8, + )); + if (userOK) { + await doInstall( + isEnableCommunityInputMethod: isEnableCommunityInputMethod); + } + } } diff --git a/lib/ui/home/localization/localization_dialog_ui.dart b/lib/ui/home/localization/localization_dialog_ui.dart index 7a5e9a3..30c8805 100644 --- a/lib/ui/home/localization/localization_dialog_ui.dart +++ b/lib/ui/home/localization/localization_dialog_ui.dart @@ -467,9 +467,12 @@ class LocalizationDialogUI extends HookConsumerWidget { builder: (BuildContext context) => const LocalizationFromFileDialogUI(), ); - if (sb is StringBuffer) { + if (sb is (StringBuffer, bool)) { await model.installFormString( - sb, S.current.localization_info_custom_files); + sb.$1, + S.current.localization_info_custom_files, + isEnableCommunityInputMethod: sb.$2, + ); } break; } diff --git a/lib/ui/home/localization/localization_form_file_dialog_ui.dart b/lib/ui/home/localization/localization_form_file_dialog_ui.dart index 82672bc..7dd707b 100644 --- a/lib/ui/home/localization/localization_form_file_dialog_ui.dart +++ b/lib/ui/home/localization/localization_form_file_dialog_ui.dart @@ -6,14 +6,19 @@ import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:go_router/go_router.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:re_editor/re_editor.dart'; +import 'package:starcitizen_doctor/ui/home/localization/localization_ui_model.dart'; import 'package:starcitizen_doctor/widgets/widgets.dart'; class LocalizationFromFileDialogUI extends HookConsumerWidget { - const LocalizationFromFileDialogUI({super.key}); + final bool isInAdvancedMode; + + const LocalizationFromFileDialogUI( + {super.key, this.isInAdvancedMode = false}); @override Widget build(BuildContext context, WidgetRef ref) { final selectedStringBuffer = useState(null); + final enableCommunityInputMethod = useState(true); final isLoading = useState(false); void onSelectFile() async { final result = await FilePicker.platform.pickFiles( @@ -40,6 +45,8 @@ class LocalizationFromFileDialogUI extends HookConsumerWidget { return null; }, const []); + final localizationState = ref.watch(localizationUIModelProvider); + return ContentDialog( constraints: BoxConstraints( maxWidth: selectedStringBuffer.value == null @@ -67,7 +74,10 @@ class LocalizationFromFileDialogUI extends HookConsumerWidget { child: Text(S.current.app_common_tip_confirm), ), onPressed: () { - Navigator.pop(context, selectedStringBuffer.value); + Navigator.pop(context, ( + selectedStringBuffer.value, + enableCommunityInputMethod.value + )); }) ], ), @@ -123,6 +133,27 @@ class LocalizationFromFileDialogUI extends HookConsumerWidget { ), ), ), + if (!isInAdvancedMode) ...[ + SizedBox(height: 16), + Row( + children: [ + Text( + "安装社区输入法支持", + ), + Spacer(), + ToggleSwitch( + checked: enableCommunityInputMethod.value, + onChanged: + localizationState.communityInputMethodLanguageData == + null + ? null + : (v) { + enableCommunityInputMethod.value = v; + }, + ) + ], + ) + ], ], ], ), diff --git a/lib/ui/home/localization/localization_ui_model.dart b/lib/ui/home/localization/localization_ui_model.dart index 5e35172..febd3f1 100644 --- a/lib/ui/home/localization/localization_ui_model.dart +++ b/lib/ui/home/localization/localization_ui_model.dart @@ -237,8 +237,7 @@ class LocalizationUIModel extends _$LocalizationUIModel { StringBuffer globalIni, String versionName, { bool? advanced, - String? communityInputMethodVersion, - String? communityInputMethodSupportData, + bool isEnableCommunityInputMethod = false, }) async { dPrint("LocalizationUIModel -> installFormString $versionName"); final iniFile = File( @@ -247,6 +246,21 @@ class LocalizationUIModel extends _$LocalizationUIModel { if (!globalIni.toString().endsWith("\n")) { globalIni.write("\n"); } + String? communityInputMethodVersion; + String? communityInputMethodSupportData; + + if (isEnableCommunityInputMethod) { + final data = state.communityInputMethodLanguageData; + if (data != null) { + communityInputMethodVersion = data.version; + final str = + await downloadOrGetCachedCommunityInputMethodSupportFile(data); + if (str.trim().isNotEmpty) { + communityInputMethodSupportData = str; + } + } + } + if (communityInputMethodVersion != null) { globalIni.write( "_starcitizen_doctor_localization_community_input_method_version=$communityInputMethodVersion\n"); @@ -324,18 +338,6 @@ class LocalizationUIModel extends _$LocalizationUIModel { 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); @@ -345,10 +347,7 @@ class LocalizationUIModel extends _$LocalizationUIModel { await installFormString( globalIni, value.versionName ?? "", - communityInputMethodSupportData: communityInputMethodSupportData, - communityInputMethodVersion: isEnableCommunityInputMethod - ? communityInputMethodData?.version - : null, + isEnableCommunityInputMethod: isEnableCommunityInputMethod, ); } catch (e) { if (!context.mounted) return;