feat: 自定义文件,高级汉化 社区输入法支持

This commit is contained in:
xkeyC 2024-11-06 20:15:49 +08:00
parent 49cef9ed90
commit 1681e2407b
5 changed files with 153 additions and 29 deletions

View File

@ -28,10 +28,11 @@ class AdvancedLocalizationUI extends HookConsumerWidget {
onSwitchFile() async { onSwitchFile() async {
final sb = await showDialog( final sb = await showDialog(
context: context, context: context,
builder: (BuildContext context) => const LocalizationFromFileDialogUI(), builder: (BuildContext context) =>
const LocalizationFromFileDialogUI(isInAdvancedMode: true),
); );
if (sb is StringBuffer) { if (sb is (StringBuffer, bool)) {
model.setCustomizeGlobalIni(sb.toString()); model.setCustomizeGlobalIni(sb.$1.toString());
} }
} }
@ -106,8 +107,8 @@ class AdvancedLocalizationUI extends HookConsumerWidget {
child: Text(S.current child: Text(S.current
.home_localization_advanced_action_install), .home_localization_advanced_action_install),
), ),
onPressed: () async { onPressed: () {
await model.doInstall().unwrap(context: context); model.onInstall(context);
}), }),
const SizedBox(width: 12), const SizedBox(width: 12),
], ],

View File

@ -1,15 +1,24 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/foundation.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: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:riverpod_annotation/riverpod_annotation.dart';
import 'package:starcitizen_doctor/api/analytics.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/log.dart';
import 'package:starcitizen_doctor/common/utils/provider.dart'; import 'package:starcitizen_doctor/common/utils/provider.dart';
import 'package:starcitizen_doctor/data/app_advanced_localization_data.dart'; import 'package:starcitizen_doctor/data/app_advanced_localization_data.dart';
import 'package:starcitizen_doctor/data/sc_localization_data.dart'; import 'package:starcitizen_doctor/data/sc_localization_data.dart';
import 'package:starcitizen_doctor/provider/unp4kc.dart'; import 'package:starcitizen_doctor/provider/unp4kc.dart';
import 'package:starcitizen_doctor/widgets/widgets.dart';
import '../home_ui_model.dart'; import '../home_ui_model.dart';
import 'advanced_localization_ui.json.dart'; import 'advanced_localization_ui.json.dart';
@ -285,7 +294,7 @@ class AdvancedLocalizationUIModel extends _$AdvancedLocalizationUIModel {
state = state.copyWith(classMap: classMap); state = state.copyWith(classMap: classMap);
} }
Future<bool> doInstall() async { Future<bool> doInstall({bool isEnableCommunityInputMethod = false}) async {
AnalyticsApi.touch("advanced_localization_apply"); AnalyticsApi.touch("advanced_localization_apply");
state = state.copyWith( state = state.copyWith(
workingText: workingText:
@ -302,10 +311,91 @@ class AdvancedLocalizationUIModel extends _$AdvancedLocalizationUIModel {
workingText: workingText:
S.current.home_localization_advanced_msg_gen_localization_install); S.current.home_localization_advanced_msg_gen_localization_install);
final localizationUIModel = ref.read(localizationUIModelProvider.notifier); final localizationUIModel = ref.read(localizationUIModelProvider.notifier);
await localizationUIModel.installFormString( await localizationUIModel.installFormString(
globalIni, state.apiLocalizationData?.versionName ?? "-", globalIni, state.apiLocalizationData?.versionName ?? "-",
advanced: true); advanced: true,
isEnableCommunityInputMethod: isEnableCommunityInputMethod);
state = state.copyWith(workingText: ""); state = state.copyWith(workingText: "");
return true; return true;
} }
// ignore: avoid_build_context_in_providers
Future<void> onInstall(BuildContext context) async {
var isEnableCommunityInputMethod = true;
final userOK = await showConfirmDialogs(context, "确认安装高级汉化?", HookConsumer(
builder: (BuildContext context, WidgetRef ref, Widget? child) {
final globalIni = useState<StringBuffer?>(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);
}
}
} }

View File

@ -467,9 +467,12 @@ class LocalizationDialogUI extends HookConsumerWidget {
builder: (BuildContext context) => builder: (BuildContext context) =>
const LocalizationFromFileDialogUI(), const LocalizationFromFileDialogUI(),
); );
if (sb is StringBuffer) { if (sb is (StringBuffer, bool)) {
await model.installFormString( await model.installFormString(
sb, S.current.localization_info_custom_files); sb.$1,
S.current.localization_info_custom_files,
isEnableCommunityInputMethod: sb.$2,
);
} }
break; break;
} }

