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) {
|
Widget build(BuildContext context) {
|
||||||
return BlocProvider<core_localization.LocaleBloc>(
|
return BlocProvider<core_localization.LocaleBloc>(
|
||||||
create: (BuildContext context) =>
|
create: (BuildContext context) =>
|
||||||
Modular.get<core_localization.LocaleBloc>()
|
Modular.get<core_localization.LocaleBloc>(),
|
||||||
..add(const core_localization.LoadLocale()),
|
|
||||||
child:
|
child:
|
||||||
BlocBuilder<
|
BlocBuilder<
|
||||||
core_localization.LocaleBloc,
|
core_localization.LocaleBloc,
|
||||||
core_localization.LocaleState
|
core_localization.LocaleState
|
||||||
>(
|
>(
|
||||||
builder: (BuildContext context, core_localization.LocaleState state) {
|
builder:
|
||||||
return core_localization.TranslationProvider(
|
(BuildContext context, core_localization.LocaleState state) {
|
||||||
child: MaterialApp.router(
|
return core_localization.TranslationProvider(
|
||||||
title: "KROW Staff",
|
child: MaterialApp.router(
|
||||||
theme: UiTheme.light,
|
title: "KROW Staff",
|
||||||
routerConfig: Modular.routerConfig,
|
theme: UiTheme.light,
|
||||||
locale: state.locale,
|
routerConfig: Modular.routerConfig,
|
||||||
supportedLocales: state.supportedLocales,
|
locale: state.locale,
|
||||||
localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
|
supportedLocales: state.supportedLocales,
|
||||||
GlobalMaterialLocalizations.delegate,
|
localizationsDelegates:
|
||||||
GlobalWidgetsLocalizations.delegate,
|
const <LocalizationsDelegate<dynamic>>[
|
||||||
GlobalCupertinoLocalizations.delegate,
|
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
|
/// It coordinates the flow between user language requests and persistent storage
|
||||||
/// using [SetLocaleUseCase] and [GetLocaleUseCase].
|
/// using [SetLocaleUseCase] and [GetLocaleUseCase].
|
||||||
class LocaleBloc extends Bloc<LocaleEvent, LocaleState> {
|
class LocaleBloc extends Bloc<LocaleEvent, LocaleState> {
|
||||||
final GetLocaleUseCase getLocaleUseCase;
|
|
||||||
final SetLocaleUseCase setLocaleUseCase;
|
|
||||||
|
|
||||||
/// Creates a [LocaleBloc] with the required use cases.
|
/// Creates a [LocaleBloc] with the required use cases.
|
||||||
LocaleBloc({required this.getLocaleUseCase, required this.setLocaleUseCase})
|
LocaleBloc({required this.getLocaleUseCase, required this.setLocaleUseCase})
|
||||||
: super(LocaleState.initial()) {
|
: super(LocaleState.initial()) {
|
||||||
on<ChangeLocale>(_onChangeLocale);
|
on<ChangeLocale>(_onChangeLocale);
|
||||||
on<LoadLocale>(_onLoadLocale);
|
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.
|
/// Handles the [ChangeLocale] event by saving it via the use case and emitting new state.
|
||||||
Future<void> _onChangeLocale(
|
Future<void> _onChangeLocale(
|
||||||
ChangeLocale event,
|
ChangeLocale event,
|
||||||
Emitter<LocaleState> emit,
|
Emitter<LocaleState> emit,
|
||||||
) async {
|
) async {
|
||||||
// 1. Update slang settings
|
// 1. Update slang settings
|
||||||
LocaleSettings.setLocaleRaw(event.locale.languageCode);
|
await LocaleSettings.setLocaleRaw(event.locale.languageCode);
|
||||||
|
|
||||||
// 2. Persist using Use Case
|
// 2. Persist using Use Case
|
||||||
await setLocaleUseCase(event.locale);
|
await setLocaleUseCase(event.locale);
|
||||||
@@ -47,7 +53,7 @@ class LocaleBloc extends Bloc<LocaleEvent, LocaleState> {
|
|||||||
Emitter<LocaleState> emit,
|
Emitter<LocaleState> emit,
|
||||||
) async {
|
) async {
|
||||||
final Locale? savedLocale = await getLocaleUseCase();
|
final Locale? savedLocale = await getLocaleUseCase();
|
||||||
final Locale locale = const Locale('es');
|
final Locale locale = savedLocale ?? const Locale('es');
|
||||||
|
|
||||||
LocaleSettings.setLocaleRaw(locale.languageCode);
|
LocaleSettings.setLocaleRaw(locale.languageCode);
|
||||||
|
|
||||||
|
|||||||
@@ -18,25 +18,25 @@ class LocalizationModule extends Module {
|
|||||||
i.addInstance<SharedPreferencesAsync>(SharedPreferencesAsync());
|
i.addInstance<SharedPreferencesAsync>(SharedPreferencesAsync());
|
||||||
|
|
||||||
// Data Sources
|
// Data Sources
|
||||||
i.addSingleton<LocaleLocalDataSource>(
|
i.addLazySingleton<LocaleLocalDataSource>(
|
||||||
() => LocaleLocalDataSourceImpl(i.get<SharedPreferencesAsync>()),
|
() => LocaleLocalDataSourceImpl(i.get<SharedPreferencesAsync>()),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Repositories
|
// Repositories
|
||||||
i.addSingleton<LocaleRepositoryInterface>(
|
i.addLazySingleton<LocaleRepositoryInterface>(
|
||||||
() => LocaleRepositoryImpl(i.get<LocaleLocalDataSource>()),
|
() => LocaleRepositoryImpl(i.get<LocaleLocalDataSource>()),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Use Cases
|
// Use Cases
|
||||||
i.addSingleton<GetLocaleUseCase>(
|
i.addLazySingleton<GetLocaleUseCase>(
|
||||||
() => GetLocaleUseCase(i.get<LocaleRepositoryInterface>()),
|
() => GetLocaleUseCase(i.get<LocaleRepositoryInterface>()),
|
||||||
);
|
);
|
||||||
i.addSingleton<SetLocaleUseCase>(
|
i.addLazySingleton<SetLocaleUseCase>(
|
||||||
() => SetLocaleUseCase(i.get<LocaleRepositoryInterface>()),
|
() => SetLocaleUseCase(i.get<LocaleRepositoryInterface>()),
|
||||||
);
|
);
|
||||||
|
|
||||||
// BLoCs
|
// BLoCs
|
||||||
i.addSingleton<LocaleBloc>(
|
i.add<LocaleBloc>(
|
||||||
() => LocaleBloc(
|
() => LocaleBloc(
|
||||||
getLocaleUseCase: i.get<GetLocaleUseCase>(),
|
getLocaleUseCase: i.get<GetLocaleUseCase>(),
|
||||||
setLocaleUseCase: i.get<SetLocaleUseCase>(),
|
setLocaleUseCase: i.get<SetLocaleUseCase>(),
|
||||||
|
|||||||
Reference in New Issue
Block a user