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

100 lines
2.7 KiB
Dart
Raw Normal View History

2022-12-08 16:39:13 +01:00
import 'package:flutter/foundation.dart';
2023-05-23 18:47:32 +02:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
2022-08-05 10:26:29 +02:00
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/person.dart';
2022-08-05 10:26:29 +02:00
import 'package:nc_photos/use_case/list_person.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
2022-12-08 16:39:13 +01:00
import 'package:to_string/to_string.dart';
part 'list_person.g.dart';
abstract class ListPersonBlocEvent {
const ListPersonBlocEvent();
}
2022-12-08 16:39:13 +01:00
@toString
class ListPersonBlocQuery extends ListPersonBlocEvent {
const ListPersonBlocQuery(this.account);
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
final Account account;
}
2022-12-08 16:39:13 +01:00
@toString
abstract class ListPersonBlocState {
const ListPersonBlocState(this.account, this.items);
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
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);
}
2022-12-08 16:39:13 +01:00
@toString
class ListPersonBlocFailure extends ListPersonBlocState {
const ListPersonBlocFailure(
Account? account, List<Person> items, this.exception)
: super(account, items);
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
2022-08-17 16:40:49 +02:00
final Object exception;
}
/// List all people recognized in an account
2022-12-16 16:01:04 +01:00
@npLog
class ListPersonBloc extends Bloc<ListPersonBlocEvent, ListPersonBlocState> {
2022-08-05 10:26:29 +02:00
ListPersonBloc(this._c)
: assert(require(_c)),
assert(ListPerson.require(_c)),
super(ListPersonBlocInit()) {
2022-07-09 07:59:09 +02:00
on<ListPersonBlocEvent>(_onEvent);
}
2022-08-05 10:26:29 +02:00
static bool require(DiContainer c) => true;
2021-10-07 07:25:09 +02:00
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));
}
}
2022-08-05 10:26:29 +02:00
Future<List<Person>> _query(ListPersonBlocQuery ev) =>
ListPerson(_c.withLocalRepo())(ev.account);
2022-08-05 10:26:29 +02:00
final DiContainer _c;
}