Skip to content

Commit

Permalink
feat: imple persistent localization storage
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixit99 committed Dec 25, 2024
1 parent 619a160 commit e459684
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 26 deletions.
14 changes: 12 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ import 'package:gui/src/core/constants/configurations.dart';
import 'package:gui/src/core/router/app_router.dart';
import 'package:gui/src/core/utils/gen/localization/codegen_loader.g.dart';
import 'package:gui/src/features/main/theme/bloc/theme_bloc.dart';
import 'src/core/services/shared_preferences_service.dart';
import 'src/features/main/language/presentation/bloc/language_bloc.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();

final sharedPreferencesService = await SharedPreferencesService.initialize();

runApp(
MultiBlocProvider(
providers: [
BlocProvider<LanguageBloc>(
create: (_) => LanguageBloc(),
create: (_) => LanguageBloc(sharedPreferencesService),
),
BlocProvider<ThemeBloc>(
create: (_) => ThemeBloc(),
),
],
child: MyApp(),
child: const MyApp(),
),
);
}
Expand All @@ -38,6 +43,11 @@ class MyApp extends StatelessWidget {
assetLoader: const CodegenLoader(),
child: BlocBuilder<LanguageBloc, LanguageState>(
builder: (context, languageState) {
if (context.locale != languageState.selectedLanguage.value) {
WidgetsBinding.instance.addPostFrameCallback((_) {
context.setLocale(languageState.selectedLanguage.value);
});
}
return BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, themeState) {
return MaterialApp.router(
Expand Down
16 changes: 0 additions & 16 deletions lib/presentation/bloc/language_bloc/language_bloc.dart

This file was deleted.

4 changes: 4 additions & 0 deletions lib/src/core/constants/app_constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class AppConstants {
static const String languagePrefsKey = 'languagePrefs';
static const String themePrefsKey = 'themePrefs';
}
26 changes: 26 additions & 0 deletions lib/src/core/services/shared_preferences_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:gui/src/core/constants/app_constants.dart';
import 'package:gui/src/features/main/language/data/language_model.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SharedPreferencesService {
SharedPreferencesService(this._preferences);
final SharedPreferences _preferences;

static Future<SharedPreferencesService> initialize() async {
final preferences = await SharedPreferences.getInstance();
return SharedPreferencesService(preferences);
}

String getSelectedLanguage() {
return _preferences.getString(AppConstants.languagePrefsKey) ??
Language.english.code;
}

Future<bool> setSelectedLanguage(String languageCode) {
return _preferences.setString(AppConstants.languagePrefsKey, languageCode);
}

Future<bool> saveSelectedLanguage(String languageCode) {
return setSelectedLanguage(languageCode);
}
}
5 changes: 5 additions & 0 deletions lib/src/features/main/language/data/language_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@ enum Language {
english(
Locale('en', 'US'),
'English',
'en',
),
spanish(
Locale('es', 'ES'),
'Español',
'es',
),
french(
Locale('fr', 'FR'),
'Français',
'fr',
);

const Language(
this.value,
this.text,
this.code,
);

final Locale value;
final String text;
final String code;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:gui/src/features/main/language/data/language_model.dart';
import '../../../../../core/services/shared_preferences_service.dart';

part 'language_event.dart';
part 'language_state.dart';

class LanguageBloc extends Bloc<LanguageEvent, LanguageState> {
LanguageBloc() : super(const LanguageState()) {
on<ChangeLanguage>(onChangeLanguage);
LanguageBloc(this._sharedPreferencesService) : super(const LanguageState()) {
on<InitializeLanguageEvent>(_onInitializeLanguage);
on<ChangeLanguage>(_onChangeLanguage);
add(InitializeLanguageEvent()); // Trigger initialization on creation
}
Future<void> onChangeLanguage(

final SharedPreferencesService _sharedPreferencesService;

Future<void> _onInitializeLanguage(
InitializeLanguageEvent event,
Emitter<LanguageState> emit,
) async {
final languageCode = _sharedPreferencesService.getSelectedLanguage();

final language = languageCode.isNotEmpty
? Language.values.firstWhere(
(lang) => lang.code == languageCode,
orElse: () => Language.english,
)
: Language.english;

await _sharedPreferencesService.saveSelectedLanguage(
language.code,
);
emit(state.copyWith(selectedLanguage: language));
}

Future<void> _onChangeLanguage(
ChangeLanguage event,
Emitter<LanguageState> emit,
) async {
await _sharedPreferencesService.saveSelectedLanguage(
event.selectedLanguage.code,
);
emit(state.copyWith(selectedLanguage: event.selectedLanguage));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ part of 'language_bloc.dart';

class LanguageState extends Equatable {
const LanguageState({
this.selectedLanguage = Language.english, // Default to English
this.selectedLanguage = Language.english,
});

final Language selectedLanguage;

@override
List<Object> get props => [selectedLanguage];

LanguageState copyWith({Language? selectedLanguage}) {
LanguageState copyWith({
Language? selectedLanguage,
bool? isInitialized,
}) {
return LanguageState(
selectedLanguage: selectedLanguage ?? this.selectedLanguage,
);
}

@override
List<Object?> get props => [selectedLanguage];
}

class LanguageInitial extends LanguageState {
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies:
go_router: ^14.6.1
intl: ^0.19.0
json_annotation: ^4.9.0
shared_preferences: ^2.3.4

dev_dependencies:
build_runner:
Expand Down

0 comments on commit e459684

Please sign in to comment.