import 'package:bloc/bloc.dart'; import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; import 'package:nc_photos/account.dart'; import 'package:nc_photos/entity/file.dart'; import 'package:nc_photos/entity/share.dart'; import 'package:nc_photos/entity/share/data_source.dart'; import 'package:np_codegen/np_codegen.dart'; import 'package:to_string/to_string.dart'; part 'list_share.g.dart'; abstract class ListShareBlocEvent { const ListShareBlocEvent(); } @toString class ListShareBlocQuery extends ListShareBlocEvent { const ListShareBlocQuery(this.account, this.file); @override String toString() => _$toString(); final Account account; final File file; } @toString abstract class ListShareBlocState { const ListShareBlocState(this.account, this.file, this.items); @override String toString() => _$toString(); final Account? account; final File file; final List items; } class ListShareBlocInit extends ListShareBlocState { ListShareBlocInit() : super(null, File(path: ""), const []); } class ListShareBlocLoading extends ListShareBlocState { const ListShareBlocLoading(Account? account, File file, List items) : super(account, file, items); } class ListShareBlocSuccess extends ListShareBlocState { const ListShareBlocSuccess(Account? account, File file, List items) : super(account, file, items); } @toString class ListShareBlocFailure extends ListShareBlocState { const ListShareBlocFailure( Account? account, File file, List items, this.exception) : super(account, file, items); @override String toString() => _$toString(); final dynamic exception; } /// List all shares from a given file @npLog class ListShareBloc extends Bloc { ListShareBloc() : super(ListShareBlocInit()) { on(_onEvent); } Future _onEvent( ListShareBlocEvent event, Emitter emit) async { _log.info("[_onEvent] $event"); if (event is ListShareBlocQuery) { await _onEventQuery(event, emit); } } Future _onEventQuery( ListShareBlocQuery ev, Emitter emit) async { try { emit(ListShareBlocLoading(ev.account, ev.file, state.items)); emit(ListShareBlocSuccess(ev.account, ev.file, await _query(ev))); } catch (e, stackTrace) { _log.severe("[_onEventQuery] Exception while request", e, stackTrace); emit(ListShareBlocFailure(ev.account, ev.file, state.items, e)); } } Future> _query(ListShareBlocQuery ev) { final shareRepo = ShareRepo(ShareRemoteDataSource()); return shareRepo.list(ev.account, ev.file); } }