2023-06-06 15:39:58 +02:00
|
|
|
part of '../language_settings.dart';
|
|
|
|
|
|
|
|
@npLog
|
2023-07-28 18:48:50 +02:00
|
|
|
class _Bloc extends Bloc<_Event, _State> with BlocLogger {
|
2023-06-06 15:39:58 +02:00
|
|
|
_Bloc({
|
|
|
|
required this.prefController,
|
|
|
|
}) : super(_State.init(
|
|
|
|
selected: prefController.language.value,
|
|
|
|
)) {
|
|
|
|
on<_Init>(_onInit);
|
|
|
|
on<_SelectLanguage>(_onSelectLanguage);
|
|
|
|
on<_SetError>(_onSetError);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get tag => _log.fullName;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onError(Object error, StackTrace stackTrace) {
|
|
|
|
// we need this to prevent onError being triggered recursively
|
|
|
|
if (!isClosed && !_isHandlingError) {
|
|
|
|
_isHandlingError = true;
|
|
|
|
try {
|
|
|
|
add(_SetError(error, stackTrace));
|
|
|
|
} catch (_) {}
|
|
|
|
_isHandlingError = false;
|
|
|
|
}
|
|
|
|
super.onError(error, stackTrace);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _onInit(_Init ev, Emitter<_State> emit) {
|
|
|
|
_log.info(ev);
|
|
|
|
return emit.forEach<language_util.AppLanguage>(
|
|
|
|
prefController.language,
|
|
|
|
onData: (data) => state.copyWith(
|
|
|
|
selected: data,
|
|
|
|
),
|
|
|
|
onError: (e, stackTrace) {
|
|
|
|
_log.severe("[_onInit] Uncaught exception", e, stackTrace);
|
|
|
|
return state.copyWith(
|
|
|
|
error: ExceptionEvent(e, stackTrace),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSelectLanguage(_SelectLanguage ev, Emitter<_State> emit) {
|
|
|
|
_log.info(ev);
|
|
|
|
prefController.setAppLanguage(ev.lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSetError(_SetError ev, Emitter<_State> emit) {
|
|
|
|
_log.info(ev);
|
|
|
|
emit(state.copyWith(error: ExceptionEvent(ev.error, ev.stackTrace)));
|
|
|
|
}
|
|
|
|
|
|
|
|
final PrefController prefController;
|
|
|
|
|
|
|
|
var _isHandlingError = false;
|
|
|
|
}
|