nc-photos/app/lib/widget/collection_picker/bloc.dart

60 lines
1.8 KiB
Dart

part of '../collection_picker.dart';
@npLog
class _Bloc extends Bloc<_Event, _State> implements BlocTag {
_Bloc({
required this.account,
required this.controller,
}) : super(_State.init()) {
on<_LoadCollections>(_onLoad);
on<_TransformItems>(_onTransformItems);
on<_SelectCollection>(_onSelectCollection);
on<_SetError>(_onSetError);
}
@override
String get tag => _log.fullName;
Future<void> _onLoad(_LoadCollections ev, Emitter<_State> emit) async {
_log.info("[_onLoad] $ev");
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) {
_log.info("[_onTransformItems] $ev");
final transformed = _transformCollections(ev.collections);
emit(state.copyWith(transformedItems: transformed));
}
void _onSelectCollection(_SelectCollection ev, Emitter<_State> emit) {
_log.info("[_onTransformItems] $ev");
emit(state.copyWith(result: ev.collection));
}
void _onSetError(_SetError ev, Emitter<_State> emit) {
_log.info("[_onSetError] $ev");
emit(state.copyWith(error: ExceptionEvent(ev.error, ev.stackTrace)));
}
List<_Item> _transformCollections(List<Collection> collections) {
final sorted =
collections.sortedBy(collection_util.CollectionSort.dateDescending);
return sorted.map((c) => _Item(c)).toList();
}
final Account account;
final CollectionsController controller;
}