refactor: improve locale management and initialization in LocaleBloc and LocalizationModule
This commit is contained in:
@@ -36,28 +36,30 @@ class AppWidget extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider<core_localization.LocaleBloc>(
|
||||
create: (BuildContext context) =>
|
||||
Modular.get<core_localization.LocaleBloc>()
|
||||
..add(const core_localization.LoadLocale()),
|
||||
Modular.get<core_localization.LocaleBloc>(),
|
||||
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 <LocalizationsDelegate<dynamic>>[
|
||||
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 <LocalizationsDelegate<dynamic>>[
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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<LocaleEvent, LocaleState> {
|
||||
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<ChangeLocale>(_onChangeLocale);
|
||||
on<LoadLocale>(_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<void> _onChangeLocale(
|
||||
ChangeLocale event,
|
||||
Emitter<LocaleState> 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<LocaleEvent, LocaleState> {
|
||||
Emitter<LocaleState> emit,
|
||||
) async {
|
||||
final Locale? savedLocale = await getLocaleUseCase();
|
||||
final Locale locale = const Locale('es');
|
||||
final Locale locale = savedLocale ?? const Locale('es');
|
||||
|
||||
LocaleSettings.setLocaleRaw(locale.languageCode);
|
||||
|
||||
|
||||
@@ -18,25 +18,25 @@ class LocalizationModule extends Module {
|
||||
i.addInstance<SharedPreferencesAsync>(SharedPreferencesAsync());
|
||||
|
||||
// Data Sources
|
||||
i.addSingleton<LocaleLocalDataSource>(
|
||||
i.addLazySingleton<LocaleLocalDataSource>(
|
||||
() => LocaleLocalDataSourceImpl(i.get<SharedPreferencesAsync>()),
|
||||
);
|
||||
|
||||
// Repositories
|
||||
i.addSingleton<LocaleRepositoryInterface>(
|
||||
i.addLazySingleton<LocaleRepositoryInterface>(
|
||||
() => LocaleRepositoryImpl(i.get<LocaleLocalDataSource>()),
|
||||
);
|
||||
|
||||
// Use Cases
|
||||
i.addSingleton<GetLocaleUseCase>(
|
||||
i.addLazySingleton<GetLocaleUseCase>(
|
||||
() => GetLocaleUseCase(i.get<LocaleRepositoryInterface>()),
|
||||
);
|
||||
i.addSingleton<SetLocaleUseCase>(
|
||||
i.addLazySingleton<SetLocaleUseCase>(
|
||||
() => SetLocaleUseCase(i.get<LocaleRepositoryInterface>()),
|
||||
);
|
||||
|
||||
// BLoCs
|
||||
i.addSingleton<LocaleBloc>(
|
||||
i.add<LocaleBloc>(
|
||||
() => LocaleBloc(
|
||||
getLocaleUseCase: i.get<GetLocaleUseCase>(),
|
||||
setLocaleUseCase: i.get<SetLocaleUseCase>(),
|
||||
|
||||
Reference in New Issue
Block a user