nc-photos/lib/widget/album_viewer.dart

380 lines
11 KiB
Dart
Raw Normal View History

2021-04-21 16:17:18 +02:00
import 'package:flutter/foundation.dart';
2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2021-07-06 15:03:29 +02:00
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
2021-04-10 06:28:12 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api_util.dart' as api_util;
import 'package:nc_photos/entity/album.dart';
2021-07-05 09:54:01 +02:00
import 'package:nc_photos/entity/album/item.dart';
2021-06-24 18:26:56 +02:00
import 'package:nc_photos/entity/album/provider.dart';
2021-07-07 20:40:43 +02:00
import 'package:nc_photos/entity/album/sort_provider.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file_util.dart' as file_util;
import 'package:nc_photos/exception_util.dart' as exception_util;
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/list_extension.dart';
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/use_case/resync_album.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/use_case/update_album.dart';
import 'package:nc_photos/widget/album_viewer_mixin.dart';
2021-05-06 13:29:20 +02:00
import 'package:nc_photos/widget/photo_list_item.dart';
2021-04-23 09:55:53 +02:00
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/widget/viewer.dart';
import 'package:quiver/iterables.dart';
2021-04-10 06:28:12 +02:00
class AlbumViewerArguments {
AlbumViewerArguments(this.account, this.album);
final Account account;
final Album album;
}
class AlbumViewer extends StatefulWidget {
static const routeName = "/album-viewer";
AlbumViewer({
Key key,
@required this.account,
@required this.album,
}) : super(key: key);
AlbumViewer.fromArgs(AlbumViewerArguments args, {Key key})
: this(
key: key,
account: args.account,
album: args.album,
);
@override
createState() => _AlbumViewerState();
final Account account;
final Album album;
}
class _AlbumViewerState extends State<AlbumViewer>
with
WidgetsBindingObserver,
SelectableItemStreamListMixin<AlbumViewer>,
AlbumViewerMixin<AlbumViewer> {
2021-04-10 06:28:12 +02:00
@override
initState() {
super.initState();
_initAlbum();
2021-04-10 06:28:12 +02:00
}
@override
build(BuildContext context) {
return AppTheme(
child: Scaffold(
2021-07-02 13:55:52 +02:00
body: Builder(
builder: (context) {
if (isEditMode) {
return Form(
key: _editFormKey,
child: _buildContent(context),
);
} else {
return _buildContent(context);
}
},
),
2021-04-10 06:28:12 +02:00
),
);
}
2021-07-02 13:55:52 +02:00
@override
doneEditMode() {
if (_editFormKey?.currentState?.validate() == true) {
_editFormKey.currentState.save();
final newAlbum = makeEdited(_album);
if (newAlbum.copyWith(lastUpdated: _album.lastUpdated) != _album) {
_log.info("[doneEditMode] Album modified: $newAlbum");
final albumRepo = AlbumRepo(AlbumCachedDataSource());
setState(() {
_album = newAlbum;
});
UpdateAlbum(albumRepo)(widget.account, newAlbum)
.catchError((e, stacktrace) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(e, context)),
duration: k.snackBarDurationNormal,
));
});
} else {
_log.fine("[doneEditMode] Album not modified");
}
return true;
}
return false;
}
void _initAlbum() {
2021-06-24 18:26:56 +02:00
assert(widget.album.provider is AlbumStaticProvider);
ResyncAlbum()(widget.account, widget.album).then((album) {
if (_shouldPropagateResyncedAlbum(album)) {
UpdateAlbum(AlbumRepo(AlbumCachedDataSource()))(widget.account, album)
.catchError((e, stacktrace) {
_log.shout("[_initAlbum] Failed while updating album", e, stacktrace);
});
}
if (mounted) {
setState(() {
_album = album;
_transformItems();
initCover(widget.account, _backingFiles);
});
}
});
}
2021-04-10 06:28:12 +02:00
Widget _buildContent(BuildContext context) {
if (_album == null) {
return CustomScrollView(
slivers: [
buildNormalAppBar(context, widget.account, widget.album),
const SliverToBoxAdapter(
child: const LinearProgressIndicator(),
),
],
);
}
2021-04-23 09:55:53 +02:00
return buildItemStreamListOuter(
context,
child: Theme(
data: Theme.of(context).copyWith(
accentColor: AppTheme.getOverscrollIndicatorColor(context),
),
child: CustomScrollView(
slivers: [
_buildAppBar(context),
SliverPadding(
2021-07-05 07:23:01 +02:00
padding: const EdgeInsets.symmetric(vertical: 8),
2021-07-02 13:55:52 +02:00
sliver: SliverIgnorePointer(
ignoring: isEditMode,
sliver: SliverOpacity(
opacity: isEditMode ? .25 : 1,
2021-07-06 07:10:36 +02:00
sliver: buildItemStreamList(
maxCrossAxisExtent: thumbSize.toDouble(),
),
2021-07-02 13:55:52 +02:00
),
),
2021-04-10 06:28:12 +02:00
),
2021-04-23 09:55:53 +02:00
],
),
2021-04-10 06:28:12 +02:00
),
);
}
Widget _buildAppBar(BuildContext context) {
2021-07-02 13:55:52 +02:00
if (isEditMode) {
return _buildEditAppBar(context);
} else if (isSelectionMode) {
return _buildSelectionAppBar(context);
} else {
return buildNormalAppBar(context, widget.account, _album);
}
2021-04-10 06:28:12 +02:00
}
2021-07-02 13:55:52 +02:00
Widget _buildSelectionAppBar(BuildContext context) {
return buildSelectionAppBar(context, [
IconButton(
icon: const Icon(Icons.remove),
tooltip: AppLocalizations.of(context).removeSelectedFromAlbumTooltip,
onPressed: () {
_onSelectionAppBarRemovePressed();
},
)
]);
}
Widget _buildEditAppBar(BuildContext context) {
return buildEditAppBar(context, widget.account, widget.album);
}
2021-04-23 09:55:53 +02:00
void _onItemTap(int index) {
Navigator.pushNamed(context, Viewer.routeName,
arguments: ViewerArguments(widget.account, _backingFiles, index));
2021-04-10 06:28:12 +02:00
}
void _onSelectionAppBarRemovePressed() {
// currently album's are auto sorted by date, so it's ok to remove items w/o
// preserving the order. this will be problematic if we want to allow custom
// sorting later
final selectedIndexes =
2021-04-23 09:55:53 +02:00
selectedListItems.map((e) => itemStreamListItems.indexOf(e)).toList();
2021-04-10 06:28:12 +02:00
final selectedFiles = _backingFiles.takeIndex(selectedIndexes).toList();
2021-06-24 18:26:56 +02:00
final newItems = _getAlbumItemsOf(_album).where((element) {
2021-04-10 06:28:12 +02:00
if (element is AlbumFileItem) {
2021-04-15 20:44:25 +02:00
return !selectedFiles.any((select) => select.path == element.file.path);
2021-04-10 06:28:12 +02:00
} else {
return true;
}
}).toList();
final albumRepo = AlbumRepo(AlbumCachedDataSource());
final newAlbum = _album.copyWith(
2021-06-24 18:26:56 +02:00
provider: AlbumStaticProvider(
items: newItems,
),
2021-04-10 06:28:12 +02:00
);
UpdateAlbum(albumRepo)(widget.account, newAlbum).then((_) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(AppLocalizations.of(context)
.removeSelectedFromAlbumSuccessNotification(
selectedIndexes.length)),
duration: k.snackBarDurationNormal,
));
setState(() {
_album = newAlbum;
_transformItems();
initCover(widget.account, _backingFiles);
2021-04-10 06:28:12 +02:00
});
}).catchError((e, stacktrace) {
2021-04-27 22:06:16 +02:00
_log.shout("[_onSelectionRemovePressed] Failed while updating album", e,
2021-04-10 06:28:12 +02:00
stacktrace);
SnackBarManager().showSnackBar(SnackBar(
content: Text(
"${AppLocalizations.of(context).removeSelectedFromAlbumFailureNotification}: "
"${exception_util.toUserString(e, context)}"),
duration: k.snackBarDurationNormal,
));
});
setState(() {
2021-04-23 09:55:53 +02:00
clearSelectedItems();
2021-04-10 06:28:12 +02:00
});
}
void _transformItems() {
2021-07-07 20:40:43 +02:00
_backingFiles = _album.sortProvider.sort(_getAlbumItemsOf(_album))
2021-04-10 06:28:12 +02:00
.whereType<AlbumFileItem>()
.map((e) => e.file)
.where((element) => file_util.isSupportedFormat(element))
2021-07-07 20:40:43 +02:00
.toList();
2021-04-10 06:28:12 +02:00
2021-05-06 13:36:20 +02:00
itemStreamListItems = () sync* {
for (int i = 0; i < _backingFiles.length; ++i) {
final f = _backingFiles[i];
final previewUrl = api_util.getFilePreviewUrl(widget.account, f,
width: thumbSize, height: thumbSize);
2021-05-06 13:36:20 +02:00
if (file_util.isSupportedImageFormat(f)) {
yield _ImageListItem(
2021-06-22 07:24:37 +02:00
file: f,
2021-05-06 13:36:20 +02:00
account: widget.account,
previewUrl: previewUrl,
onTap: () => _onItemTap(i),
);
} else if (file_util.isSupportedVideoFormat(f)) {
yield _VideoListItem(
account: widget.account,
previewUrl: previewUrl,
onTap: () => _onItemTap(i),
);
} else {
_log.shout(
"[_transformItems] Unsupported file format: ${f.contentType}");
}
}
2021-07-07 20:51:34 +02:00
}()
.toList();
2021-04-10 06:28:12 +02:00
}
bool _shouldPropagateResyncedAlbum(Album album) {
2021-06-24 18:26:56 +02:00
final origItems = _getAlbumItemsOf(widget.album);
final resyncItems = _getAlbumItemsOf(album);
if (origItems.length != resyncItems.length) {
_log.info(
2021-06-24 18:26:56 +02:00
"[_shouldPropagateResyncedAlbum] Item length differ: ${origItems.length}, ${resyncItems.length}");
return true;
}
2021-06-24 18:26:56 +02:00
for (final z in zip([origItems, resyncItems])) {
final a = z[0], b = z[1];
bool isEqual;
if (a is AlbumFileItem && b is AlbumFileItem) {
// faster compare
isEqual = a.equals(b, isDeep: false);
} else {
isEqual = a == b;
}
if (!isEqual) {
_log.info(
"[_shouldPropagateResyncedAlbum] Item differ:\nOriginal: ${z[0]}\nResynced: ${z[1]}");
return true;
}
}
_log.info("[_shouldPropagateResyncedAlbum] false");
return false;
}
2021-06-24 18:26:56 +02:00
static List<AlbumItem> _getAlbumItemsOf(Album a) =>
AlbumStaticProvider.of(a).items;
2021-04-10 06:28:12 +02:00
Album _album;
var _backingFiles = <File>[];
2021-07-02 13:55:52 +02:00
final _editFormKey = GlobalKey<FormState>();
2021-04-10 06:28:12 +02:00
static final _log = Logger("widget.album_viewer._AlbumViewerState");
}
2021-07-06 15:03:29 +02:00
abstract class _ListItem implements SelectableItem {
_ListItem({
VoidCallback onTap,
}) : _onTap = onTap;
@override
get onTap => _onTap;
@override
get isSelectable => true;
@override
get staggeredTile => const StaggeredTile.count(1, 1);
final VoidCallback _onTap;
}
class _ImageListItem extends _ListItem {
2021-04-23 09:55:53 +02:00
_ImageListItem({
2021-06-22 07:24:37 +02:00
@required this.file,
2021-04-23 09:55:53 +02:00
@required this.account,
@required this.previewUrl,
VoidCallback onTap,
2021-07-06 15:03:29 +02:00
}) : super(onTap: onTap);
2021-04-10 06:28:12 +02:00
2021-04-23 09:55:53 +02:00
@override
buildWidget(BuildContext context) {
2021-05-06 13:29:20 +02:00
return PhotoListImage(
account: account,
previewUrl: previewUrl,
2021-06-22 07:24:37 +02:00
isGif: file.contentType == "image/gif",
2021-04-23 09:55:53 +02:00
);
}
2021-04-10 06:28:12 +02:00
2021-06-22 07:24:37 +02:00
final File file;
2021-04-23 09:55:53 +02:00
final Account account;
2021-04-10 06:28:12 +02:00
final String previewUrl;
}
2021-05-06 13:36:20 +02:00
2021-07-06 15:03:29 +02:00
class _VideoListItem extends _ListItem {
2021-05-06 13:36:20 +02:00
_VideoListItem({
@required this.account,
@required this.previewUrl,
VoidCallback onTap,
2021-07-06 15:03:29 +02:00
}) : super(onTap: onTap);
2021-05-06 13:36:20 +02:00
@override
buildWidget(BuildContext context) {
return PhotoListVideo(
account: account,
previewUrl: previewUrl,
);
}
final Account account;
final String previewUrl;
}