From ed588e0ec7247500c97d2538e947851dd9a8c2ba Mon Sep 17 00:00:00 2001 From: Achintha Isuru Date: Wed, 28 Jan 2026 10:09:43 -0500 Subject: [PATCH] refactor: improve locale management and initialization in LocaleBloc and LocalizationModule --- apps/mobile/apps/staff/lib/main.dart | 36 ++++++++++--------- .../lib/src/bloc/locale_bloc.dart | 16 ++++++--- .../lib/src/localization_module.dart | 10 +++--- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/apps/mobile/apps/staff/lib/main.dart b/apps/mobile/apps/staff/lib/main.dart index 92770719..52919e9a 100644 --- a/apps/mobile/apps/staff/lib/main.dart +++ b/apps/mobile/apps/staff/lib/main.dart @@ -36,28 +36,30 @@ class AppWidget extends StatelessWidget { Widget build(BuildContext context) { return BlocProvider( create: (BuildContext context) => - Modular.get() - ..add(const core_localization.LoadLocale()), + Modular.get(), child: BlocBuilder< core_localization.LocaleBloc, core_localization.LocaleState >( - builder: (BuildContext context, core_localization.LocaleState state) { - return core_localization.TranslationProvider( - child: MaterialApp.router( - title: "KROW Staff", - theme: UiTheme.light, - routerConfig: Modular.routerConfig, - locale: state.locale, - supportedLocales: state.supportedLocales, - localizationsDelegates: const >[ - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, - ], - )); - }, + builder: + (BuildContext context, core_localization.LocaleState state) { + return core_localization.TranslationProvider( + child: MaterialApp.router( + title: "KROW Staff", + theme: UiTheme.light, + routerConfig: Modular.routerConfig, + locale: state.locale, + supportedLocales: state.supportedLocales, + localizationsDelegates: + const >[ + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + ), + ); + }, ), ); } diff --git a/apps/mobile/packages/core_localization/lib/src/bloc/locale_bloc.dart b/apps/mobile/packages/core_localization/lib/src/bloc/locale_bloc.dart index 5ae60907..27b3c4ce 100644 --- a/apps/mobile/packages/core_localization/lib/src/bloc/locale_bloc.dart +++ b/apps/mobile/packages/core_localization/lib/src/bloc/locale_bloc.dart @@ -11,23 +11,29 @@ import 'locale_state.dart'; /// It coordinates the flow between user language requests and persistent storage /// using [SetLocaleUseCase] and [GetLocaleUseCase]. class LocaleBloc extends Bloc { - final GetLocaleUseCase getLocaleUseCase; - final SetLocaleUseCase setLocaleUseCase; - /// Creates a [LocaleBloc] with the required use cases. LocaleBloc({required this.getLocaleUseCase, required this.setLocaleUseCase}) : super(LocaleState.initial()) { on(_onChangeLocale); on(_onLoadLocale); + + /// Initial event + add(const LoadLocale()); } + /// Use case for retrieving the saved locale. + final GetLocaleUseCase getLocaleUseCase; + + /// Use case for saving the selected locale. + final SetLocaleUseCase setLocaleUseCase; + /// Handles the [ChangeLocale] event by saving it via the use case and emitting new state. Future _onChangeLocale( ChangeLocale event, Emitter emit, ) async { // 1. Update slang settings - LocaleSettings.setLocaleRaw(event.locale.languageCode); + await LocaleSettings.setLocaleRaw(event.locale.languageCode); // 2. Persist using Use Case await setLocaleUseCase(event.locale); @@ -47,7 +53,7 @@ class LocaleBloc extends Bloc { Emitter emit, ) async { final Locale? savedLocale = await getLocaleUseCase(); - final Locale locale = const Locale('es'); + final Locale locale = savedLocale ?? const Locale('es'); LocaleSettings.setLocaleRaw(locale.languageCode); diff --git a/apps/mobile/packages/core_localization/lib/src/localization_module.dart b/apps/mobile/packages/core_localization/lib/src/localization_module.dart index bbc87c6d..e292120f 100644 --- a/apps/mobile/packages/core_localization/lib/src/localization_module.dart +++ b/apps/mobile/packages/core_localization/lib/src/localization_module.dart @@ -18,25 +18,25 @@ class LocalizationModule extends Module { i.addInstance(SharedPreferencesAsync()); // Data Sources - i.addSingleton( + i.addLazySingleton( () => LocaleLocalDataSourceImpl(i.get()), ); // Repositories - i.addSingleton( + i.addLazySingleton( () => LocaleRepositoryImpl(i.get()), ); // Use Cases - i.addSingleton( + i.addLazySingleton( () => GetLocaleUseCase(i.get()), ); - i.addSingleton( + i.addLazySingleton( () => SetLocaleUseCase(i.get()), ); // BLoCs - i.addSingleton( + i.add( () => LocaleBloc( getLocaleUseCase: i.get(), setLocaleUseCase: i.get(),