2023-04-13 17:32:31 +02:00
|
|
|
part of '../collection_picker.dart';
|
|
|
|
|
|
|
|
@npLog
|
2023-07-28 18:48:50 +02:00
|
|
|
class _Bloc extends Bloc<_Event, _State> with BlocLogger {
|
2023-04-13 17:32:31 +02:00
|
|
|
_Bloc({
|
|
|
|
required this.account,
|
|
|
|
required this.controller,
|
|
|
|
}) : super(_State.init()) {
|
|
|
|
on<_LoadCollections>(_onLoad);
|
|
|
|
on<_TransformItems>(_onTransformItems);
|
|
|
|
on<_SelectCollection>(_onSelectCollection);
|
2023-05-20 13:07:07 +02:00
|
|
|
|
2023-04-13 17:32:31 +02:00
|
|
|
on<_SetError>(_onSetError);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get tag => _log.fullName;
|
|
|
|
|
2023-05-20 13:07:07 +02:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2023-04-13 17:32:31 +02:00
|
|
|
Future<void> _onLoad(_LoadCollections ev, Emitter<_State> emit) async {
|
2023-04-29 05:20:39 +02:00
|
|
|
_log.info(ev);
|
2023-04-13 17:32:31 +02:00
|
|
|
return emit.forEach<CollectionStreamEvent>(
|
|
|
|
controller.stream,
|
|
|
|
onData: (data) => state.copyWith(
|
|
|
|
collections: data.data.map((e) => e.collection).toList(),
|
|
|
|
isLoading: data.hasNext,
|
|
|
|
),
|
|
|
|
onError: (e, stackTrace) {
|
|
|
|
_log.severe("[_onLoad] Uncaught exception", e, stackTrace);
|
|
|
|
return state.copyWith(
|
|
|
|
isLoading: false,
|
|
|
|
error: ExceptionEvent(e, stackTrace),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onTransformItems(_TransformItems ev, Emitter<_State> emit) {
|
2023-04-29 05:20:39 +02:00
|
|
|
_log.info(ev);
|
2023-04-13 17:32:31 +02:00
|
|
|
final transformed = _transformCollections(ev.collections);
|
|
|
|
emit(state.copyWith(transformedItems: transformed));
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSelectCollection(_SelectCollection ev, Emitter<_State> emit) {
|
2023-04-29 05:20:39 +02:00
|
|
|
_log.info(ev);
|
2023-04-13 17:32:31 +02:00
|
|
|
emit(state.copyWith(result: ev.collection));
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSetError(_SetError ev, Emitter<_State> emit) {
|
2023-04-29 05:20:39 +02:00
|
|
|
_log.info(ev);
|
2023-04-13 17:32:31 +02:00
|
|
|
emit(state.copyWith(error: ExceptionEvent(ev.error, ev.stackTrace)));
|
|
|
|
}
|
|
|
|
|
|
|
|
List<_Item> _transformCollections(List<Collection> collections) {
|
2023-04-29 05:20:39 +02:00
|
|
|
final sorted = collections
|
|
|
|
.where((c) => CollectionAdapter.of(
|
|
|
|
KiwiContainer().resolve<DiContainer>(), account, c)
|
|
|
|
.isPermitted(CollectionCapability.manualItem))
|
|
|
|
.sortedBy(collection_util.CollectionSort.dateDescending);
|
2023-04-13 17:32:31 +02:00
|
|
|
return sorted.map((c) => _Item(c)).toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
final CollectionsController controller;
|
2023-05-20 13:07:07 +02:00
|
|
|
|
|
|
|
var _isHandlingError = false;
|
2023-04-13 17:32:31 +02:00
|
|
|
}
|