mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-03-24 07:54:42 +01:00
List album returns its own object
This commit is contained in:
parent
8bd586fc4a
commit
2092b6aa0c
3 changed files with 41 additions and 27 deletions
|
@ -10,6 +10,12 @@ import 'package:nc_photos/throttler.dart';
|
||||||
import 'package:nc_photos/use_case/list_album.dart';
|
import 'package:nc_photos/use_case/list_album.dart';
|
||||||
import 'package:tuple/tuple.dart';
|
import 'package:tuple/tuple.dart';
|
||||||
|
|
||||||
|
class ListAlbumBlocItem {
|
||||||
|
ListAlbumBlocItem(this.album);
|
||||||
|
|
||||||
|
final Album album;
|
||||||
|
}
|
||||||
|
|
||||||
abstract class ListAlbumBlocEvent {
|
abstract class ListAlbumBlocEvent {
|
||||||
const ListAlbumBlocEvent();
|
const ListAlbumBlocEvent();
|
||||||
}
|
}
|
||||||
|
@ -39,18 +45,18 @@ class _ListAlbumBlocExternalEvent extends ListAlbumBlocEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class ListAlbumBlocState {
|
abstract class ListAlbumBlocState {
|
||||||
const ListAlbumBlocState(this.account, this.albums);
|
const ListAlbumBlocState(this.account, this.items);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
toString() {
|
toString() {
|
||||||
return "$runtimeType {"
|
return "$runtimeType {"
|
||||||
"account: $account, "
|
"account: $account, "
|
||||||
"albums: List {length: ${albums.length}}, "
|
"items: List {length: ${items.length}}, "
|
||||||
"}";
|
"}";
|
||||||
}
|
}
|
||||||
|
|
||||||
final Account? account;
|
final Account? account;
|
||||||
final List<Album> albums;
|
final List<ListAlbumBlocItem> items;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ListAlbumBlocInit extends ListAlbumBlocState {
|
class ListAlbumBlocInit extends ListAlbumBlocState {
|
||||||
|
@ -58,19 +64,19 @@ class ListAlbumBlocInit extends ListAlbumBlocState {
|
||||||
}
|
}
|
||||||
|
|
||||||
class ListAlbumBlocLoading extends ListAlbumBlocState {
|
class ListAlbumBlocLoading extends ListAlbumBlocState {
|
||||||
const ListAlbumBlocLoading(Account? account, List<Album> albums)
|
const ListAlbumBlocLoading(Account? account, List<ListAlbumBlocItem> items)
|
||||||
: super(account, albums);
|
: super(account, items);
|
||||||
}
|
}
|
||||||
|
|
||||||
class ListAlbumBlocSuccess extends ListAlbumBlocState {
|
class ListAlbumBlocSuccess extends ListAlbumBlocState {
|
||||||
const ListAlbumBlocSuccess(Account? account, List<Album> albums)
|
const ListAlbumBlocSuccess(Account? account, List<ListAlbumBlocItem> items)
|
||||||
: super(account, albums);
|
: super(account, items);
|
||||||
}
|
}
|
||||||
|
|
||||||
class ListAlbumBlocFailure extends ListAlbumBlocState {
|
class ListAlbumBlocFailure extends ListAlbumBlocState {
|
||||||
const ListAlbumBlocFailure(
|
const ListAlbumBlocFailure(
|
||||||
Account? account, List<Album> albums, this.exception)
|
Account? account, List<ListAlbumBlocItem> items, this.exception)
|
||||||
: super(account, albums);
|
: super(account, items);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
toString() {
|
toString() {
|
||||||
|
@ -86,8 +92,9 @@ class ListAlbumBlocFailure extends ListAlbumBlocState {
|
||||||
/// The state of this bloc is inconsistent. This typically means that the data
|
/// The state of this bloc is inconsistent. This typically means that the data
|
||||||
/// may have been changed externally
|
/// may have been changed externally
|
||||||
class ListAlbumBlocInconsistent extends ListAlbumBlocState {
|
class ListAlbumBlocInconsistent extends ListAlbumBlocState {
|
||||||
const ListAlbumBlocInconsistent(Account? account, List<Album> albums)
|
const ListAlbumBlocInconsistent(
|
||||||
: super(account, albums);
|
Account? account, List<ListAlbumBlocItem> items)
|
||||||
|
: super(account, items);
|
||||||
}
|
}
|
||||||
|
|
||||||
class ListAlbumBloc extends Bloc<ListAlbumBlocEvent, ListAlbumBlocState> {
|
class ListAlbumBloc extends Bloc<ListAlbumBlocEvent, ListAlbumBlocState> {
|
||||||
|
@ -130,21 +137,21 @@ class ListAlbumBloc extends Bloc<ListAlbumBlocEvent, ListAlbumBlocState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Stream<ListAlbumBlocState> _onEventQuery(ListAlbumBlocQuery ev) async* {
|
Stream<ListAlbumBlocState> _onEventQuery(ListAlbumBlocQuery ev) async* {
|
||||||
yield ListAlbumBlocLoading(ev.account, state.albums);
|
yield ListAlbumBlocLoading(ev.account, state.items);
|
||||||
bool hasContent = state.albums.isNotEmpty;
|
bool hasContent = state.items.isNotEmpty;
|
||||||
|
|
||||||
if (!hasContent) {
|
if (!hasContent) {
|
||||||
// show something instantly on first load
|
// show something instantly on first load
|
||||||
final cacheState = await _queryOffline(ev);
|
final cacheState = await _queryOffline(ev);
|
||||||
yield ListAlbumBlocLoading(ev.account, cacheState.albums);
|
yield ListAlbumBlocLoading(ev.account, cacheState.items);
|
||||||
hasContent = cacheState.albums.isNotEmpty;
|
hasContent = cacheState.items.isNotEmpty;
|
||||||
}
|
}
|
||||||
|
|
||||||
final newState = await _queryOnline(ev);
|
final newState = await _queryOnline(ev);
|
||||||
if (newState is ListAlbumBlocFailure) {
|
if (newState is ListAlbumBlocFailure) {
|
||||||
yield ListAlbumBlocFailure(
|
yield ListAlbumBlocFailure(
|
||||||
ev.account,
|
ev.account,
|
||||||
newState.albums.isNotEmpty ? newState.albums : state.albums,
|
newState.items.isNotEmpty ? newState.items : state.items,
|
||||||
newState.exception);
|
newState.exception);
|
||||||
} else {
|
} else {
|
||||||
yield newState;
|
yield newState;
|
||||||
|
@ -153,7 +160,7 @@ class ListAlbumBloc extends Bloc<ListAlbumBlocEvent, ListAlbumBlocState> {
|
||||||
|
|
||||||
Stream<ListAlbumBlocState> _onExternalEvent(
|
Stream<ListAlbumBlocState> _onExternalEvent(
|
||||||
_ListAlbumBlocExternalEvent ev) async* {
|
_ListAlbumBlocExternalEvent ev) async* {
|
||||||
yield ListAlbumBlocInconsistent(state.account, state.albums);
|
yield ListAlbumBlocInconsistent(state.account, state.items);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onAlbumUpdatedEvent(AlbumUpdatedEvent ev) {
|
void _onAlbumUpdatedEvent(AlbumUpdatedEvent ev) {
|
||||||
|
@ -216,10 +223,15 @@ class ListAlbumBloc extends Bloc<ListAlbumBlocEvent, ListAlbumBlocState> {
|
||||||
albums.add(result);
|
albums.add(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final items = albums.map((e) {
|
||||||
|
return ListAlbumBlocItem(e);
|
||||||
|
}).toList();
|
||||||
|
|
||||||
if (errors.isEmpty) {
|
if (errors.isEmpty) {
|
||||||
return ListAlbumBlocSuccess(ev.account, albums);
|
return ListAlbumBlocSuccess(ev.account, items);
|
||||||
} else {
|
} else {
|
||||||
return ListAlbumBlocFailure(ev.account, albums, errors.first);
|
return ListAlbumBlocFailure(ev.account, items, errors.first);
|
||||||
}
|
}
|
||||||
} catch (e, stacktrace) {
|
} catch (e, stacktrace) {
|
||||||
_log.severe("[_queryWithAlbumDataSource] Exception", e, stacktrace);
|
_log.severe("[_queryWithAlbumDataSource] Exception", e, stacktrace);
|
||||||
|
|
|
@ -114,9 +114,9 @@ class _AlbumPickerDialogState extends State<AlbumPickerDialog> {
|
||||||
if (state is ListAlbumBlocInit) {
|
if (state is ListAlbumBlocInit) {
|
||||||
_items.clear();
|
_items.clear();
|
||||||
} else if (state is ListAlbumBlocSuccess || state is ListAlbumBlocLoading) {
|
} else if (state is ListAlbumBlocSuccess || state is ListAlbumBlocLoading) {
|
||||||
_transformItems(state.albums);
|
_transformItems(state.items);
|
||||||
} else if (state is ListAlbumBlocFailure) {
|
} else if (state is ListAlbumBlocFailure) {
|
||||||
_transformItems(state.albums);
|
_transformItems(state.items);
|
||||||
SnackBarManager().showSnackBar(SnackBar(
|
SnackBarManager().showSnackBar(SnackBar(
|
||||||
content: Text(exception_util.toUserString(state.exception, context)),
|
content: Text(exception_util.toUserString(state.exception, context)),
|
||||||
duration: k.snackBarDurationNormal,
|
duration: k.snackBarDurationNormal,
|
||||||
|
@ -149,8 +149,9 @@ class _AlbumPickerDialogState extends State<AlbumPickerDialog> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void _transformItems(List<Album> albums) {
|
void _transformItems(List<ListAlbumBlocItem> items) {
|
||||||
final sortedAlbums = albums
|
final sortedAlbums = items
|
||||||
|
.map((e) => e.album)
|
||||||
.where((element) => element.provider is AlbumStaticProvider)
|
.where((element) => element.provider is AlbumStaticProvider)
|
||||||
.map((e) => Tuple2(e.provider.latestItemTime ?? e.lastUpdated, e))
|
.map((e) => Tuple2(e.provider.latestItemTime ?? e.lastUpdated, e))
|
||||||
.sorted((a, b) {
|
.sorted((a, b) {
|
||||||
|
|
|
@ -250,9 +250,9 @@ class _HomeAlbumsState extends State<HomeAlbums>
|
||||||
if (state is ListAlbumBlocInit) {
|
if (state is ListAlbumBlocInit) {
|
||||||
_items.clear();
|
_items.clear();
|
||||||
} else if (state is ListAlbumBlocSuccess || state is ListAlbumBlocLoading) {
|
} else if (state is ListAlbumBlocSuccess || state is ListAlbumBlocLoading) {
|
||||||
_transformItems(state.albums);
|
_transformItems(state.items);
|
||||||
} else if (state is ListAlbumBlocFailure) {
|
} else if (state is ListAlbumBlocFailure) {
|
||||||
_transformItems(state.albums);
|
_transformItems(state.items);
|
||||||
if (isPageVisible()) {
|
if (isPageVisible()) {
|
||||||
SnackBarManager().showSnackBar(SnackBar(
|
SnackBarManager().showSnackBar(SnackBar(
|
||||||
content: Text(exception_util.toUserString(state.exception, context)),
|
content: Text(exception_util.toUserString(state.exception, context)),
|
||||||
|
@ -380,8 +380,9 @@ class _HomeAlbumsState extends State<HomeAlbums>
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transform an Album list to grid items
|
/// Transform an Album list to grid items
|
||||||
void _transformItems(List<Album> albums) {
|
void _transformItems(List<ListAlbumBlocItem> items) {
|
||||||
final sortedAlbums = albums
|
final sortedAlbums = items
|
||||||
|
.map((e) => e.album)
|
||||||
.map((e) => Tuple2(e.provider.latestItemTime ?? e.lastUpdated, e))
|
.map((e) => Tuple2(e.provider.latestItemTime ?? e.lastUpdated, e))
|
||||||
.sorted((a, b) {
|
.sorted((a, b) {
|
||||||
// then sort in descending order
|
// then sort in descending order
|
||||||
|
|
Loading…
Add table
Reference in a new issue