2021-08-07 22:34:44 +02:00
|
|
|
import 'package:bloc/bloc.dart';
|
2021-11-19 19:29:56 +01:00
|
|
|
import 'package:kiwi/kiwi.dart';
|
2021-08-07 22:34:44 +02:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
2022-01-27 11:02:45 +01:00
|
|
|
import 'package:nc_photos/bloc/bloc_util.dart' as bloc_util;
|
2021-08-07 22:34:44 +02:00
|
|
|
import 'package:nc_photos/entity/sharee.dart';
|
|
|
|
import 'package:nc_photos/entity/sharee/data_source.dart';
|
|
|
|
|
|
|
|
abstract class ListShareeBlocEvent {
|
|
|
|
const ListShareeBlocEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ListShareeBlocQuery extends ListShareeBlocEvent {
|
|
|
|
const ListShareeBlocQuery(this.account);
|
|
|
|
|
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
|
|
|
"account: $account, "
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class ListShareeBlocState {
|
|
|
|
const ListShareeBlocState(this.account, this.items);
|
|
|
|
|
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
|
|
|
"account: $account, "
|
|
|
|
"items: List {length: ${items.length}}, "
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
final Account? account;
|
|
|
|
final List<Sharee> items;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ListShareeBlocInit extends ListShareeBlocState {
|
|
|
|
ListShareeBlocInit() : super(null, const []);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ListShareeBlocLoading extends ListShareeBlocState {
|
|
|
|
const ListShareeBlocLoading(Account? account, List<Sharee> items)
|
|
|
|
: super(account, items);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ListShareeBlocSuccess extends ListShareeBlocState {
|
|
|
|
const ListShareeBlocSuccess(Account? account, List<Sharee> items)
|
|
|
|
: super(account, items);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ListShareeBlocFailure extends ListShareeBlocState {
|
|
|
|
const ListShareeBlocFailure(
|
|
|
|
Account? account, List<Sharee> items, this.exception)
|
|
|
|
: super(account, items);
|
|
|
|
|
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
|
|
|
"super: ${super.toString()}, "
|
|
|
|
"exception: $exception, "
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
final dynamic exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List all sharees of this account
|
|
|
|
class ListShareeBloc extends Bloc<ListShareeBlocEvent, ListShareeBlocState> {
|
|
|
|
ListShareeBloc() : super(ListShareeBlocInit());
|
|
|
|
|
2021-11-19 19:29:56 +01:00
|
|
|
static ListShareeBloc of(Account account) {
|
2022-01-27 11:02:45 +01:00
|
|
|
final name = bloc_util.getInstNameForAccount("ListShareeBloc", account);
|
2021-11-19 19:29:56 +01:00
|
|
|
try {
|
2022-01-27 11:02:45 +01:00
|
|
|
_log.fine("[of] Resolving bloc for '$name'");
|
|
|
|
return KiwiContainer().resolve<ListShareeBloc>(name);
|
2021-11-19 19:29:56 +01:00
|
|
|
} catch (_) {
|
|
|
|
// no created instance for this account, make a new one
|
|
|
|
_log.info("[of] New bloc instance for account: $account");
|
|
|
|
final bloc = ListShareeBloc();
|
2022-01-27 11:02:45 +01:00
|
|
|
KiwiContainer().registerInstance<ListShareeBloc>(bloc, name: name);
|
2021-11-19 19:29:56 +01:00
|
|
|
return bloc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:34:44 +02:00
|
|
|
@override
|
|
|
|
mapEventToState(ListShareeBlocEvent event) async* {
|
|
|
|
_log.info("[mapEventToState] $event");
|
|
|
|
if (event is ListShareeBlocQuery) {
|
|
|
|
yield* _onEventQuery(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream<ListShareeBlocState> _onEventQuery(ListShareeBlocQuery ev) async* {
|
|
|
|
try {
|
|
|
|
yield ListShareeBlocLoading(ev.account, state.items);
|
|
|
|
yield ListShareeBlocSuccess(ev.account, await _query(ev));
|
|
|
|
} catch (e, stackTrace) {
|
2021-08-21 11:59:26 +02:00
|
|
|
_log.shout("[_onEventQuery] Exception while request", e, stackTrace);
|
2021-08-07 22:34:44 +02:00
|
|
|
yield ListShareeBlocFailure(ev.account, state.items, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Sharee>> _query(ListShareeBlocQuery ev) {
|
|
|
|
final shareeRepo = ShareeRepo(ShareeRemoteDataSource());
|
|
|
|
return shareeRepo.list(ev.account);
|
|
|
|
}
|
|
|
|
|
|
|
|
static final _log = Logger("bloc.list_sharee.ListShareeBloc");
|
|
|
|
}
|