-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: imple persistent localization storage
- Loading branch information
1 parent
619a160
commit e459684
Showing
8 changed files
with
88 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 32 additions & 3 deletions
35
lib/src/features/main/language/presentation/bloc/language_bloc.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters