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

127 lines
3.7 KiB
Dart
Raw Normal View History

2022-08-06 06:21:11 +02:00
import 'package:bloc/bloc.dart';
2022-12-08 16:39:13 +01:00
import 'package:flutter/foundation.dart';
2022-08-06 06:21:11 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/person.dart';
2022-08-28 17:35:29 +02:00
import 'package:nc_photos/use_case/list_location_group.dart';
2022-08-06 06:21:11 +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 'search_landing.g.dart';
2022-08-06 06:21:11 +02:00
abstract class SearchLandingBlocEvent {
const SearchLandingBlocEvent();
}
2022-12-08 16:39:13 +01:00
@toString
2022-08-06 06:21:11 +02:00
class SearchLandingBlocQuery extends SearchLandingBlocEvent {
const SearchLandingBlocQuery(this.account);
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
2022-08-06 06:21:11 +02:00
final Account account;
}
2022-12-08 16:39:13 +01:00
@toString
2022-08-06 06:21:11 +02:00
abstract class SearchLandingBlocState {
2022-08-28 17:35:29 +02:00
const SearchLandingBlocState(this.account, this.persons, this.locations);
2022-08-06 06:21:11 +02:00
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
2022-08-06 06:21:11 +02:00
final Account? account;
final List<Person> persons;
2022-08-28 17:35:29 +02:00
final LocationGroupResult locations;
2022-08-06 06:21:11 +02:00
}
class SearchLandingBlocInit extends SearchLandingBlocState {
2022-08-28 17:35:29 +02:00
SearchLandingBlocInit()
: super(null, const [], const LocationGroupResult([], [], [], []));
2022-08-06 06:21:11 +02:00
}
class SearchLandingBlocLoading extends SearchLandingBlocState {
2022-08-28 17:35:29 +02:00
const SearchLandingBlocLoading(
Account? account, List<Person> persons, LocationGroupResult locations)
: super(account, persons, locations);
2022-08-06 06:21:11 +02:00
}
class SearchLandingBlocSuccess extends SearchLandingBlocState {
2022-08-28 17:35:29 +02:00
const SearchLandingBlocSuccess(
Account? account, List<Person> persons, LocationGroupResult locations)
: super(account, persons, locations);
2022-08-06 06:21:11 +02:00
}
2022-12-08 16:39:13 +01:00
@toString
2022-08-06 06:21:11 +02:00
class SearchLandingBlocFailure extends SearchLandingBlocState {
2022-08-28 17:35:29 +02:00
const SearchLandingBlocFailure(Account? account, List<Person> persons,
LocationGroupResult locations, this.exception)
: super(account, persons, locations);
2022-08-06 06:21:11 +02:00
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
2022-08-06 06:21:11 +02:00
final Object exception;
}
2022-12-16 16:01:04 +01:00
@npLog
2022-08-06 06:21:11 +02:00
class SearchLandingBloc
extends Bloc<SearchLandingBlocEvent, SearchLandingBlocState> {
SearchLandingBloc(this._c)
: assert(require(_c)),
2022-08-17 16:42:14 +02:00
assert(ListPerson.require(_c)),
2022-08-06 06:21:11 +02:00
super(SearchLandingBlocInit()) {
on<SearchLandingBlocEvent>(_onEvent);
}
static bool require(DiContainer c) => true;
Future<void> _onEvent(SearchLandingBlocEvent event,
Emitter<SearchLandingBlocState> emit) async {
_log.info("[_onEvent] $event");
if (event is SearchLandingBlocQuery) {
await _onEventQuery(event, emit);
}
}
Future<void> _onEventQuery(
SearchLandingBlocQuery ev, Emitter<SearchLandingBlocState> emit) async {
try {
2022-08-28 17:35:29 +02:00
emit(
SearchLandingBlocLoading(ev.account, state.persons, state.locations));
List<Person>? persons;
try {
persons = await _queryPeople(ev);
} catch (e, stackTrace) {
_log.shout("[_onEventQuery] Failed while _queryPeople", e, stackTrace);
}
LocationGroupResult? locations;
try {
locations = await _queryLocations(ev);
} catch (e, stackTrace) {
_log.shout(
"[_onEventQuery] Failed while _queryLocations", e, stackTrace);
}
emit(SearchLandingBlocSuccess(ev.account, persons ?? [],
locations ?? const LocationGroupResult([], [], [], [])));
2022-08-06 06:21:11 +02:00
} catch (e, stackTrace) {
_log.severe("[_onEventQuery] Exception while request", e, stackTrace);
2022-08-28 17:35:29 +02:00
emit(SearchLandingBlocFailure(
ev.account, state.persons, state.locations, e));
2022-08-06 06:21:11 +02:00
}
}
2022-08-28 17:35:29 +02:00
Future<List<Person>> _queryPeople(SearchLandingBlocQuery ev) =>
2022-08-06 06:21:11 +02:00
ListPerson(_c.withLocalRepo())(ev.account);
2022-08-28 17:35:29 +02:00
Future<LocationGroupResult> _queryLocations(SearchLandingBlocQuery ev) =>
ListLocationGroup(_c.withLocalRepo())(ev.account);
2022-08-06 06:21:11 +02:00
final DiContainer _c;
}