nc-photos/app/lib/bloc/list_person.dart

117 lines
3.2 KiB
Dart
Raw Normal View History

import 'package:bloc/bloc.dart';
2021-10-07 07:25:09 +02:00
import 'package:kiwi/kiwi.dart';
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;
import 'package:nc_photos/entity/person.dart';
import 'package:nc_photos/entity/person/data_source.dart';
abstract class ListPersonBlocEvent {
const ListPersonBlocEvent();
}
class ListPersonBlocQuery extends ListPersonBlocEvent {
const ListPersonBlocQuery(this.account);
@override
toString() {
return "$runtimeType {"
"account: $account, "
"}";
}
final Account account;
}
abstract class ListPersonBlocState {
const ListPersonBlocState(this.account, this.items);
@override
toString() {
return "$runtimeType {"
"account: $account, "
"items: List {length: ${items.length}}, "
"}";
}
final Account? account;
final List<Person> items;
}
class ListPersonBlocInit extends ListPersonBlocState {
ListPersonBlocInit() : super(null, const []);
}
class ListPersonBlocLoading extends ListPersonBlocState {
const ListPersonBlocLoading(Account? account, List<Person> items)
: super(account, items);
}
class ListPersonBlocSuccess extends ListPersonBlocState {
const ListPersonBlocSuccess(Account? account, List<Person> items)
: super(account, items);
}
class ListPersonBlocFailure extends ListPersonBlocState {
const ListPersonBlocFailure(
Account? account, List<Person> items, this.exception)
: super(account, items);
@override
toString() {
return "$runtimeType {"
"super: ${super.toString()}, "
"exception: $exception, "
"}";
}
final dynamic exception;
}
/// List all people recognized in an account
class ListPersonBloc extends Bloc<ListPersonBlocEvent, ListPersonBlocState> {
2022-07-09 07:59:09 +02:00
ListPersonBloc() : super(ListPersonBlocInit()) {
on<ListPersonBlocEvent>(_onEvent);
}
2021-10-07 07:25:09 +02:00
static ListPersonBloc of(Account account) {
2022-01-27 11:02:45 +01:00
final name = bloc_util.getInstNameForAccount("ListPersonBloc", account);
2021-10-07 07:25:09 +02:00
try {
2022-01-27 11:02:45 +01:00
_log.fine("[of] Resolving bloc for '$name'");
return KiwiContainer().resolve<ListPersonBloc>(name);
2021-10-07 07:25:09 +02:00
} catch (_) {
// no created instance for this account, make a new one
_log.info("[of] New bloc instance for account: $account");
final bloc = ListPersonBloc();
2022-01-27 11:02:45 +01:00
KiwiContainer().registerInstance<ListPersonBloc>(bloc, name: name);
2021-10-07 07:25:09 +02:00
return bloc;
}
}
2022-07-09 07:59:09 +02:00
Future<void> _onEvent(
ListPersonBlocEvent event, Emitter<ListPersonBlocState> emit) async {
_log.info("[_onEvent] $event");
if (event is ListPersonBlocQuery) {
2022-07-09 07:59:09 +02:00
await _onEventQuery(event, emit);
}
}
2022-07-09 07:59:09 +02:00
Future<void> _onEventQuery(
ListPersonBlocQuery ev, Emitter<ListPersonBlocState> emit) async {
try {
2022-07-09 07:59:09 +02:00
emit(ListPersonBlocLoading(ev.account, state.items));
emit(ListPersonBlocSuccess(ev.account, await _query(ev)));
} catch (e, stackTrace) {
_log.severe("[_onEventQuery] Exception while request", e, stackTrace);
2022-07-09 07:59:09 +02:00
emit(ListPersonBlocFailure(ev.account, state.items, e));
}
}
Future<List<Person>> _query(ListPersonBlocQuery ev) {
2021-09-15 08:58:06 +02:00
const personRepo = PersonRepo(PersonRemoteDataSource());
return personRepo.list(ev.account);
}
static final _log = Logger("bloc.list_personListPersonBloc");
}