nc-photos/app/lib/widget/album_browser.dart

1068 lines
30 KiB
Dart
Raw Normal View History

2022-07-28 18:59:26 +02:00
import 'dart:async';
2023-03-13 12:55:39 +01:00
import 'package:clock/clock.dart';
2022-12-08 16:39:13 +01:00
import 'package:flutter/foundation.dart';
2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
2021-07-06 15:03:29 +02:00
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:kiwi/kiwi.dart';
2021-04-10 06:28:12 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/di_container.dart';
2021-09-28 22:56:44 +02:00
import 'package:nc_photos/download_handler.dart';
2021-04-10 06:28:12 +02:00
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;
2021-08-13 22:18:35 +02:00
import 'package:nc_photos/event/event.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/exception_util.dart' as exception_util;
2022-09-07 11:37:50 +02:00
import 'package:nc_photos/flutter_util.dart' as flutter_util;
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/list_extension.dart';
2022-06-06 19:37:46 +02:00
import 'package:nc_photos/object_extension.dart';
2021-07-22 08:15:41 +02:00
import 'package:nc_photos/or_null.dart';
2021-10-04 15:53:03 +02:00
import 'package:nc_photos/pref.dart';
2021-07-08 10:57:20 +02:00
import 'package:nc_photos/session_storage.dart';
import 'package:nc_photos/share_handler.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/use_case/album/remove_from_album.dart';
import 'package:nc_photos/use_case/ls_single_file.dart';
2021-09-29 13:00:00 +02:00
import 'package:nc_photos/use_case/preprocess_album.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/use_case/update_album.dart';
import 'package:nc_photos/use_case/update_album_with_actual_items.dart';
import 'package:nc_photos/widget/album_browser_mixin.dart';
import 'package:nc_photos/widget/album_share_outlier_browser.dart';
2021-07-08 10:57:20 +02:00
import 'package:nc_photos/widget/draggable_item_list_mixin.dart';
2021-07-10 21:25:23 +02:00
import 'package:nc_photos/widget/fancy_option_picker.dart';
import 'package:nc_photos/widget/handler/add_selection_to_collection_handler.dart';
import 'package:nc_photos/widget/network_thumbnail.dart';
2021-05-06 13:29:20 +02:00
import 'package:nc_photos/widget/photo_list_item.dart';
2021-12-19 12:30:56 +01:00
import 'package:nc_photos/widget/photo_list_util.dart' as photo_list_util;
2021-04-23 09:55:53 +02:00
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
2021-10-18 11:29:55 +02:00
import 'package:nc_photos/widget/share_album_dialog.dart';
import 'package:nc_photos/widget/shared_album_info_dialog.dart';
2021-07-09 12:07:37 +02:00
import 'package:nc_photos/widget/simple_input_dialog.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/widget/viewer.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 'album_browser.g.dart';
2021-04-10 06:28:12 +02:00
class AlbumBrowserArguments {
AlbumBrowserArguments(this.account, this.album);
2021-04-10 06:28:12 +02:00
final Account account;
final Album album;
}
class AlbumBrowser extends StatefulWidget {
static const routeName = "/album-browser";
2021-04-10 06:28:12 +02:00
static Route buildRoute(AlbumBrowserArguments args) => MaterialPageRoute(
builder: (context) => AlbumBrowser.fromArgs(args),
2021-07-23 22:05:57 +02:00
);
2021-09-15 08:58:06 +02:00
const AlbumBrowser({
2021-07-23 22:05:57 +02:00
Key? key,
required this.account,
required this.album,
2021-04-10 06:28:12 +02:00
}) : super(key: key);
AlbumBrowser.fromArgs(AlbumBrowserArguments args, {Key? key})
2021-04-10 06:28:12 +02:00
: this(
key: key,
account: args.account,
album: args.album,
);
@override
createState() => _AlbumBrowserState();
2021-04-10 06:28:12 +02:00
final Account account;
final Album album;
}
2022-12-16 16:01:04 +01:00
@npLog
class _AlbumBrowserState extends State<AlbumBrowser>
with
SelectableItemStreamListMixin<AlbumBrowser>,
DraggableItemListMixin<AlbumBrowser>,
AlbumBrowserMixin<AlbumBrowser> {
_AlbumBrowserState() {
final c = KiwiContainer().resolve<DiContainer>();
assert(require(c));
assert(PreProcessAlbum.require(c));
_c = c;
}
static bool require(DiContainer c) => DiContainer.has(c, DiType.albumRepo);
2021-04-10 06:28:12 +02:00
@override
initState() {
super.initState();
_initAlbum();
2021-08-13 22:18:35 +02:00
_albumUpdatedListener =
AppEventListener<AlbumUpdatedEvent>(_onAlbumUpdatedEvent);
_albumUpdatedListener.begin();
}
@override
dispose() {
super.dispose();
_albumUpdatedListener.end();
2021-04-10 06:28:12 +02:00
}
@override
build(BuildContext context) {
2022-11-12 10:55:33 +01:00
return Scaffold(
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
),
);
}
2022-06-06 19:37:46 +02:00
@override
onItemTap(SelectableItem item, int index) {
item.as<_ListItem>()?.onTap?.call();
}
2021-09-15 08:58:06 +02:00
@override
@protected
get canEdit => _album?.albumFile?.isOwned(widget.account.userId) == true;
@override
enterEditMode() {
super.enterEditMode();
2021-07-23 22:05:57 +02:00
_editAlbum = _album!.copyWith();
2021-07-09 12:07:37 +02:00
setState(() {
_transformItems();
});
2021-07-08 10:57:20 +02:00
if (!SessionStorage().hasShowDragRearrangeNotification) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().albumEditDragRearrangeNotification),
2021-07-08 10:57:20 +02:00
duration: k.snackBarDurationNormal,
));
SessionStorage().hasShowDragRearrangeNotification = true;
}
}
@override
2021-07-23 22:05:57 +02:00
validateEditMode() => _editFormKey.currentState?.validate() == true;
2021-07-02 13:55:52 +02:00
@override
doneEditMode() {
try {
// persist the changes
2021-07-23 22:05:57 +02:00
_editFormKey.currentState!.save();
final newAlbum = makeEdited(_editAlbum!);
if (newAlbum.copyWith(lastUpdated: OrNull(_album!.lastUpdated)) !=
2021-07-22 08:15:41 +02:00
_album) {
_log.info("[doneEditMode] Album modified: $newAlbum");
2021-07-02 13:55:52 +02:00
setState(() {
_album = newAlbum;
});
UpdateAlbum(_c.albumRepo)(
2021-09-22 18:33:55 +02:00
widget.account,
newAlbum,
).catchError((e, stackTrace) {
2021-09-25 09:32:15 +02:00
_log.shout("[doneEditMode] Failed while UpdateAlbum", e, stackTrace);
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(e)),
duration: k.snackBarDurationNormal,
));
2021-07-02 13:55:52 +02:00
});
} else {
_log.fine("[doneEditMode] Album not modified");
2021-07-02 13:55:52 +02:00
}
} finally {
setState(() {
// reset edits
_editAlbum = null;
// update the list to show the real album
_transformItems();
});
2021-07-02 13:55:52 +02:00
}
}
Future<void> _initAlbum() async {
var album = await _c.albumRepo.get(widget.account, widget.album.albumFile!);
if (widget.album.shares?.isNotEmpty == true) {
try {
final file =
await LsSingleFile(_c)(widget.account, album.albumFile!.path);
if (file.etag != album.albumFile!.etag) {
_log.info("[_initAlbum] Album modified in remote, forcing download");
album = await _c.albumRepo.get(widget.account, File(path: file.path));
}
} catch (e, stackTrace) {
_log.warning("[_initAlbum] Failed while syncing remote album file", e,
stackTrace);
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(e)),
duration: k.snackBarDurationNormal,
));
}
}
await _setAlbum(album);
if (album.shares?.isNotEmpty == true) {
2022-07-28 18:59:26 +02:00
unawaited(_showSharedAlbumInfoDialog());
}
}
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(
2021-09-15 08:58:06 +02:00
child: LinearProgressIndicator(),
),
],
);
}
2021-07-08 10:57:20 +02:00
Widget content = CustomScrollView(
controller: _scrollController,
slivers: [
_buildAppBar(context),
2021-10-04 21:29:01 +02:00
isEditMode
? buildDraggableItemList(
maxCrossAxisExtent: thumbSize.toDouble(),
onMaxExtentChanged: (value) {
_itemListMaxExtent = value;
},
)
: buildItemStreamList(
maxCrossAxisExtent: thumbSize.toDouble(),
),
2021-07-08 10:57:20 +02:00
],
);
if (isEditMode) {
content = Listener(
2021-09-29 16:55:49 +02:00
onPointerMove: _onEditPointerMove,
2021-07-08 10:57:20 +02:00
child: content,
);
}
2022-11-12 10:55:33 +01:00
return buildItemStreamListOuter(context, child: content);
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 {
2021-09-29 16:34:56 +02:00
return buildNormalAppBar(
context,
widget.account,
_album!,
2021-10-18 11:29:55 +02:00
actions: [
if (_album!.albumFile!.isOwned(widget.account.userId) &&
2021-10-27 22:40:54 +02:00
Pref().isLabEnableSharedAlbumOr(false))
2021-10-18 11:29:55 +02:00
IconButton(
onPressed: () => _onSharePressed(context),
icon: const Icon(Icons.share),
tooltip: L10n.global().shareTooltip,
),
],
2021-09-29 16:34:56 +02:00
menuItemBuilder: (_) => [
if (Pref().isLabEnableSharedAlbumOr(false))
PopupMenuItem(
value: _menuValueFixShares,
child: Text(L10n.global().fixSharesTooltip),
),
2021-09-29 16:34:56 +02:00
PopupMenuItem(
value: _menuValueDownload,
child: Text(L10n.global().downloadTooltip),
),
],
onSelectedMenuItem: (option) => _onMenuSelected(context, option),
);
}
2021-04-10 06:28:12 +02:00
}
2021-07-02 13:55:52 +02:00
Widget _buildSelectionAppBar(BuildContext context) {
return buildSelectionAppBar(context, [
2021-10-09 10:42:18 +02:00
IconButton(
icon: const Icon(Icons.share),
tooltip: L10n.global().shareTooltip,
onPressed: () {
_onSelectionSharePressed(context);
},
),
2021-07-02 13:55:52 +02:00
IconButton(
icon: const Icon(Icons.add),
2023-04-28 19:50:54 +02:00
tooltip: L10n.global().addItemToCollectionTooltip,
onPressed: () => _onSelectionAddPressed(context),
2021-09-28 22:56:44 +02:00
),
PopupMenuButton<_SelectionMenuOption>(
tooltip: MaterialLocalizations.of(context).moreButtonTooltip,
itemBuilder: (context) => [
2021-11-18 09:05:33 +01:00
if (_canRemoveSelection)
PopupMenuItem(
value: _SelectionMenuOption.removeFromAlbum,
child: Text(L10n.global().removeFromAlbumTooltip),
),
2021-09-28 22:56:44 +02:00
PopupMenuItem(
value: _SelectionMenuOption.download,
child: Text(L10n.global().downloadTooltip),
),
],
onSelected: (option) => _onSelectionMenuSelected(context, option),
),
2021-07-02 13:55:52 +02:00
]);
}
Widget _buildEditAppBar(BuildContext context) {
return buildEditAppBar(context, widget.account, _album!, actions: [
2021-07-09 12:07:37 +02:00
IconButton(
2021-09-15 08:58:06 +02:00
icon: const Icon(Icons.text_fields),
tooltip: L10n.global().albumAddTextTooltip,
2021-09-29 16:55:49 +02:00
onPressed: _onEditAddTextPressed,
2021-07-09 12:07:37 +02:00
),
IconButton(
2021-09-15 08:58:06 +02:00
icon: const Icon(Icons.sort_by_alpha),
tooltip: L10n.global().sortTooltip,
2021-09-29 16:55:49 +02:00
onPressed: _onEditSortPressed,
),
]);
2021-07-02 13:55:52 +02:00
}
2021-04-23 09:55:53 +02:00
void _onItemTap(int index) {
// convert item index to file index
var fileIndex = index;
for (int i = 0; i < index; ++i) {
if (_sortedItems[i] is! AlbumFileItem ||
!file_util
.isSupportedFormat((_sortedItems[i] as AlbumFileItem).file)) {
--fileIndex;
}
}
2021-04-23 09:55:53 +02:00
Navigator.pushNamed(context, Viewer.routeName,
2023-04-17 18:15:29 +02:00
arguments: ViewerArguments(widget.account, _backingFiles, fileIndex));
2021-04-10 06:28:12 +02:00
}
2022-07-28 20:45:43 +02:00
Future<void> _onSharePressed(BuildContext context) async {
await _showSharedAlbumInfoDialog();
2021-10-18 11:29:55 +02:00
await showDialog(
context: context,
builder: (_) => ShareAlbumDialog(
account: widget.account,
album: _album!,
2021-10-18 11:29:55 +02:00
),
);
}
2021-09-29 16:34:56 +02:00
void _onMenuSelected(BuildContext context, int option) {
switch (option) {
case _menuValueDownload:
_onDownloadPressed();
break;
case _menuValueFixShares:
_onFixSharesPressed();
break;
2021-09-29 16:34:56 +02:00
default:
_log.shout("[_onMenuSelected] Unknown option: $option");
break;
}
}
void _onDownloadPressed() {
final c = KiwiContainer().resolve<DiContainer>();
DownloadHandler(c).downloadFiles(
2021-09-29 16:34:56 +02:00
widget.account,
_sortedItems.whereType<AlbumFileItem>().map((e) => e.file).toList(),
parentDir: _album!.name,
);
}
void _onFixSharesPressed() {
Navigator.of(context).pushNamed(
AlbumShareOutlierBrowser.routeName,
arguments: AlbumShareOutlierBrowserArguments(widget.account, _album!),
);
}
2021-09-29 16:55:49 +02:00
void _onSelectionMenuSelected(
BuildContext context, _SelectionMenuOption option) {
switch (option) {
case _SelectionMenuOption.download:
_onSelectionDownloadPressed();
break;
case _SelectionMenuOption.removeFromAlbum:
_onSelectionRemovePressed();
break;
2021-09-29 16:55:49 +02:00
default:
_log.shout("[_onSelectionMenuSelected] Unknown option: $option");
break;
}
}
void _onSelectionSharePressed(BuildContext context) {
final c = KiwiContainer().resolve<DiContainer>();
final selected = selectedListItems
.whereType<_FileListItem>()
.map((e) => e.file)
.toList();
if (selected.isEmpty) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().shareSelectedEmptyNotification),
duration: k.snackBarDurationNormal,
));
return;
}
2021-10-08 20:39:53 +02:00
ShareHandler(
c,
2021-10-08 20:39:53 +02:00
context: context,
clearSelection: () {
setState(() {
clearSelectedItems();
});
},
).shareFiles(widget.account, selected);
}
Future<void> _onSelectionAddPressed(BuildContext context) async {
return const AddSelectionToCollectionHandler()(
context: context,
selection: selectedListItems
.whereType<_FileListItem>()
.map((e) => e.file)
.toList(),
clearSelection: () {
if (mounted) {
setState(() {
clearSelectedItems();
});
}
},
);
}
2021-09-29 16:55:49 +02:00
Future<void> _onSelectionRemovePressed() async {
2021-04-10 06:28:12 +02:00
final selectedIndexes =
2021-11-18 09:05:33 +01:00
selectedListItems.whereType<_ListItem>().map((e) => e.index).toList();
final selectedItems = _sortedItems
.takeIndex(selectedIndexes)
// can only remove owned files
.where((element) =>
_album!.albumFile!.isOwned(widget.account.userId) == true ||
element.addedBy == widget.account.userId)
2021-11-18 09:05:33 +01:00
.toList();
2021-09-22 18:33:55 +02:00
setState(() {
clearSelectedItems();
});
2021-09-22 18:33:55 +02:00
try {
await RemoveFromAlbum(KiwiContainer().resolve<DiContainer>())(
2021-10-24 17:32:36 +02:00
widget.account, _album!, selectedItems);
2021-04-10 06:28:12 +02:00
SnackBarManager().showSnackBar(SnackBar(
2021-11-18 09:05:33 +01:00
content: Text(L10n.global()
.removeSelectedFromAlbumSuccessNotification(selectedItems.length)),
2021-04-10 06:28:12 +02:00
duration: k.snackBarDurationNormal,
));
2021-09-22 18:33:55 +02:00
} catch (e, stackTrace) {
2021-04-27 22:06:16 +02:00
_log.shout("[_onSelectionRemovePressed] Failed while updating album", e,
2021-09-22 18:33:55 +02:00
stackTrace);
2021-04-10 06:28:12 +02:00
SnackBarManager().showSnackBar(SnackBar(
content:
Text("${L10n.global().removeSelectedFromAlbumFailureNotification}: "
"${exception_util.toUserString(e)}"),
2021-04-10 06:28:12 +02:00
duration: k.snackBarDurationNormal,
));
2021-09-22 18:33:55 +02:00
}
2021-04-10 06:28:12 +02:00
}
2021-09-28 22:56:44 +02:00
void _onSelectionDownloadPressed() {
final c = KiwiContainer().resolve<DiContainer>();
2021-09-28 22:56:44 +02:00
final selected = selectedListItems
.whereType<_FileListItem>()
.map((e) => e.file)
.toList();
DownloadHandler(c).downloadFiles(widget.account, selected);
2021-09-28 22:56:44 +02:00
setState(() {
clearSelectedItems();
});
}
2021-09-29 16:55:49 +02:00
void _onEditPointerMove(PointerMoveEvent event) {
2021-07-08 10:57:20 +02:00
assert(isEditMode);
if (!_isDragging) {
return;
}
if (event.position.dy >= MediaQuery.of(context).size.height - 100) {
// near bottom of screen
if (_isDragScrollingDown == true) {
return;
}
final maxExtent =
_itemListMaxExtent ?? _scrollController.position.maxScrollExtent;
2021-09-29 16:55:49 +02:00
_log.fine("[_onEditPointerMove] Begin scrolling down");
2021-07-08 10:57:20 +02:00
if (_scrollController.offset <
_scrollController.position.maxScrollExtent) {
_scrollController.animateTo(maxExtent,
duration: Duration(
milliseconds:
((maxExtent - _scrollController.offset) * 1.6).round()),
curve: Curves.linear);
_isDragScrollingDown = true;
}
} else if (event.position.dy <= 100) {
// near top of screen
if (_isDragScrollingDown == false) {
return;
}
2021-09-29 16:55:49 +02:00
_log.fine("[_onEditPointerMove] Begin scrolling up");
2021-07-08 10:57:20 +02:00
if (_scrollController.offset > 0) {
_scrollController.animateTo(0,
duration: Duration(
milliseconds: (_scrollController.offset * 1.6).round()),
curve: Curves.linear);
_isDragScrollingDown = false;
}
} else if (_isDragScrollingDown != null) {
2021-09-29 16:55:49 +02:00
_log.fine("[_onEditPointerMove] Stop scrolling");
2021-07-08 10:57:20 +02:00
_scrollController.jumpTo(_scrollController.offset);
_isDragScrollingDown = null;
}
}
2021-09-29 16:55:49 +02:00
void _onEditItemMoved(int fromIndex, int toIndex, bool isBefore) {
2021-07-08 10:57:20 +02:00
if (fromIndex == toIndex) {
return;
}
final item = _sortedItems.removeAt(fromIndex);
final newIndex =
toIndex + (isBefore ? 0 : 1) + (fromIndex < toIndex ? -1 : 0);
_sortedItems.insert(newIndex, item);
2021-07-23 22:05:57 +02:00
_editAlbum = _editAlbum!.copyWith(
2021-09-15 08:58:06 +02:00
sortProvider: const AlbumNullSortProvider(),
2021-07-08 10:57:20 +02:00
// save the current order
provider: AlbumStaticProvider.of(_editAlbum!).copyWith(
2021-07-08 10:57:20 +02:00
items: _sortedItems,
),
);
setState(() {
_transformItems();
});
}
2021-09-29 16:55:49 +02:00
void _onEditSortPressed() {
final sortProvider = _editAlbum!.sortProvider;
showDialog(
context: context,
builder: (context) => FancyOptionPicker(
title: L10n.global().sortOptionDialogTitle,
items: [
FancyOptionPickerItem(
label: L10n.global().sortOptionTimeDescendingLabel,
isSelected: sortProvider is AlbumTimeSortProvider &&
!sortProvider.isAscending,
onSelect: () {
_onEditSortNewestPressed();
Navigator.of(context).pop();
},
),
FancyOptionPickerItem(
label: L10n.global().sortOptionTimeAscendingLabel,
isSelected: sortProvider is AlbumTimeSortProvider &&
sortProvider.isAscending,
onSelect: () {
_onEditSortOldestPressed();
Navigator.of(context).pop();
},
),
2022-05-26 06:00:55 +02:00
FancyOptionPickerItem(
label: L10n.global().sortOptionFilenameAscendingLabel,
isSelected: sortProvider is AlbumFilenameSortProvider &&
sortProvider.isAscending,
onSelect: () {
_onEditSortFilenamePressed();
Navigator.of(context).pop();
},
),
FancyOptionPickerItem(
label: L10n.global().sortOptionFilenameDescendingLabel,
isSelected: sortProvider is AlbumFilenameSortProvider &&
!sortProvider.isAscending,
onSelect: () {
_onEditSortFilenameDescendingPressed();
Navigator.of(context).pop();
},
),
2021-10-04 18:53:18 +02:00
if (sortProvider is AlbumNullSortProvider)
FancyOptionPickerItem(
label: L10n.global().sortOptionManualLabel,
isSelected: true,
onSelect: () {
Navigator.of(context).pop();
},
),
2021-09-29 16:55:49 +02:00
],
),
);
}
void _onEditSortOldestPressed() {
_editAlbum = _editAlbum!.copyWith(
sortProvider: const AlbumTimeSortProvider(isAscending: true),
);
setState(() {
_transformItems();
});
}
void _onEditSortNewestPressed() {
_editAlbum = _editAlbum!.copyWith(
sortProvider: const AlbumTimeSortProvider(isAscending: false),
);
setState(() {
_transformItems();
});
}
2022-05-26 06:00:55 +02:00
void _onEditSortFilenamePressed() {
_editAlbum = _editAlbum!.copyWith(
sortProvider: const AlbumFilenameSortProvider(isAscending: true),
);
setState(() {
_transformItems();
});
}
void _onEditSortFilenameDescendingPressed() {
_editAlbum = _editAlbum!.copyWith(
sortProvider: const AlbumFilenameSortProvider(isAscending: false),
);
setState(() {
_transformItems();
});
}
2021-09-29 16:55:49 +02:00
void _onEditAddTextPressed() {
2021-07-23 22:05:57 +02:00
showDialog<String>(
2021-07-09 12:07:37 +02:00
context: context,
2021-09-28 06:44:03 +02:00
builder: (context) => SimpleInputDialog(
buttonText: MaterialLocalizations.of(context).saveButtonLabel,
),
2021-07-09 12:07:37 +02:00
).then((value) {
if (value == null) {
return;
}
2021-07-23 22:05:57 +02:00
_editAlbum = _editAlbum!.copyWith(
provider: AlbumStaticProvider.of(_editAlbum!).copyWith(
2021-07-09 12:07:37 +02:00
items: [
AlbumLabelItem(
addedBy: widget.account.userId,
2023-03-13 12:55:39 +01:00
addedAt: clock.now(),
text: value,
),
2021-07-09 12:07:37 +02:00
..._sortedItems,
],
),
);
setState(() {
_transformItems();
});
});
}
2021-09-29 16:55:49 +02:00
void _onEditLabelItemEditPressed(AlbumLabelItem item, int index) {
2021-07-23 22:05:57 +02:00
showDialog<String>(
2021-07-09 12:07:37 +02:00
context: context,
builder: (context) => SimpleInputDialog(
2021-09-28 06:44:03 +02:00
buttonText: MaterialLocalizations.of(context).saveButtonLabel,
2021-07-09 12:07:37 +02:00
initialText: item.text,
),
).then((value) {
if (value == null) {
return;
}
_sortedItems[index] = item.copyWith(
text: value,
);
2021-07-23 22:05:57 +02:00
_editAlbum = _editAlbum!.copyWith(
provider: AlbumStaticProvider.of(_editAlbum!).copyWith(
2021-07-09 12:07:37 +02:00
items: _sortedItems,
),
);
setState(() {
_transformItems();
});
});
}
Future<void> _onAlbumUpdatedEvent(AlbumUpdatedEvent ev) async {
2021-08-13 22:18:35 +02:00
if (ev.album.albumFile!.path == _album?.albumFile?.path) {
await _setAlbum(ev.album);
2021-08-13 22:18:35 +02:00
}
}
2021-04-10 06:28:12 +02:00
void _transformItems() {
if (_editAlbum != null) {
// edit mode
// _sortedItems =
// _editAlbum!.sortProvider.sort(_getAlbumItemsOf(_editAlbum!));
_sortedItems = _getAlbumItemsOf(_editAlbum!);
} else {
// _sortedItems = _album!.sortProvider.sort(_getAlbumItemsOf(_album!));
_sortedItems = _getAlbumItemsOf(_album!);
}
2021-07-08 10:57:20 +02:00
_backingFiles = _sortedItems
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-12-19 12:30:56 +01:00
final dateHelper = photo_list_util.DateGroupHelper(
2021-10-04 15:53:03 +02:00
isMonthOnly: false,
);
2021-04-10 06:28:12 +02:00
2021-07-08 10:57:20 +02:00
final items = () sync* {
for (int i = 0; i < _sortedItems.length; ++i) {
final item = _sortedItems[i];
if (item is AlbumFileItem) {
final previewUrl =
NetworkRectThumbnail.imageUrlForFile(widget.account, item.file);
2021-10-04 15:53:03 +02:00
if ((_editAlbum ?? _album)?.sortProvider is AlbumTimeSortProvider &&
2021-10-27 22:40:54 +02:00
Pref().isAlbumBrowserShowDateOr()) {
2021-10-04 15:53:03 +02:00
final date = dateHelper.onFile(item.file);
if (date != null) {
yield _DateListItem(date: date);
}
}
if (file_util.isSupportedImageFormat(item.file)) {
yield _ImageListItem(
index: i,
file: item.file,
account: widget.account,
previewUrl: previewUrl,
onTap: () => _onItemTap(i),
onDropBefore: (dropItem) =>
2021-09-29 16:55:49 +02:00
_onEditItemMoved((dropItem as _ListItem).index, i, true),
onDropAfter: (dropItem) =>
2021-09-29 16:55:49 +02:00
_onEditItemMoved((dropItem as _ListItem).index, i, false),
onDragStarted: () {
_isDragging = true;
},
onDragEndedAny: () {
_isDragging = false;
},
);
} else if (file_util.isSupportedVideoFormat(item.file)) {
yield _VideoListItem(
index: i,
file: item.file,
account: widget.account,
previewUrl: previewUrl,
onTap: () => _onItemTap(i),
onDropBefore: (dropItem) =>
2021-09-29 16:55:49 +02:00
_onEditItemMoved((dropItem as _ListItem).index, i, true),
onDropAfter: (dropItem) =>
2021-09-29 16:55:49 +02:00
_onEditItemMoved((dropItem as _ListItem).index, i, false),
onDragStarted: () {
_isDragging = true;
},
onDragEndedAny: () {
_isDragging = false;
},
);
} else {
_log.shout(
"[_transformItems] Unsupported file format: ${item.file.contentType}");
}
2021-07-09 12:07:37 +02:00
} else if (item is AlbumLabelItem) {
if (isEditMode) {
yield _EditLabelListItem(
index: i,
text: item.text,
2021-09-29 16:55:49 +02:00
onEditPressed: () => _onEditLabelItemEditPressed(item, i),
2021-07-09 12:07:37 +02:00
onDropBefore: (dropItem) =>
2021-09-29 16:55:49 +02:00
_onEditItemMoved((dropItem as _ListItem).index, i, true),
2021-07-09 12:07:37 +02:00
onDropAfter: (dropItem) =>
2021-09-29 16:55:49 +02:00
_onEditItemMoved((dropItem as _ListItem).index, i, false),
2021-07-09 12:07:37 +02:00
onDragStarted: () {
_isDragging = true;
},
onDragEndedAny: () {
_isDragging = false;
},
);
} else {
yield _LabelListItem(
index: i,
text: item.text,
);
}
2021-05-06 13:36:20 +02:00
}
}
2021-07-07 20:51:34 +02:00
}()
.toList();
2021-07-08 10:57:20 +02:00
itemStreamListItems = items;
draggableItemList = items;
2021-04-10 06:28:12 +02:00
}
Future<void> _setAlbum(Album album) async {
assert(album.provider is AlbumStaticProvider);
final items = await PreProcessAlbum(_c)(widget.account, album);
if (album.albumFile!.isOwned(widget.account.userId)) {
album = await _updateAlbumPostResync(album, items);
}
album = album.copyWith(
provider: AlbumStaticProvider.of(album).copyWith(
items: items,
2021-07-17 16:35:26 +02:00
),
);
if (mounted) {
setState(() {
_album = album;
_transformItems();
initCover(widget.account, album);
});
}
2021-07-17 16:35:26 +02:00
}
Future<Album> _updateAlbumPostResync(
Album album, List<AlbumItem> items) async {
return await UpdateAlbumWithActualItems(_c.albumRepo)(
widget.account, album, items);
}
Future<void> _showSharedAlbumInfoDialog() {
if (!Pref().hasShownSharedAlbumInfoOr(false)) {
return showDialog(
context: context,
builder: (_) => const SharedAlbumInfoDialog(),
barrierDismissible: false,
);
} else {
return Future.value();
}
}
2021-11-18 09:05:33 +01:00
bool get _canRemoveSelection {
if (_album!.albumFile!.isOwned(widget.account.userId) == true) {
2021-11-18 09:05:33 +01:00
return true;
}
final selectedIndexes =
selectedListItems.whereType<_ListItem>().map((e) => e.index).toList();
final selectedItemsIt = _sortedItems.takeIndex(selectedIndexes);
return selectedItemsIt.any((item) => item.addedBy == widget.account.userId);
2021-11-18 09:05:33 +01:00
}
2021-06-24 18:26:56 +02:00
static List<AlbumItem> _getAlbumItemsOf(Album a) =>
AlbumStaticProvider.of(a).items;
late final DiContainer _c;
2021-07-23 22:05:57 +02:00
Album? _album;
2021-07-08 10:57:20 +02:00
var _sortedItems = <AlbumItem>[];
2021-04-10 06:28:12 +02:00
var _backingFiles = <File>[];
2021-07-08 10:57:20 +02:00
final _scrollController = ScrollController();
2021-07-23 22:05:57 +02:00
double? _itemListMaxExtent;
2021-07-08 10:57:20 +02:00
bool _isDragging = false;
// == null if not drag scrolling
2021-07-23 22:05:57 +02:00
bool? _isDragScrollingDown;
2021-07-02 13:55:52 +02:00
final _editFormKey = GlobalKey<FormState>();
2021-07-23 22:05:57 +02:00
Album? _editAlbum;
2021-07-02 13:55:52 +02:00
2021-08-13 22:18:35 +02:00
late AppEventListener<AlbumUpdatedEvent> _albumUpdatedListener;
2021-09-29 16:34:56 +02:00
static const _menuValueDownload = 0;
static const _menuValueFixShares = 1;
2021-04-10 06:28:12 +02:00
}
2021-09-28 22:56:44 +02:00
enum _SelectionMenuOption {
download,
removeFromAlbum,
2021-09-28 22:56:44 +02:00
}
2022-12-08 16:39:13 +01:00
@toString
2021-07-08 10:57:20 +02:00
abstract class _ListItem implements SelectableItem, DraggableItem {
2021-10-04 15:53:03 +02:00
const _ListItem({
2021-07-23 22:05:57 +02:00
required this.index,
2022-06-06 19:37:46 +02:00
this.onTap,
2022-09-07 11:30:23 +02:00
this.onDropBefore,
this.onDropAfter,
this.onDragStarted,
this.onDragEndedAny,
});
2021-07-06 15:03:29 +02:00
@override
2022-06-06 19:37:46 +02:00
get isTappable => onTap != null;
2021-07-06 15:03:29 +02:00
@override
get isSelectable => true;
2021-07-08 10:57:20 +02:00
@override
get isDraggable => true;
2021-07-06 15:03:29 +02:00
@override
get staggeredTile => const StaggeredTile.count(1, 1);
2021-07-09 12:07:37 +02:00
@override
buildDragFeedbackWidget(BuildContext context) => null;
2021-07-08 10:57:20 +02:00
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
2021-07-08 10:57:20 +02:00
final int index;
2022-12-08 16:39:13 +01:00
@ignore
2022-06-06 19:37:46 +02:00
final VoidCallback? onTap;
2022-09-07 11:30:23 +02:00
@override
2022-12-08 16:39:13 +01:00
@ignore
2022-09-07 11:30:23 +02:00
final DragTargetAccept<DraggableItem>? onDropBefore;
@override
2022-12-08 16:39:13 +01:00
@ignore
2022-09-07 11:30:23 +02:00
final DragTargetAccept<DraggableItem>? onDropAfter;
@override
2022-12-08 16:39:13 +01:00
@ignore
2022-09-07 11:30:23 +02:00
final VoidCallback? onDragStarted;
@override
2022-12-08 16:39:13 +01:00
@ignore
2022-09-07 11:30:23 +02:00
final VoidCallback? onDragEndedAny;
2021-07-06 15:03:29 +02:00
}
abstract class _FileListItem extends _ListItem {
_FileListItem({
2022-09-07 11:30:23 +02:00
required super.index,
2021-07-23 22:05:57 +02:00
required this.file,
2022-09-07 11:30:23 +02:00
super.onTap,
super.onDropBefore,
super.onDropAfter,
super.onDragStarted,
super.onDragEndedAny,
});
final File file;
}
class _ImageListItem extends _FileListItem {
_ImageListItem({
2022-09-07 11:30:23 +02:00
required super.index,
required super.file,
2021-07-23 22:05:57 +02:00
required this.account,
required this.previewUrl,
2022-09-07 11:30:23 +02:00
super.onTap,
super.onDropBefore,
super.onDropAfter,
super.onDragStarted,
super.onDragEndedAny,
});
2021-04-10 06:28:12 +02:00
2021-04-23 09:55:53 +02:00
@override
2022-09-07 11:30:23 +02:00
buildWidget(BuildContext context) => PhotoListImage(
account: account,
previewUrl: previewUrl,
isGif: file.contentType == "image/gif",
2022-09-07 11:37:50 +02:00
heroKey: flutter_util.getImageHeroTag(file),
2022-09-07 11:30:23 +02:00
);
2021-04-10 06:28:12 +02:00
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
class _VideoListItem extends _FileListItem {
2021-05-06 13:36:20 +02:00
_VideoListItem({
2022-09-07 11:30:23 +02:00
required super.index,
required super.file,
2021-07-23 22:05:57 +02:00
required this.account,
required this.previewUrl,
2022-09-07 11:30:23 +02:00
super.onTap,
super.onDropBefore,
super.onDropAfter,
super.onDragStarted,
super.onDragEndedAny,
});
2021-05-06 13:36:20 +02:00
@override
2022-09-07 11:30:23 +02:00
buildWidget(BuildContext context) => PhotoListVideo(
account: account,
previewUrl: previewUrl,
);
2021-05-06 13:36:20 +02:00
final Account account;
final String previewUrl;
}
2021-07-09 12:07:37 +02:00
class _LabelListItem extends _ListItem {
_LabelListItem({
2022-09-07 11:30:23 +02:00
required super.index,
2021-07-23 22:05:57 +02:00
required this.text,
2022-09-07 11:30:23 +02:00
super.onDropBefore,
super.onDropAfter,
super.onDragStarted,
super.onDragEndedAny,
});
2021-07-09 12:07:37 +02:00
@override
get staggeredTile => const StaggeredTile.extent(99, 56);
@override
2022-09-07 11:30:23 +02:00
buildWidget(BuildContext context) => PhotoListLabel(
text: text,
);
2021-07-09 12:07:37 +02:00
final String text;
}
class _EditLabelListItem extends _LabelListItem {
_EditLabelListItem({
2022-09-07 11:30:23 +02:00
required super.index,
required super.text,
2021-07-23 22:05:57 +02:00
required this.onEditPressed,
2022-09-07 11:30:23 +02:00
super.onDropBefore,
super.onDropAfter,
super.onDragStarted,
super.onDragEndedAny,
});
2021-07-09 12:07:37 +02:00
@override
2022-09-07 11:30:23 +02:00
buildWidget(BuildContext context) => PhotoListLabelEdit(
text: text,
onEditPressed: onEditPressed,
);
2021-07-09 12:07:37 +02:00
@override
buildDragFeedbackWidget(BuildContext context) {
return super.buildWidget(context);
}
2021-07-23 22:05:57 +02:00
final VoidCallback? onEditPressed;
2021-07-09 12:07:37 +02:00
}
2021-10-04 15:53:03 +02:00
class _DateListItem extends _ListItem {
const _DateListItem({
required this.date,
}) : super(index: -1);
@override
get isSelectable => false;
@override
get staggeredTile => const StaggeredTile.extent(99, 32);
@override
2022-09-07 11:30:23 +02:00
buildWidget(BuildContext context) => PhotoListDate(
date: date,
);
2021-10-04 15:53:03 +02:00
final DateTime date;
}