nc-photos/app/lib/widget/settings/expert/bloc.dart

62 lines
1.6 KiB
Dart
Raw Normal View History

2023-05-28 19:11:34 +02:00
part of '../expert_settings.dart';
class _Error {
const _Error(this.ev, [this.error, this.stackTrace]);
final _Event ev;
final Object? error;
final StackTrace? stackTrace;
}
@npLog
2024-08-05 19:22:46 +02:00
class _Bloc extends Bloc<_Event, _State>
with BlocLogger, BlocForEachMixin<_Event, _State> {
2024-10-18 19:47:25 +02:00
_Bloc({
required this.db,
2024-08-05 19:22:46 +02:00
required this.prefController,
2024-10-18 19:47:25 +02:00
}) : super(_State.init(
2024-08-05 19:22:46 +02:00
isNewHttpEngine: prefController.isNewHttpEngineValue,
)) {
on<_Init>(_onInit);
2023-05-28 19:11:34 +02:00
on<_ClearCacheDatabase>(_onClearCacheDatabase);
2024-08-05 19:22:46 +02:00
on<_SetNewHttpEngine>(_onSetNewHttpEngine);
2023-05-28 19:11:34 +02:00
}
2023-07-28 18:48:50 +02:00
@override
String get tag => _log.fullName;
2023-05-28 19:11:34 +02:00
Stream<_Error> errorStream() => _errorStream.stream;
2024-08-05 19:22:46 +02:00
Future<void> _onInit(_Init ev, Emitter<_State> emit) async {
_log.info(ev);
return forEach(
emit,
prefController.isNewHttpEngineChange,
onData: (data) => state.copyWith(isNewHttpEngine: data),
);
}
2023-05-28 19:11:34 +02:00
Future<void> _onClearCacheDatabase(
_ClearCacheDatabase ev, Emitter<_State> emit) async {
2024-08-05 19:22:46 +02:00
_log.info(ev);
2023-05-28 19:11:34 +02:00
try {
2024-10-18 19:47:25 +02:00
final accounts = prefController.accountsValue;
await db.clearAndInitWithAccounts(accounts.toDb());
2023-05-28 19:11:34 +02:00
emit(state.copyWith(lastSuccessful: ev));
} catch (e, stackTrace) {
_log.shout("[_onClearCacheDatabase] Uncaught exception", e, stackTrace);
_errorStream.add(_Error(ev, e, stackTrace));
}
}
2024-08-05 19:22:46 +02:00
void _onSetNewHttpEngine(_SetNewHttpEngine ev, Emitter<_State> emit) {
_log.info(ev);
prefController.setNewHttpEngine(ev.value);
}
final NpDb db;
2024-08-05 19:22:46 +02:00
final PrefController prefController;
2023-05-28 19:11:34 +02:00
final _errorStream = StreamController<_Error>.broadcast();
}