forked from AlienKevin/wordshk_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.dart
252 lines (238 loc) · 8.51 KB
/
utils.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:flutter_web_browser/flutter_web_browser.dart';
import 'package:provider/provider.dart';
import 'package:wordshk/bridge_generated.dart';
import 'package:wordshk/constants.dart';
import 'package:wordshk/models/entry_language.dart';
import 'package:wordshk/models/pronunciation_method.dart';
import 'package:wordshk/models/speech_rate.dart';
import 'package:wordshk/states/language_state.dart';
import 'package:wordshk/states/speech_recognition_state.dart';
import 'models/font_size.dart';
import 'models/language.dart';
void openLink(String url) async {
return FlutterWebBrowser.openWebPage(
url: url,
customTabsOptions: const CustomTabsOptions(
colorScheme: CustomTabsColorScheme.dark,
darkColorSchemeParams: CustomTabsColorSchemeParams(
toolbarColor: blueColor,
secondaryToolbarColor: whiteColor,
navigationBarColor: whiteColor,
navigationBarDividerColor: greyColor,
),
shareState: CustomTabsShareState.on,
instantAppsEnabled: true,
showTitle: true,
urlBarHidingEnabled: true,
),
safariVCOptions: const SafariViewControllerOptions(
barCollapsingEnabled: true,
preferredBarTintColor: blueColor,
preferredControlTintColor: whiteColor,
dismissButtonStyle: SafariViewControllerDismissButtonStyle.close,
modalPresentationCapturesStatusBarAppearance: true,
),
);
}
switchKeyboardType(FocusNode focusNode) {
focusNode.unfocus();
WidgetsBinding.instance.addPostFrameCallback(
(_) => focusNode.requestFocus(),
);
}
String getRomanizationName(Romanization romanization, AppLocalizations s) {
switch (romanization) {
case Romanization.Jyutping:
return s.romanizationJyutping;
case Romanization.YaleNumbers:
return s.romanizationYaleNumbers;
case Romanization.YaleDiacritics:
return s.romanizationYaleDiacritics;
case Romanization.CantonesePinyin:
return s.romanizationCantonesePinyin;
case Romanization.Guangdong:
return s.romanizationGuangdong;
case Romanization.SidneyLau:
return "SidneyLau is deprecated";
case Romanization.Ipa:
return s.romanizationIpa;
}
}
String getRomanizationDescription(
Romanization romanization, AppLocalizations s) {
switch (romanization) {
case Romanization.Jyutping:
return s.romanizationJyutpingDescription;
case Romanization.YaleNumbers:
return s.romanizationYaleNumbersDescription;
case Romanization.YaleDiacritics:
return s.romanizationYaleDiacriticsDescription;
case Romanization.CantonesePinyin:
return s.romanizationCantonesePinyinDescription;
case Romanization.Guangdong:
return s.romanizationGuangdongDescription;
case Romanization.Ipa:
return s.romanizationIpaDescription;
case Romanization.SidneyLau:
return "SidneyLau is deprecated";
}
}
String getRomanizationShortName(
Romanization romanization, AppLocalizations s, Language language) {
if (language == Language.en) {
switch (romanization) {
case Romanization.Jyutping:
return s.romanizationJyutpingShort;
case Romanization.YaleNumbers:
return s.romanizationYaleNumbersShort;
case Romanization.YaleDiacritics:
return s.romanizationYaleDiacriticsShort;
case Romanization.CantonesePinyin:
return s.romanizationCantonesePinyinShort;
case Romanization.Guangdong:
return s.romanizationGuangdongShort;
case Romanization.SidneyLau:
return "SidneyLau is deprecated";
case Romanization.Ipa:
return s.romanizationIpaShort;
}
} else {
switch (romanization) {
case Romanization.Jyutping:
return s.romanizationJyutping;
case Romanization.YaleNumbers:
return s.romanizationYaleNumbers;
case Romanization.YaleDiacritics:
return s.romanizationYaleDiacritics;
case Romanization.CantonesePinyin:
return s.romanizationCantonesePinyin;
case Romanization.Guangdong:
return s.romanizationGuangdong;
case Romanization.SidneyLau:
return "SidneyLau is deprecated";
case Romanization.Ipa:
return s.romanizationIpa;
}
}
}
String getEntryLanguageName(EntryLanguage language, AppLocalizations s) {
switch (language) {
case EntryLanguage.both:
return s.entryLanguageBoth;
case EntryLanguage.cantonese:
return s.entryLanguageCantonese;
case EntryLanguage.english:
return s.entryLanguageEnglish;
}
}
String getFontSizeName(FontSize size, AppLocalizations s) {
switch (size) {
case FontSize.small:
return s.fontSizeSmall;
case FontSize.medium:
return s.fontSizeMedium;
case FontSize.large:
return s.fontSizeLarge;
case FontSize.veryLarge:
return s.fontSizeVeryLarge;
}
}
String getPronunciationMethodName(
PronunciationMethod method, AppLocalizations s) {
switch (method) {
case PronunciationMethod.tts:
return s.pronunciationMethodTts;
case PronunciationMethod.syllableRecordings:
return s.pronunciationMethodSyllableRecordings;
}
}
String getPronunciationMethodShortName(
PronunciationMethod method, AppLocalizations s, Language language) {
if (language == Language.en) {
switch (method) {
case PronunciationMethod.tts:
return s.pronunciationMethodTtsShort;
case PronunciationMethod.syllableRecordings:
return s.pronunciationMethodSyllableRecordingsShort;
}
} else {
switch (method) {
case PronunciationMethod.tts:
return s.pronunciationMethodTts;
case PronunciationMethod.syllableRecordings:
return s.pronunciationMethodSyllableRecordings;
}
}
}
Script getScript(BuildContext context) =>
context.read<LanguageState>().language == Language.zhHansCN
? Script.Simplified
: Script.Traditional;
void showSpeechRecognitionDialog(BuildContext context) async {
final state = context.read<SpeechRecognitionState>();
await state.startListening(getScript(context));
showPlatformDialog(
context: context,
builder: (context) {
final state = context.watch<SpeechRecognitionState>();
state.setCloseDialog(() {
// print("close speech recognition dialog");
Navigator.pop(context, false);
});
final s = AppLocalizations.of(context)!;
final recognitionFinished =
state.speechToText.hasRecognized || !state.isDialogOpen;
return PlatformAlertDialog(
title: Text(s.speakNow(s.entryLanguageCantonese)),
content: state.isAvailable
? (state.lastError != null
? (state.lastError!.errorMsg == "error_no_match"
? Text(s.speechRecognitionNoMatch)
: Text(s.errorInSpeechRecognition))
: (state.speechToText.isListening
? Text(s.listening)
: recognitionFinished
? Text(s.speechRecognitionFinished)
: Text(s.loadingRecognitionEngine)))
: Text(Platform.isAndroid
? s.speechRecognitionNotAvailableAndroid
: s.speechRecognitionNotAvailableIos),
actions: (state.isAvailable &&
(state.speechToText.isListening || recognitionFinished)
? <Widget>[]
: <Widget>[
PlatformDialogAction(
child: PlatformText(s.tryAgain),
onPressed: () {
state.cancelListening();
Navigator.pop(context, false);
// restart listening
showSpeechRecognitionDialog(context);
})
]) +
[
PlatformDialogAction(
child: PlatformText(s.done),
onPressed: () {
state.cancelListening();
// print("close speech recognition dialog");
Navigator.pop(context, false);
}),
],
);
});
}
String getSpeechRateName(SpeechRate rate, AppLocalizations s) {
switch (rate) {
case SpeechRate.verySlow:
return s.speechRateVerySlow;
case SpeechRate.slow:
return s.speechRateSlow;
case SpeechRate.normal:
return s.speechRateNormal;
}
}