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 {
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),
],

View File

@ -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<bool> doInstall() async {
Future<bool> 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<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) =>
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;
}

View File

@ -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<StringBuffer?>(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;
},
)
],
)
],
],
],
),

View File

@ -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;