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';
|
2021-09-08 12:44:14 +02:00
|
|
|
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';
|
2021-09-08 12:44:14 +02:00
|
|
|
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';
|
2021-09-08 12:44:14 +02:00
|
|
|
|
|
|
|
abstract class ListPersonBlocEvent {
|
|
|
|
const ListPersonBlocEvent();
|
|
|
|
}
|
|
|
|
|
2022-12-08 16:39:13 +01:00
|
|
|
@toString
|
2021-09-08 12:44:14 +02:00
|
|
|
class ListPersonBlocQuery extends ListPersonBlocEvent {
|
|
|
|
const ListPersonBlocQuery(this.account);
|
|
|
|
|
|
|
|
@override
|
2022-12-08 16:39:13 +01:00
|
|
|
String toString() => _$toString();
|
2021-09-08 12:44:14 +02:00
|
|
|
|
|
|
|
final Account account;
|
|
|
|
}
|
|
|
|
|
2022-12-08 16:39:13 +01:00
|
|
|
@toString
|
2021-09-08 12:44:14 +02:00
|
|
|
abstract class ListPersonBlocState {
|
|
|
|
const ListPersonBlocState(this.account, this.items);
|
|
|
|
|
|
|
|
@override
|
2022-12-08 16:39:13 +01:00
|
|
|
String toString() => _$toString();
|
2021-09-08 12:44:14 +02:00
|
|
|
|
|
|
|
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
|
2021-09-08 12:44:14 +02:00
|
|
|
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();
|
2021-09-08 12:44:14 +02:00
|
|
|
|
2022-08-17 16:40:49 +02:00
|
|
|
final Object exception;
|
2021-09-08 12:44:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// List all people recognized in an account
|
2022-12-16 16:01:04 +01:00
|
|
|
@npLog
|
2021-09-08 12:44:14 +02:00
|
|
|
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);
|
|
|
|
}
|
2021-09-08 12:44:14 +02:00
|
|
|
|
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");
|
2021-09-08 12:44:14 +02:00
|
|
|
if (event is ListPersonBlocQuery) {
|
2022-07-09 07:59:09 +02:00
|
|
|
await _onEventQuery(event, emit);
|
2021-09-08 12:44:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-09 07:59:09 +02:00
|
|
|
Future<void> _onEventQuery(
|
|
|
|
ListPersonBlocQuery ev, Emitter<ListPersonBlocState> emit) async {
|
2021-09-08 12:44:14 +02:00
|
|
|
try {
|
2022-07-09 07:59:09 +02:00
|
|
|
emit(ListPersonBlocLoading(ev.account, state.items));
|
|
|
|
emit(ListPersonBlocSuccess(ev.account, await _query(ev)));
|
2021-09-08 12:44:14 +02:00
|
|
|
} 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));
|
2021-09-08 12:44:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:26:29 +02:00
|
|
|
Future<List<Person>> _query(ListPersonBlocQuery ev) =>
|
2022-08-17 16:42:01 +02:00
|
|
|
ListPerson(_c.withLocalRepo())(ev.account);
|
2022-08-05 10:26:29 +02:00
|
|
|
|
|
|
|
final DiContainer _c;
|
2021-09-08 12:44:14 +02:00
|
|
|
}
|