View File

@ -6,14 +6,19 @@ import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:re_editor/re_editor.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'; import 'package:starcitizen_doctor/widgets/widgets.dart';
class LocalizationFromFileDialogUI extends HookConsumerWidget { class LocalizationFromFileDialogUI extends HookConsumerWidget {
const LocalizationFromFileDialogUI({super.key}); final bool isInAdvancedMode;
const LocalizationFromFileDialogUI(
{super.key, this.isInAdvancedMode = false});
@override @override
Widget build(BuildContext context, WidgetRef ref) { Widget build(BuildContext context, WidgetRef ref) {
final selectedStringBuffer = useState<StringBuffer?>(null); final selectedStringBuffer = useState<StringBuffer?>(null);
final enableCommunityInputMethod = useState(true);
final isLoading = useState(false); final isLoading = useState(false);
void onSelectFile() async { void onSelectFile() async {
final result = await FilePicker.platform.pickFiles( final result = await FilePicker.platform.pickFiles(
@ -40,6 +45,8 @@ class LocalizationFromFileDialogUI extends HookConsumerWidget {
return null; return null;
}, const []); }, const []);
final localizationState = ref.watch(localizationUIModelProvider);
return ContentDialog( return ContentDialog(
constraints: BoxConstraints( constraints: BoxConstraints(
maxWidth: selectedStringBuffer.value == null maxWidth: selectedStringBuffer.value == null
@ -67,7 +74,10 @@ class LocalizationFromFileDialogUI extends HookConsumerWidget {
child: Text(S.current.app_common_tip_confirm), child: Text(S.current.app_common_tip_confirm),
), ),
onPressed: () { 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;
},
)
],
)
],
], ],
], ],
), ),

View File

@ -237,8 +237,7 @@ class LocalizationUIModel extends _$LocalizationUIModel {
StringBuffer globalIni, StringBuffer globalIni,
String versionName, { String versionName, {
bool? advanced, bool? advanced,
String? communityInputMethodVersion, bool isEnableCommunityInputMethod = false,
String? communityInputMethodSupportData,
}) async { }) async {
dPrint("LocalizationUIModel -> installFormString $versionName"); dPrint("LocalizationUIModel -> installFormString $versionName");
final iniFile = File( final iniFile = File(
@ -247,6 +246,21 @@ class LocalizationUIModel extends _$LocalizationUIModel {
if (!globalIni.toString().endsWith("\n")) { if (!globalIni.toString().endsWith("\n")) {
globalIni.write("\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) { if (communityInputMethodVersion != null) {
globalIni.write( globalIni.write(
"_starcitizen_doctor_localization_community_input_method_version=$communityInputMethodVersion\n"); "_starcitizen_doctor_localization_community_input_method_version=$communityInputMethodVersion\n");
@ -324,18 +338,6 @@ class LocalizationUIModel extends _$LocalizationUIModel {
dPrint("use cache $savePath"); 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)); await Future.delayed(const Duration(milliseconds: 300));
// check file // check file
final globalIni = await compute(readArchive, savePath.absolute.path); final globalIni = await compute(readArchive, savePath.absolute.path);
@ -345,10 +347,7 @@ class LocalizationUIModel extends _$LocalizationUIModel {
await installFormString( await installFormString(
globalIni, globalIni,
value.versionName ?? "", value.versionName ?? "",
communityInputMethodSupportData: communityInputMethodSupportData, isEnableCommunityInputMethod: isEnableCommunityInputMethod,
communityInputMethodVersion: isEnableCommunityInputMethod
? communityInputMethodData?.version
: null,
); );
} catch (e) { } catch (e) {
if (!context.mounted) return; if (!context.mounted) return;