Sonnat Project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.5 KiB

2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
  1. import 'package:data/app_setting_data/repository/app_setting_box_repository_impl.dart';
  2. import 'package:flutter_bloc/flutter_bloc.dart';
  3. import 'package:local_db_core/lib/boxes/box_list/setting_box/app_setting_box.dart';
  4. import 'package:repositories/app_setting_box_domain/repository/app_setting_box_repository.dart';
  5. import 'package:sonnat/core/language/languages.dart';
  6. import 'package:sonnat/core/language/translator.dart';
  7. import 'package:sonnat/core/utils/base_cubit_type.dart';
  8. enum LanguageState {
  9. loading,
  10. loaded,
  11. notSet,
  12. }
  13. enum CurrentLanguage {
  14. en('en'),
  15. fa('fa'),
  16. ar('ar');
  17. final String value;
  18. const CurrentLanguage(this.value);
  19. }
  20. class LanguageCubit extends Cubit<BaseCubitType<LanguageState>> {
  21. final AppSettingBoxRepository _repository = AppSettingBoxRepositoryImpl(appSettingBox: AppSettingBox());
  22. LanguageCubit() : super(BaseCubitType(eventName: LanguageState.loading)) {
  23. _initial();
  24. }
  25. Future<void> _initial() async {
  26. String? language = _repository.getCurrentLanguage();
  27. if (language == null || language == '') {
  28. emit(BaseCubitType(eventName: LanguageState.notSet, data: CurrentLanguage.en));
  29. return;
  30. }
  31. switch (language) {
  32. case 'fa':
  33. await Translator.setNewLanguage(Languages.fa);
  34. emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.fa));
  35. return;
  36. case 'en':
  37. await Translator.setNewLanguage(Languages.en);
  38. emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.en));
  39. return;
  40. case 'ar':
  41. await Translator.setNewLanguage(Languages.ar);
  42. emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.ar));
  43. return;
  44. }
  45. }
  46. String getCurrentLanguage() {
  47. String? language = _repository.getCurrentLanguage();
  48. if (language == null || language == '') {
  49. return 'fa';
  50. }
  51. return language;
  52. }
  53. Future<void> changeLanguage(String language) async {
  54. switch (language) {
  55. case 'fa':
  56. await Translator.setNewLanguage(Languages.fa);
  57. emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.fa));
  58. return;
  59. case 'en':
  60. await Translator.setNewLanguage(Languages.en);
  61. emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.en));
  62. return;
  63. case 'ar':
  64. await Translator.setNewLanguage(Languages.ar);
  65. emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.ar));
  66. return;
  67. }
  68. }
  69. }