forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome_provider.dart
More file actions
350 lines (318 loc) · 11.5 KB
/
Copy pathhome_provider.dart
File metadata and controls
350 lines (318 loc) · 11.5 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import 'package:omi/utils/platform/platform_manager.dart';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:omi/backend/http/api/speech_profile.dart';
import 'package:omi/backend/http/api/users.dart';
import 'package:omi/backend/preferences.dart';
import 'package:omi/app_globals.dart';
import 'package:omi/pages/settings/language_selection_dialog.dart';
import 'package:omi/providers/user_provider.dart';
import 'package:omi/utils/logger.dart';
class HomeProvider extends ChangeNotifier {
int _sessionGeneration = 0;
int selectedIndex = 0;
Function(int idx)? onSelectedIndexChanged;
final FocusNode chatFieldFocusNode = FocusNode();
final FocusNode appsSearchFieldFocusNode = FocusNode();
final FocusNode convoSearchFieldFocusNode = FocusNode();
final FocusNode memoriesSearchFieldFocusNode = FocusNode();
bool isAppsSearchFieldFocused = false;
bool isChatFieldFocused = false;
bool isConvoSearchFieldFocused = false;
bool isMemoriesSearchFieldFocused = false;
bool showConvoSearchBar = false;
bool hasSpeakerProfile = true;
bool isLoading = false;
String userPrimaryLanguage = SharedPreferencesUtil().userPrimaryLanguage;
bool hasSetPrimaryLanguage = SharedPreferencesUtil().hasSetPrimaryLanguage;
/// Offline floor for the served list. Not dead code — a first run with no
/// network shows this.
static const Map<String, String> _bundledLanguages = {
// Top languages first
'English': 'en',
'English (US)': 'en-US',
'English (UK)': 'en-GB',
'English (Australia)': 'en-AU',
'English (New Zealand)': 'en-NZ',
'English (India)': 'en-IN',
'Spanish': 'es',
'Spanish (Latin America)': 'es-419',
'Chinese (Mandarin, Simplified)': 'zh',
'Chinese (Mandarin, Simplified, CN)': 'zh-CN',
'Chinese (Mandarin, Simplified, Hans)': 'zh-Hans',
'Hindi': 'hi',
'Portuguese': 'pt',
'Portuguese (Brazil)': 'pt-BR',
'Portuguese (Portugal)': 'pt-PT',
'Russian': 'ru',
'Japanese': 'ja',
'German': 'de',
// Other languages alphabetically
'Arabic': 'ar',
'Belarusian': 'be',
'Bengali': 'bn',
'Bosnian': 'bs',
'Bulgarian': 'bg',
'Catalan': 'ca',
'Chinese (Mandarin, Traditional)': 'zh-TW',
'Chinese (Mandarin, Traditional, Hant)': 'zh-Hant',
'Chinese (Cantonese, Traditional)': 'zh-HK',
'Croatian': 'hr',
'Czech': 'cs',
'Danish': 'da',
'Danish (Denmark)': 'da-DK',
'Dutch': 'nl',
'Estonian': 'et',
'Finnish': 'fi',
'Flemish': 'nl-BE',
'French': 'fr',
'French (Canada)': 'fr-CA',
'German (Switzerland)': 'de-CH',
'Greek': 'el',
'Hebrew': 'he',
'Hungarian': 'hu',
'Indonesian': 'id',
'Italian': 'it',
'Kannada': 'kn',
'Korean': 'ko',
'Korean (Korea)': 'ko-KR',
'Latvian': 'lv',
'Lithuanian': 'lt',
'Macedonian': 'mk',
'Malay': 'ms',
'Marathi': 'mr',
'Norwegian': 'no',
'Persian': 'fa',
'Polish': 'pl',
'Romanian': 'ro',
'Serbian': 'sr',
'Slovak': 'sk',
'Slovenian': 'sl',
'Swedish': 'sv',
'Swedish (Sweden)': 'sv-SE',
'Tagalog': 'tl',
'Tamil': 'ta',
'Telugu': 'te',
'Thai': 'th',
'Thai (Thailand)': 'th-TH',
'Turkish': 'tr',
'Ukrainian': 'uk',
'Urdu': 'ur',
'Vietnamese': 'vi',
};
Map<String, String>? _serverLanguages;
Map<String, String> get availableLanguages => _serverLanguages ?? _bundledLanguages;
/// Cached list first so the UI never waits, then the server's. A failed fetch
/// leaves whatever the picker already had.
Future<void> loadAvailableLanguages({Future<Map<String, String>?> Function()? fetch}) async {
final cached = SharedPreferencesUtil().cachedAvailableLanguages;
if (cached.isNotEmpty) {
final restored = _decodeLanguages(cached);
if (restored != null) {
_serverLanguages = restored;
notifyListeners();
}
}
final generation = _sessionGeneration;
Map<String, String>? fetched;
try {
fetched = await (fetch ?? getAvailableLanguages)();
} catch (e) {
Logger.debug('Error loading available languages: $e');
return;
}
if (fetched == null || fetched.isEmpty || generation != _sessionGeneration) return;
SharedPreferencesUtil().cachedAvailableLanguages = jsonEncode(fetched);
_serverLanguages = fetched;
notifyListeners();
}
static Map<String, String>? _decodeLanguages(String raw) {
try {
final decoded = jsonDecode(raw);
if (decoded is! Map || decoded.isEmpty) return null;
return {for (final entry in decoded.entries) entry.key as String: entry.value as String};
} catch (_) {
return null;
}
}
HomeProvider() {
chatFieldFocusNode.addListener(_onFocusChange);
appsSearchFieldFocusNode.addListener(_onFocusChange);
convoSearchFieldFocusNode.addListener(_onConvoSearchFocusChange);
memoriesSearchFieldFocusNode.addListener(_onFocusChange);
}
void clearUserData() {
_sessionGeneration++;
selectedIndex = 0;
isAppsSearchFieldFocused = false;
isChatFieldFocused = false;
isConvoSearchFieldFocused = false;
isMemoriesSearchFieldFocused = false;
showConvoSearchBar = false;
hasSpeakerProfile = false;
isLoading = false;
userPrimaryLanguage = '';
hasSetPrimaryLanguage = false;
chatFieldFocusNode.unfocus();
appsSearchFieldFocusNode.unfocus();
convoSearchFieldFocusNode.unfocus();
memoriesSearchFieldFocusNode.unfocus();
notifyListeners();
}
void _onFocusChange() {
isChatFieldFocused = chatFieldFocusNode.hasFocus;
isAppsSearchFieldFocused = appsSearchFieldFocusNode.hasFocus;
isMemoriesSearchFieldFocused = memoriesSearchFieldFocusNode.hasFocus;
notifyListeners();
}
void _onConvoSearchFocusChange() {
isConvoSearchFieldFocused = convoSearchFieldFocusNode.hasFocus;
// Don't auto-hide search bar when focus is lost - the widget will handle visibility
// based on whether there's an active search query
notifyListeners();
}
void setIndex(int index) {
selectedIndex = index;
onSelectedIndexChanged?.call(index);
notifyListeners();
}
void toggleConvoSearchBar() {
showConvoSearchBar = !showConvoSearchBar;
if (showConvoSearchBar) {
// Focus the search field when showing the search bar
// Use a post-frame callback for reliability.
WidgetsBinding.instance.addPostFrameCallback((_) {
convoSearchFieldFocusNode.requestFocus();
});
} else {
// Clear search and unfocus when hiding
convoSearchFieldFocusNode.unfocus();
}
notifyListeners();
}
void hideConvoSearchBar() {
if (showConvoSearchBar) {
showConvoSearchBar = false;
convoSearchFieldFocusNode.unfocus();
notifyListeners();
}
}
void setIsLoading(bool loading) {
isLoading = loading;
notifyListeners();
}
void setSpeakerProfile(bool? value) {
hasSpeakerProfile = value ?? SharedPreferencesUtil().hasSpeakerProfile;
notifyListeners();
}
Future setupHasSpeakerProfile() async {
final generation = _sessionGeneration;
setIsLoading(true);
var res = await userHasSpeakerProfile();
if (generation != _sessionGeneration) return;
setSpeakerProfile(res);
SharedPreferencesUtil().hasSpeakerProfile = res;
Logger.debug('_setupHasSpeakerProfile: ${SharedPreferencesUtil().hasSpeakerProfile}');
PlatformManager.instance.analytics.setUserAttribute('Speaker Profile', SharedPreferencesUtil().hasSpeakerProfile);
setIsLoading(false);
notifyListeners();
}
/// Loads the picker options, then runs primary-language setup — but only if
/// the session survived the request. setupUserPrimaryLanguage captures the
/// generation when it starts, which is too late to notice a sign-out that
/// happened while the options were still loading.
Future<void> loadLanguagesThenSetupPrimary({Future<Map<String, String>?> Function()? fetch}) async {
final generation = _sessionGeneration;
await loadAvailableLanguages(fetch: fetch);
if (generation != _sessionGeneration) return;
await setupUserPrimaryLanguage();
}
Future<void> setupUserPrimaryLanguage() async {
if (SharedPreferencesUtil().hasSetPrimaryLanguage && SharedPreferencesUtil().userPrimaryLanguage.isNotEmpty) {
return;
}
final generation = _sessionGeneration;
try {
final language = await getUserPrimaryLanguage();
if (generation != _sessionGeneration) return;
if (language == null) {
// User hasn't set a primary language yet
userPrimaryLanguage = '';
hasSetPrimaryLanguage = false;
// Show language dialog after a short delay to ensure UI is ready
Future.delayed(const Duration(milliseconds: 500), () {
if (generation == _sessionGeneration && globalNavigatorKey.currentContext != null) {
showLanguageDialogIfNeeded(globalNavigatorKey.currentContext!);
}
});
} else {
userPrimaryLanguage = language;
hasSetPrimaryLanguage = true;
SharedPreferencesUtil().userPrimaryLanguage = language;
SharedPreferencesUtil().hasSetPrimaryLanguage = true;
PlatformManager.instance.analytics.setUserAttribute('Primary Language', language);
}
Logger.debug('setupUserPrimaryLanguage: $language, hasSet: $hasSetPrimaryLanguage');
} catch (e) {
if (generation != _sessionGeneration) return;
Logger.debug('Error setting up user primary language: $e');
userPrimaryLanguage = '';
hasSetPrimaryLanguage = false;
}
notifyListeners();
return;
}
void showLanguageDialogIfNeeded(BuildContext context) {
if (!hasSetPrimaryLanguage) {
LanguageSelectionDialog.show(context, isRequired: true);
}
}
Future<bool> updateUserPrimaryLanguage(String languageCode, {UserProvider? userProvider}) async {
try {
final serverSingleLanguageMode = await setUserPrimaryLanguage(languageCode);
if (serverSingleLanguageMode != null) {
userPrimaryLanguage = languageCode;
hasSetPrimaryLanguage = true;
SharedPreferencesUtil().userPrimaryLanguage = languageCode;
SharedPreferencesUtil().hasSetPrimaryLanguage = true;
PlatformManager.instance.analytics.setUserAttribute('Primary Language', languageCode);
// The server decides single_language_mode from the live STT policy
// (#10022); local state mirrors its response, never a client-side list.
userProvider?.updateSingleLanguageModeLocally(serverSingleLanguageMode);
notifyListeners();
return true;
}
return false;
} catch (e) {
Logger.debug('Error setting user primary language: $e');
return false;
}
}
String getLanguageName(String code) {
// A stored code can outlive the list that offered it.
for (final entry in availableLanguages.entries) {
if (entry.value == code) return entry.key;
}
for (final entry in _bundledLanguages.entries) {
if (entry.value == code) return entry.key;
}
return code;
}
Future setUserPeople() async {
SharedPreferencesUtil().cachedPeople = await getAllPeople();
notifyListeners();
}
@override
void dispose() {
chatFieldFocusNode.removeListener(_onFocusChange);
appsSearchFieldFocusNode.removeListener(_onFocusChange);
convoSearchFieldFocusNode.removeListener(_onConvoSearchFocusChange);
memoriesSearchFieldFocusNode.removeListener(_onFocusChange);
memoriesSearchFieldFocusNode.dispose();
chatFieldFocusNode.dispose();
appsSearchFieldFocusNode.dispose();
convoSearchFieldFocusNode.dispose();
onSelectedIndexChanged = null;
super.dispose();
}
}