From 88ba31049eb3a718db3e1ee5e8e85f56689d94ba Mon Sep 17 00:00:00 2001 From: Ali Akbar <116874053+aliakbar-deriv@users.noreply.github.com> Date: Wed, 3 Apr 2024 11:23:41 +0400 Subject: [PATCH] feat(deriv_language_selector): aliakbar/1586/set_device_lang_as_default_if_app_supports (#540) --- .../lib/src/cubits/language_cubit.dart | 5 +- .../lib/src/services/language_service.dart | 70 +++++++++++++------ 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/packages/deriv_language_selector/lib/src/cubits/language_cubit.dart b/packages/deriv_language_selector/lib/src/cubits/language_cubit.dart index 3caaf0633..c0952f8bb 100644 --- a/packages/deriv_language_selector/lib/src/cubits/language_cubit.dart +++ b/packages/deriv_language_selector/lib/src/cubits/language_cubit.dart @@ -1,8 +1,9 @@ import 'dart:async'; + import 'package:deriv_language_selector/deriv_language_selector.dart'; +import 'package:deriv_ui/deriv_ui.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:deriv_ui/deriv_ui.dart'; part 'language_state.dart'; @@ -35,7 +36,7 @@ class LanguageCubit extends Cubit { } /// Updates the active languages and emits [LanguageLoadedState]. - /// [languageService.languages] is changed incase active languages are + /// [languageService.languages] is changed in case active languages are /// coming from a stream. Future updateActiveLanguages() async { emit(LanguageLoadedState( diff --git a/packages/deriv_language_selector/lib/src/services/language_service.dart b/packages/deriv_language_selector/lib/src/services/language_service.dart index cb899cf7f..71679626a 100644 --- a/packages/deriv_language_selector/lib/src/services/language_service.dart +++ b/packages/deriv_language_selector/lib/src/services/language_service.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:deriv_language_selector/deriv_language_selector.dart'; import 'package:deriv_ui/deriv_ui.dart'; import 'package:flutter/material.dart'; @@ -9,22 +11,32 @@ class LanguageService implements BaseLanguageService { required this.languageRepository, required this.languageDataSource, this.supportedLanguages, - }); + }) { + _languages = _generateLanguages( + [], + supportedLanguages ?? defaultLanguages, + ); + } - /// Instantance of [BaseLanguageRepository]. + /// Instance of [BaseLanguageRepository]. final BaseLanguageRepository languageRepository; - /// Instantance of [BaseLanguageDataSource]. + /// Instance of [BaseLanguageDataSource]. final BaseLanguageDataSource languageDataSource; /// List of supported languages. final List? supportedLanguages; - List _languages = - _generateLanguages([], defaultLanguages); + late List _languages; /// Default language of the app. - LanguageModel get defaultLanguage => _languages.first; + LanguageModel get defaultLanguage => _languages.firstWhere( + (LanguageModel language) => language.code == platformLanguage, + orElse: () => _languages.first, + ); + + /// System/Device language code. + String get platformLanguage => Platform.localeName.split('_').first; /// List of active languages. List get languages => _languages; @@ -34,10 +46,12 @@ class LanguageService implements BaseLanguageService { final String? code = await languageDataSource.getLanguage(); if (code == null) { - return _languages.first; + return defaultLanguage; } else { - return _languages - .firstWhere((LanguageModel element) => element.code == code); + return _languages.firstWhere( + (LanguageModel element) => element.code == code, + orElse: () => defaultLanguage, + ); } } @@ -49,17 +63,17 @@ class LanguageService implements BaseLanguageService { final List activeLanguages = (await languageRepository.getSupportedLanguagesFromServer( onLanguageFetched: (List value) { - _setLanguges(value, + _setLanguages(value, localLanguages); // useful for stream (refer to Deriv Go's getSupportedLanguagesFromServer method) }, )) ?? []; - _setLanguges(activeLanguages, localLanguages); + _setLanguages(activeLanguages, localLanguages); } /// Set the active languages. - void _setLanguges( + void _setLanguages( List activeLanguages, List localLanguages) { _languages = _generateLanguages( localLanguages @@ -84,15 +98,25 @@ class LanguageService implements BaseLanguageService { static List _generateLanguages( List activeLanguages, List localLanguages, - ) => - localLanguages - .where((LanguageEntity language) => - activeLanguages.isEmpty || - activeLanguages - .where((LanguageEntity element) => - element.locale == language.locale) - .isNotEmpty) - .map((LanguageEntity activeLanguage) => activeLanguage.toModel( - 'assets/icons/flags/ic_flag_${activeLanguage.locale.languageCode}.png')) - .toList(); + ) { + // Local/scoped function to convert a LanguageEntity to a LanguageModel. + LanguageModel convertToModel(LanguageEntity language) => language.toModel( + 'assets/icons/flags/ic_flag_${language.locale.languageCode}.png'); + + // If there are no active languages, convert all local languages to models. + if (activeLanguages.isEmpty) { + return localLanguages.map(convertToModel).toList(); + } + + // Prepare a set of active languages based on locale for efficient lookup. + final Set activeLanguagesSet = activeLanguages + .map((LanguageEntity language) => language.locale) + .toSet(); + + return localLanguages + .where((LanguageEntity language) => + activeLanguagesSet.contains(language.locale)) + .map(convertToModel) + .toList(); + } }