2021-04-10 06:28:12 +02:00
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/entity/album.dart';
|
|
|
|
import 'package:nc_photos/entity/file.dart';
|
2021-05-24 09:09:25 +02:00
|
|
|
import 'package:nc_photos/entity/file/data_source.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:nc_photos/event/event.dart';
|
2021-08-06 06:54:06 +02:00
|
|
|
import 'package:nc_photos/exception.dart';
|
2021-07-31 21:49:28 +02:00
|
|
|
import 'package:nc_photos/throttler.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:nc_photos/use_case/list_album.dart';
|
2021-07-09 18:33:07 +02:00
|
|
|
import 'package:tuple/tuple.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
|
2021-08-13 12:38:46 +02:00
|
|
|
class ListAlbumBlocItem {
|
|
|
|
ListAlbumBlocItem(this.album);
|
|
|
|
|
|
|
|
final Album album;
|
|
|
|
}
|
|
|
|
|
2021-04-10 06:28:12 +02:00
|
|
|
abstract class ListAlbumBlocEvent {
|
|
|
|
const ListAlbumBlocEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ListAlbumBlocQuery extends ListAlbumBlocEvent {
|
|
|
|
const ListAlbumBlocQuery(this.account);
|
|
|
|
|
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
|
|
|
"account: $account, "
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An external event has happened and may affect the state of this bloc
|
|
|
|
class _ListAlbumBlocExternalEvent extends ListAlbumBlocEvent {
|
|
|
|
const _ListAlbumBlocExternalEvent();
|
|
|
|
|
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class ListAlbumBlocState {
|
2021-08-13 12:38:46 +02:00
|
|
|
const ListAlbumBlocState(this.account, this.items);
|
2021-04-10 06:28:12 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
|
|
|
"account: $account, "
|
2021-08-13 12:38:46 +02:00
|
|
|
"items: List {length: ${items.length}}, "
|
2021-04-10 06:28:12 +02:00
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
final Account? account;
|
2021-08-13 12:38:46 +02:00
|
|
|
final List<ListAlbumBlocItem> items;
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ListAlbumBlocInit extends ListAlbumBlocState {
|
|
|
|
const ListAlbumBlocInit() : super(null, const []);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ListAlbumBlocLoading extends ListAlbumBlocState {
|
2021-08-13 12:38:46 +02:00
|
|
|
const ListAlbumBlocLoading(Account? account, List<ListAlbumBlocItem> items)
|
|
|
|
: super(account, items);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ListAlbumBlocSuccess extends ListAlbumBlocState {
|
2021-08-13 12:38:46 +02:00
|
|
|
const ListAlbumBlocSuccess(Account? account, List<ListAlbumBlocItem> items)
|
|
|
|
: super(account, items);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ListAlbumBlocFailure extends ListAlbumBlocState {
|
|
|
|
const ListAlbumBlocFailure(
|
2021-08-13 12:38:46 +02:00
|
|
|
Account? account, List<ListAlbumBlocItem> items, this.exception)
|
|
|
|
: super(account, items);
|
2021-04-10 06:28:12 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
|
|
|
"super: ${super.toString()}, "
|
|
|
|
"exception: $exception, "
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
final dynamic exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The state of this bloc is inconsistent. This typically means that the data
|
|
|
|
/// may have been changed externally
|
|
|
|
class ListAlbumBlocInconsistent extends ListAlbumBlocState {
|
2021-08-13 12:38:46 +02:00
|
|
|
const ListAlbumBlocInconsistent(
|
|
|
|
Account? account, List<ListAlbumBlocItem> items)
|
|
|
|
: super(account, items);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ListAlbumBloc extends Bloc<ListAlbumBlocEvent, ListAlbumBlocState> {
|
|
|
|
ListAlbumBloc() : super(ListAlbumBlocInit()) {
|
|
|
|
_albumUpdatedListener =
|
|
|
|
AppEventListener<AlbumUpdatedEvent>(_onAlbumUpdatedEvent);
|
|
|
|
_fileRemovedListener =
|
|
|
|
AppEventListener<FileRemovedEvent>(_onFileRemovedEvent);
|
|
|
|
_albumCreatedListener =
|
|
|
|
AppEventListener<AlbumCreatedEvent>(_onAlbumCreatedEvent);
|
|
|
|
_albumUpdatedListener.begin();
|
|
|
|
_fileRemovedListener.begin();
|
|
|
|
_albumCreatedListener.begin();
|
2021-07-31 21:49:28 +02:00
|
|
|
|
|
|
|
_refreshThrottler = Throttler(
|
|
|
|
onTriggered: (_) {
|
|
|
|
add(_ListAlbumBlocExternalEvent());
|
|
|
|
},
|
|
|
|
logTag: "ListAlbumBloc.refresh",
|
|
|
|
);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
mapEventToState(ListAlbumBlocEvent event) async* {
|
|
|
|
_log.info("[mapEventToState] $event");
|
|
|
|
if (event is ListAlbumBlocQuery) {
|
|
|
|
yield* _onEventQuery(event);
|
|
|
|
} else if (event is _ListAlbumBlocExternalEvent) {
|
|
|
|
yield* _onExternalEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
close() {
|
|
|
|
_albumUpdatedListener.end();
|
|
|
|
_fileRemovedListener.end();
|
|
|
|
_albumCreatedListener.end();
|
2021-07-31 21:49:28 +02:00
|
|
|
_refreshThrottler.clear();
|
2021-04-10 06:28:12 +02:00
|
|
|
return super.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream<ListAlbumBlocState> _onEventQuery(ListAlbumBlocQuery ev) async* {
|
2021-08-13 12:38:46 +02:00
|
|
|
yield ListAlbumBlocLoading(ev.account, state.items);
|
|
|
|
bool hasContent = state.items.isNotEmpty;
|
2021-04-10 06:28:12 +02:00
|
|
|
|
2021-04-26 18:14:44 +02:00
|
|
|
if (!hasContent) {
|
|
|
|
// show something instantly on first load
|
2021-07-09 18:33:07 +02:00
|
|
|
final cacheState = await _queryOffline(ev);
|
2021-08-13 12:38:46 +02:00
|
|
|
yield ListAlbumBlocLoading(ev.account, cacheState.items);
|
|
|
|
hasContent = cacheState.items.isNotEmpty;
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-09 18:33:07 +02:00
|
|
|
final newState = await _queryOnline(ev);
|
|
|
|
if (newState is ListAlbumBlocFailure) {
|
|
|
|
yield ListAlbumBlocFailure(
|
|
|
|
ev.account,
|
2021-08-13 12:38:46 +02:00
|
|
|
newState.items.isNotEmpty ? newState.items : state.items,
|
2021-07-09 18:33:07 +02:00
|
|
|
newState.exception);
|
2021-04-10 06:28:12 +02:00
|
|
|
} else {
|
2021-07-09 18:33:07 +02:00
|
|
|
yield newState;
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream<ListAlbumBlocState> _onExternalEvent(
|
|
|
|
_ListAlbumBlocExternalEvent ev) async* {
|
2021-08-13 12:38:46 +02:00
|
|
|
yield ListAlbumBlocInconsistent(state.account, state.items);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _onAlbumUpdatedEvent(AlbumUpdatedEvent ev) {
|
|
|
|
if (state is ListAlbumBlocInit) {
|
|
|
|
// no data in this bloc, ignore
|
|
|
|
return;
|
|
|
|
}
|
2021-07-31 21:49:28 +02:00
|
|
|
_refreshThrottler.trigger(
|
|
|
|
maxResponceTime: const Duration(seconds: 3),
|
|
|
|
maxPendingCount: 10,
|
|
|
|
);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _onFileRemovedEvent(FileRemovedEvent ev) {
|
|
|
|
if (state is ListAlbumBlocInit) {
|
|
|
|
// no data in this bloc, ignore
|
|
|
|
return;
|
|
|
|
}
|
2021-07-18 19:22:02 +02:00
|
|
|
if (isAlbumFile(ev.account, ev.file)) {
|
2021-07-31 21:49:28 +02:00
|
|
|
_refreshThrottler.trigger(
|
|
|
|
maxResponceTime: const Duration(seconds: 3),
|
|
|
|
maxPendingCount: 10,
|
|
|
|
);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onAlbumCreatedEvent(AlbumCreatedEvent ev) {
|
|
|
|
if (state is ListAlbumBlocInit) {
|
|
|
|
// no data in this bloc, ignore
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
add(_ListAlbumBlocExternalEvent());
|
|
|
|
}
|
|
|
|
|
2021-07-09 18:33:07 +02:00
|
|
|
Future<ListAlbumBlocState> _queryOffline(ListAlbumBlocQuery ev) =>
|
2021-04-10 06:28:12 +02:00
|
|
|
_queryWithAlbumDataSource(
|
2021-07-09 18:33:07 +02:00
|
|
|
ev, FileAppDbDataSource(), AlbumAppDbDataSource());
|
2021-04-10 06:28:12 +02:00
|
|
|
|
2021-07-09 18:33:07 +02:00
|
|
|
Future<ListAlbumBlocState> _queryOnline(ListAlbumBlocQuery ev) =>
|
2021-04-10 06:28:12 +02:00
|
|
|
_queryWithAlbumDataSource(
|
2021-07-09 18:33:07 +02:00
|
|
|
ev, FileCachedDataSource(), AlbumCachedDataSource());
|
2021-04-10 06:28:12 +02:00
|
|
|
|
2021-07-09 18:33:07 +02:00
|
|
|
Future<ListAlbumBlocState> _queryWithAlbumDataSource(ListAlbumBlocQuery ev,
|
|
|
|
FileDataSource fileDataSource, AlbumDataSource albumDataSrc) async {
|
2021-04-10 06:28:12 +02:00
|
|
|
try {
|
2021-07-09 18:33:07 +02:00
|
|
|
final albums = <Album>[];
|
|
|
|
final errors = <dynamic>[];
|
|
|
|
await for (final result in ListAlbum(
|
|
|
|
FileRepo(fileDataSource), AlbumRepo(albumDataSrc))(ev.account)) {
|
|
|
|
if (result is Tuple2) {
|
2021-08-06 06:54:06 +02:00
|
|
|
if (result.item1 is CacheNotFoundException) {
|
|
|
|
_log.info(
|
|
|
|
"[_queryWithAlbumDataSource] Cache not found", result.item1);
|
|
|
|
} else {
|
|
|
|
_log.shout("[_queryWithAlbumDataSource] Exception while ListAlbum",
|
|
|
|
result.item1, result.item2);
|
|
|
|
}
|
2021-07-09 18:33:07 +02:00
|
|
|
errors.add(result.item1);
|
|
|
|
} else if (result is Album) {
|
|
|
|
albums.add(result);
|
|
|
|
}
|
|
|
|
}
|
2021-08-13 12:38:46 +02:00
|
|
|
|
|
|
|
final items = albums.map((e) {
|
|
|
|
return ListAlbumBlocItem(e);
|
|
|
|
}).toList();
|
|
|
|
|
2021-07-09 18:33:07 +02:00
|
|
|
if (errors.isEmpty) {
|
2021-08-13 12:38:46 +02:00
|
|
|
return ListAlbumBlocSuccess(ev.account, items);
|
2021-07-09 18:33:07 +02:00
|
|
|
} else {
|
2021-08-13 12:38:46 +02:00
|
|
|
return ListAlbumBlocFailure(ev.account, items, errors.first);
|
2021-07-09 18:33:07 +02:00
|
|
|
}
|
2021-04-26 18:15:37 +02:00
|
|
|
} catch (e, stacktrace) {
|
2021-07-09 18:33:07 +02:00
|
|
|
_log.severe("[_queryWithAlbumDataSource] Exception", e, stacktrace);
|
|
|
|
return ListAlbumBlocFailure(ev.account, [], e);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
late AppEventListener<AlbumUpdatedEvent> _albumUpdatedListener;
|
|
|
|
late AppEventListener<FileRemovedEvent> _fileRemovedListener;
|
|
|
|
late AppEventListener<AlbumCreatedEvent> _albumCreatedListener;
|
2021-04-10 06:28:12 +02:00
|
|
|
|
2021-07-31 21:49:28 +02:00
|
|
|
late Throttler _refreshThrottler;
|
|
|
|
|
2021-04-10 06:28:12 +02:00
|
|
|
static final _log = Logger("bloc.list_album.ListAlbumBloc");
|
|
|
|
}
|