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

146 lines
4.3 KiB
Dart
Raw Normal View History

2022-01-25 11:08:13 +01:00
import 'package:bloc/bloc.dart';
import 'package:kiwi/kiwi.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/bloc/bloc_util.dart' as bloc_util;
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/use_case/cache_favorite.dart';
import 'package:nc_photos/use_case/list_favorite.dart';
import 'package:nc_photos/use_case/list_favorite_offline.dart';
abstract class ListFavoriteBlocEvent {
const ListFavoriteBlocEvent();
}
class ListFavoriteBlocQuery extends ListFavoriteBlocEvent {
const ListFavoriteBlocQuery(this.account);
@override
toString() => "$runtimeType {"
"account: $account, "
"}";
final Account account;
}
abstract class ListFavoriteBlocState {
const ListFavoriteBlocState(this.account, this.items);
@override
toString() => "$runtimeType {"
"account: $account, "
"items: List {length: ${items.length}}, "
"}";
final Account? account;
final List<File> items;
}
class ListFavoriteBlocInit extends ListFavoriteBlocState {
const ListFavoriteBlocInit() : super(null, const []);
}
class ListFavoriteBlocLoading extends ListFavoriteBlocState {
const ListFavoriteBlocLoading(Account? account, List<File> items)
: super(account, items);
}
class ListFavoriteBlocSuccess extends ListFavoriteBlocState {
const ListFavoriteBlocSuccess(Account? account, List<File> items)
: super(account, items);
}
class ListFavoriteBlocFailure extends ListFavoriteBlocState {
const ListFavoriteBlocFailure(
Account? account, List<File> items, this.exception)
: super(account, items);
@override
toString() => "$runtimeType {"
"super: ${super.toString()}, "
"exception: $exception, "
"}";
final dynamic exception;
}
/// List all favorites for this account
class ListFavoriteBloc
extends Bloc<ListFavoriteBlocEvent, ListFavoriteBlocState> {
ListFavoriteBloc(this._c)
: assert(require(_c)),
assert(ListFavorite.require(_c)),
2022-07-09 07:59:09 +02:00
super(const ListFavoriteBlocInit()) {
on<ListFavoriteBlocEvent>(_onEvent);
}
2022-01-25 11:08:13 +01:00
static bool require(DiContainer c) => true;
static ListFavoriteBloc of(Account account) {
final name =
bloc_util.getInstNameForRootAwareAccount("ListFavoriteBloc", account);
try {
_log.fine("[of] Resolving bloc for '$name'");
return KiwiContainer().resolve<ListFavoriteBloc>(name);
} catch (_) {
// no created instance for this account, make a new one
_log.info("[of] New bloc instance for account: $account");
final bloc = ListFavoriteBloc(KiwiContainer().resolve<DiContainer>());
KiwiContainer().registerInstance<ListFavoriteBloc>(bloc, name: name);
return bloc;
}
}
2022-07-09 07:59:09 +02:00
Future<void> _onEvent(
ListFavoriteBlocEvent event, Emitter<ListFavoriteBlocState> emit) async {
_log.info("[_onEvent] $event");
2022-01-25 11:08:13 +01:00
if (event is ListFavoriteBlocQuery) {
2022-07-09 07:59:09 +02:00
await _onEventQuery(event, emit);
2022-01-25 11:08:13 +01:00
}
}
2022-07-09 07:59:09 +02:00
Future<void> _onEventQuery(
ListFavoriteBlocQuery ev, Emitter<ListFavoriteBlocState> emit) async {
2022-01-25 11:08:13 +01:00
List<File>? cache;
try {
2022-07-09 07:59:09 +02:00
emit(ListFavoriteBlocLoading(ev.account, state.items));
2022-01-25 11:08:13 +01:00
cache = await _queryOffline(ev);
if (cache != null) {
2022-07-09 07:59:09 +02:00
emit(ListFavoriteBlocLoading(ev.account, cache));
2022-01-25 11:08:13 +01:00
}
final remote = await _queryOnline(ev);
2022-07-09 07:59:09 +02:00
emit(ListFavoriteBlocSuccess(ev.account, remote));
2022-01-25 11:08:13 +01:00
if (cache != null) {
CacheFavorite(_c)(ev.account, remote.map((f) => f.fileId!))
2022-01-25 11:08:13 +01:00
.onError((e, stackTrace) {
_log.shout(
"[_onEventQuery] Failed while CacheFavorite", e, stackTrace);
return -1;
2022-01-25 11:08:13 +01:00
});
}
} catch (e, stackTrace) {
_log.severe("[_onEventQuery] Exception while request", e, stackTrace);
2022-07-09 07:59:09 +02:00
emit(ListFavoriteBlocFailure(ev.account, cache ?? state.items, e));
2022-01-25 11:08:13 +01:00
}
}
Future<List<File>?> _queryOffline(ListFavoriteBlocQuery ev) async {
try {
return await ListFavoriteOffline(_c)(ev.account);
} catch (e, stackTrace) {
_log.shout("[_query] Failed", e, stackTrace);
return null;
}
}
Future<List<File>> _queryOnline(ListFavoriteBlocQuery ev) {
return ListFavorite(_c)(ev.account);
}
final DiContainer _c;
static final _log = Logger("bloc.list_favorite.ListFavoriteBloc");
}