initializeSettingsBloc method
Future<FastAppSettingsBlocState>
initializeSettingsBloc(
- BuildContext context, {
- IFastErrorReporter? errorReporter,
Initializes the FastAppSettingsBloc by retrieving the settings from the FastAppSettingsBloc and returns the FastAppSettingsBlocState.
Implementation
Future<FastAppSettingsBlocState> initializeSettingsBloc(
BuildContext context, {
IFastErrorReporter? errorReporter,
}) async {
final appInfoBloc = FastAppInfoBloc.instance;
final settingsBloc = FastAppSettingsBloc.instance;
final appInfoState = appInfoBloc.currentState;
final deviceLanguageCode = appInfoState.deviceLanguageCode;
final deviceCountryCode = appInfoState.deviceCountryCode;
final isFirstLaunch = appInfoBloc.currentState.isFirstLaunch;
final supportedLocales = appInfoBloc.currentState.supportedLocales;
late final String languageCode;
final String? countryCode;
await notifiyErrorReporterIfNeeded(
deviceLanguageCode,
deviceCountryCode,
errorReporter: errorReporter,
);
_logger
..info('Device language code', deviceLanguageCode)
..info('Device country code', deviceCountryCode)
..info('Is first launch', isFirstLaunch);
// We initialize the settings bloc.
settingsBloc.addEvent(const FastAppSettingsBlocEvent.init());
var settingsState = await RaceStream([
settingsBloc.onError,
settingsBloc.onData.where((state) => state.isInitialized),
]).first;
if (settingsState is! FastAppSettingsBlocState) {
_logger.error('Failed to initialize: $settingsState');
throw settingsState;
}
// Once the settings bloc is initialized, we determine the language code
// that will be used to initialize the language of the application.
if (isFirstLaunch) {
// If it is the first launch of the application, we use the device locale
// to initialize the language of the application.
languageCode = await determineUserLanguageCode(
deviceLanguageCode,
supportedLocales,
);
countryCode = deviceCountryCode;
} else {
// If it is not the first launch of the application, we use the saved
// language code to initialize the language of the application.
languageCode = settingsBloc.currentState.languageCode;
countryCode = settingsBloc.currentState.countryCode;
}
_logger.info('User language code', languageCode);
// We update the language code of the application if needed.
if (languageCode != settingsBloc.currentState.languageCode) {
_logger.debug('Updating User language code setting...');
settingsBloc.addEvent(
FastAppSettingsBlocEvent.languageCodeChanged(languageCode),
);
settingsState = await settingsBloc.onData
.where((state) => state.languageCode == languageCode)
.first;
_logger.debug('User language code setting updated');
}
if (countryCode != null) {
_logger.info('User country code', countryCode);
// We update the country code of the application if needed.
if (countryCode != settingsBloc.currentState.countryCode) {
_logger.debug('Updating User country code setting...');
settingsBloc.addEvent(
FastAppSettingsBlocEvent.countryCodeChanged(countryCode),
);
settingsState = await settingsBloc.onData
.where((state) => state.countryCode == countryCode)
.first;
_logger.debug('User country code setting updated');
}
}
final use24HourFormat = await shouldUse24HourFormat(context);
_logger
..info('Use 24 hour format', use24HourFormat)
..debug('Updating use 24 hour format setting...');
// We update the use 24 hour format of the application if needed.
settingsBloc.addEvent(
FastAppSettingsBlocEvent.use24HourFormatChanged(use24HourFormat),
);
settingsState = await settingsBloc.onData
.where((state) => state.use24HourFormat == use24HourFormat)
.first;
_logger.debug('Use 24 hour format setting updated');
return settingsState;
}