Skip to content

Commit

Permalink
Remove system default language option
Browse files Browse the repository at this point in the history
  • Loading branch information
elibon99 committed Jan 28, 2025
1 parent a62c4b7 commit c11fa2f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 69 deletions.
2 changes: 1 addition & 1 deletion lib/app/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class YubicoAuthenticatorApp extends StatelessWidget {
themeMode: ref.watch(themeModeProvider),
home: page,
debugShowCheckedModeBanner: false,
locale: ref.watch(currentLocaleProvider).locale,
locale: ref.watch(currentLocaleProvider),
supportedLocales: AppLocalizations.supportedLocales,
localizationsDelegates: const [
AppLocalizations.delegate,
Expand Down
46 changes: 22 additions & 24 deletions lib/app/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,50 +70,48 @@ final supportedThemesProvider = StateProvider<List<ThemeMode>>(
(ref) => throw UnimplementedError(),
);

final supportedLocalesProvider = Provider<List<Locale>>((_) {
final officialLocalesSet = officialLocales.toSet();
final allLocalesSet = AppLocalizations.supportedLocales.toSet();

// Ensure supported locales are in correct priority order
// (english has highest priority)
final supportedLocales = {...officialLocalesSet, ...allLocalesSet}.toList();
return supportedLocales;
});

final currentLocaleProvider =
StateNotifierProvider<CurrentLocaleProvider, AppLocale>(
(ref) => CurrentLocaleProvider(ref.watch(prefProvider)),
StateNotifierProvider<CurrentLocaleProvider, Locale>(
(ref) => CurrentLocaleProvider(
ref.watch(prefProvider), ref.read(supportedLocalesProvider)),
);

class CurrentLocaleProvider extends StateNotifier<AppLocale> {
class CurrentLocaleProvider extends StateNotifier<Locale> {
static const String _key = 'APP_LOCALE';
final SharedPreferences _prefs;

CurrentLocaleProvider(this._prefs) : super(_fromName(_prefs.getString(_key)));
CurrentLocaleProvider(this._prefs, List<Locale> supportedLocales)
: super(_fromName(_prefs.getString(_key), supportedLocales));

void setLocale(Locale locale) {
_log.debug('Set locale to $locale');
state = AppLocale(locale, false);
state = locale;
_prefs.setString(_key, locale.languageCode);
}

void resetLocale() {
_log.debug('Resetting locale to system default');
state = _getDefaultLocale();
_prefs.remove(_key);
}

static AppLocale _getDefaultLocale() => AppLocale(
basicLocaleListResolution(PlatformDispatcher.instance.locales,
AppLocalizations.supportedLocales),
true,
);

static AppLocale _fromName(String? localeStr) {
static Locale _fromName(String? localeStr, List<Locale> supportedLocales) {
if (localeStr != null) {
// Force locale
final locale = Locale(localeStr, '');
return AppLocale(
basicLocaleListResolution([locale], AppLocalizations.supportedLocales),
false,
);
return basicLocaleListResolution([locale], supportedLocales);
}
return _getDefaultLocale();
return basicLocaleListResolution(
PlatformDispatcher.instance.locales, supportedLocales);
}
}

final l10nProvider = Provider<AppLocalizations>(
(ref) => lookupAppLocalizations(ref.watch(currentLocaleProvider).locale),
(ref) => lookupAppLocalizations(ref.watch(currentLocaleProvider)),
);

final themeModeProvider = StateNotifierProvider<ThemeModeNotifier, ThemeMode>(
Expand Down
75 changes: 32 additions & 43 deletions lib/app/views/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import '../../core/state.dart';
import '../../widgets/list_title.dart';
import '../../widgets/responsive_dialog.dart';
import '../icon_provider/icon_pack_dialog.dart';
import '../models.dart';
import '../state.dart';
import 'keys.dart' as keys;

Expand Down Expand Up @@ -96,57 +95,47 @@ class _ThemeModeView extends ConsumerWidget {
class _LanguageView extends ConsumerWidget {
const _LanguageView();

void _selectLocale(
Future<Locale> _selectLocale(
BuildContext context,
WidgetRef ref,
AppLocale currentLocale,
) async {
final groupValue =
currentLocale.systemDefault ? null : currentLocale.locale;
await showDialog(
context: context,
builder: (context) {
final l10n = AppLocalizations.of(context)!;
return SimpleDialog(
title: Text('Choose language'),
children: [
RadioListTile(
title: Text(l10n.s_system_default),
value: null,
groupValue: groupValue,
toggleable: true,
onChanged: (_) {
ref.read(currentLocaleProvider.notifier).resetLocale();
Navigator.pop(context);
},
),
...AppLocalizations.supportedLocales.map(
(e) => RadioListTile(
title: Text(e.getDisplayName(l10n)),
value: e,
groupValue: groupValue,
toggleable: true,
onChanged: (value) {
ref.read(currentLocaleProvider.notifier).setLocale(e);
Navigator.pop(context);
},
),
)
],
);
},
);
}
List<Locale> supportedLocales,
Locale currentLocale,
) async =>
await showDialog<Locale>(
context: context,
builder: (context) {
final l10n = AppLocalizations.of(context)!;
return SimpleDialog(
title: Text(l10n.s_choose_language),
children: supportedLocales
.map(
(e) => RadioListTile(
title: Text(e.getDisplayName(l10n)),
value: e,
groupValue: currentLocale,
toggleable: true,
onChanged: (value) {
Navigator.pop(context, e);
},
),
)
.toList(),
);
}) ??
currentLocale;

@override
Widget build(BuildContext context, WidgetRef ref) {
final l10n = AppLocalizations.of(context)!;
final currentLocale = ref.watch(currentLocaleProvider);
return ListTile(
title: Text(l10n.s_language),
subtitle: Text(currentLocale.locale.getDisplayName(l10n)),
subtitle: Text(currentLocale.getDisplayName(l10n)),
key: keys.languageSetting,
onTap: () => _selectLocale(context, ref, currentLocale),
onTap: () async {
final newLocale = await _selectLocale(
context, ref.read(supportedLocalesProvider), currentLocale);
ref.read(currentLocaleProvider.notifier).setLocale(newLocale);
},
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/piv/views/cert_info_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CertInfoTable extends ConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final l10n = AppLocalizations.of(context)!;
final dateFormat =
DateFormat.yMMMEd(ref.watch(currentLocaleProvider).locale.toString());
DateFormat.yMMMEd(ref.watch(currentLocaleProvider).toString());

final certInfo = this.certInfo;
final metadata = this.metadata;
Expand Down

0 comments on commit c11fa2f

Please sign in to comment.