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.
|
|
import 'package:data/app_setting_data/repository/app_setting_box_repository_impl.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:local_db_core/lib/boxes/box_list/setting_box/app_setting_box.dart'; import 'package:repositories/app_setting_box_domain/repository/app_setting_box_repository.dart'; import 'package:sonnat/core/language/languages.dart'; import 'package:sonnat/core/language/translator.dart'; import 'package:sonnat/core/utils/base_cubit_type.dart';
enum LanguageState { loading, loaded, notSet, }
enum CurrentLanguage { en('en'), fa('fa'), ar('ar');
final String value;
const CurrentLanguage(this.value); }
class LanguageCubit extends Cubit<BaseCubitType<LanguageState>> { final AppSettingBoxRepository _repository = AppSettingBoxRepositoryImpl(appSettingBox: AppSettingBox());
LanguageCubit() : super(BaseCubitType(eventName: LanguageState.loading)) { _initial(); }
Future<void> _initial() async { String? language = _repository.getCurrentLanguage(); if (language == null || language == '') { emit(BaseCubitType(eventName: LanguageState.notSet, data: CurrentLanguage.en)); return; } switch (language) { case 'fa': await Translator.setNewLanguage(Languages.fa); emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.fa)); return; case 'en': await Translator.setNewLanguage(Languages.en); emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.en)); return; case 'ar': await Translator.setNewLanguage(Languages.ar); emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.ar)); return; } }
String getCurrentLanguage() { String? language = _repository.getCurrentLanguage(); if (language == null || language == '') { return 'fa'; } return language; }
Future<void> changeLanguage(String language) async { switch (language) { case 'fa': await Translator.setNewLanguage(Languages.fa); emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.fa)); return; case 'en': await Translator.setNewLanguage(Languages.en); emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.en)); return; case 'ar': await Translator.setNewLanguage(Languages.ar); emit(BaseCubitType(eventName: LanguageState.loaded, data: CurrentLanguage.ar)); return; } } }
|