nc-photos/lib/widget/album_browser.dart

953 lines
27 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';
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;
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.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;
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/list_extension.dart';
2021-07-22 08:15:41 +02:00
import 'package:nc_photos/or_null.dart';
import 'package:nc_photos/platform/k.dart' as platform_k;
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/theme.dart';
2021-09-29 13:00:00 +02:00
import 'package:nc_photos/use_case/preprocess_album.dart';
import 'package:nc_photos/use_case/remove_from_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';
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';
2021-10-04 15:53:03 +02:00
import 'package:nc_photos/widget/photo_list_helper.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-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';
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;
}
class _AlbumBrowserState extends State<AlbumBrowser>
with
SelectableItemStreamListMixin<AlbumBrowser>,
DraggableItemListMixin<AlbumBrowser>,
AlbumBrowserMixin<AlbumBrowser> {
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) {
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-09-15 08:58:06 +02:00
@override
@protected
2021-08-20 19:02:13 +02:00
get canEdit => _album?.albumFile?.isOwned(widget.account.username) == 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");
final albumRepo = AlbumRepo(AlbumCachedDataSource());
2021-07-02 13:55:52 +02:00
setState(() {
_album = newAlbum;
});
2021-09-22 18:33:55 +02:00
UpdateAlbum(albumRepo)(
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 {
final albumRepo = AlbumRepo(AlbumCachedDataSource());
final album = await albumRepo.get(widget.account, widget.album.albumFile!);
await _setAlbum(album);
}
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,
);
}
2021-04-23 09:55:53 +02:00
return buildItemStreamListOuter(
context,
child: Theme(
data: Theme.of(context).copyWith(
colorScheme: Theme.of(context).colorScheme.copyWith(
secondary: AppTheme.getOverscrollIndicatorColor(context),
),
2021-04-23 09:55:53 +02:00
),
2021-07-08 10:57:20 +02:00
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!,
menuItemBuilder: (_) => [
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, [
if (platform_k.isAndroid)
IconButton(
icon: const Icon(Icons.share),
tooltip: L10n.global().shareTooltip,
onPressed: () {
2021-09-29 16:55:49 +02:00
_onSelectionSharePressed(context);
},
),
2021-07-02 13:55:52 +02:00
IconButton(
icon: const Icon(Icons.remove),
tooltip: L10n.global().removeSelectedFromAlbumTooltip,
2021-09-29 16:55:49 +02:00
onPressed: _onSelectionRemovePressed,
2021-09-28 22:56:44 +02:00
),
PopupMenuButton<_SelectionMenuOption>(
tooltip: MaterialLocalizations.of(context).moreButtonTooltip,
itemBuilder: (context) => [
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,
2021-08-13 22:18:35 +02:00
arguments: ViewerArguments(widget.account, _backingFiles, fileIndex,
2021-08-21 19:58:17 +02:00
album: _album));
2021-04-10 06:28:12 +02:00
}
2021-09-29 16:34:56 +02:00
void _onMenuSelected(BuildContext context, int option) {
switch (option) {
case _menuValueDownload:
_onDownloadPressed();
break;
default:
_log.shout("[_onMenuSelected] Unknown option: $option");
break;
}
}
void _onDownloadPressed() {
DownloadHandler().downloadFiles(
widget.account,
_sortedItems.whereType<AlbumFileItem>().map((e) => e.file).toList(),
parentDir: _album!.name,
);
}
2021-09-29 16:55:49 +02:00
void _onSelectionMenuSelected(
BuildContext context, _SelectionMenuOption option) {
switch (option) {
case _SelectionMenuOption.download:
_onSelectionDownloadPressed();
break;
default:
_log.shout("[_onSelectionMenuSelected] Unknown option: $option");
break;
}
}
void _onSelectionSharePressed(BuildContext context) {
assert(platform_k.isAndroid);
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;
}
ShareHandler().shareFiles(context, widget.account, selected).then((_) {
setState(() {
clearSelectedItems();
});
});
}
2021-09-29 16:55:49 +02:00
Future<void> _onSelectionRemovePressed() async {
2021-04-10 06:28:12 +02:00
final selectedIndexes =
selectedListItems.map((e) => (e as _ListItem).index).toList();
final selectedItems = _sortedItems.takeIndex(selectedIndexes).toList();
2021-09-22 18:33:55 +02:00
setState(() {
clearSelectedItems();
});
2021-09-22 18:33:55 +02:00
try {
final albumRepo = AlbumRepo(AlbumCachedDataSource());
await RemoveFromAlbum(albumRepo)(widget.account, _album!, selectedItems);
2021-04-10 06:28:12 +02:00
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().removeSelectedFromAlbumSuccessNotification(
selectedIndexes.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 selected = selectedListItems
.whereType<_FileListItem>()
.map((e) => e.file)
.toList();
DownloadHandler().downloadFiles(widget.account, selected);
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();
},
),
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();
});
}
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(text: value),
..._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] = AlbumLabelItem(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
2021-07-23 22:05:57 +02:00
_sortedItems =
_editAlbum!.sortProvider.sort(_getAlbumItemsOf(_editAlbum!));
} else {
2021-07-23 22:05:57 +02:00
_sortedItems = _album!.sortProvider.sort(_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-10-04 15:53:03 +02:00
final dateHelper = PhotoListDateGroupHelper(
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 = api_util.getFilePreviewUrl(
widget.account,
item.file,
2021-09-16 12:25:08 +02:00
width: k.photoThumbSize,
height: k.photoThumbSize,
2021-05-06 13:36:20 +02:00
);
2021-10-04 15:53:03 +02:00
if ((_editAlbum ?? _album)?.sortProvider is AlbumTimeSortProvider &&
Pref.inst().isAlbumBrowserShowDateOr()) {
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);
2021-09-29 13:00:00 +02:00
final items = await PreProcessAlbum()(widget.account, album);
album = album.copyWith(
provider: AlbumStaticProvider.of(album).copyWith(
items: items,
2021-07-17 16:35:26 +02:00
),
);
album = await _updateAlbumPostResync(album, items);
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 {
final albumRepo = AlbumRepo(AlbumCachedDataSource());
return await UpdateAlbumWithActualItems(albumRepo)(
widget.account, album, items);
}
2021-06-24 18:26:56 +02:00
static List<AlbumItem> _getAlbumItemsOf(Album a) =>
AlbumStaticProvider.of(a).items;
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;
static final _log = Logger("widget.album_browser._AlbumBrowserState");
2021-09-29 16:34:56 +02:00
static const _menuValueDownload = 0;
2021-04-10 06:28:12 +02:00
}
2021-09-28 22:56:44 +02:00
enum _SelectionMenuOption {
download,
}
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,
VoidCallback? onTap,
DragTargetAccept<DraggableItem>? onDropBefore,
DragTargetAccept<DraggableItem>? onDropAfter,
VoidCallback? onDragStarted,
VoidCallback? onDragEndedAny,
2021-07-08 10:57:20 +02:00
}) : _onTap = onTap,
_onDropBefore = onDropBefore,
_onDropAfter = onDropAfter,
_onDragStarted = onDragStarted,
_onDragEndedAny = onDragEndedAny;
2021-07-06 15:03:29 +02:00
@override
get onTap => _onTap;
@override
get isSelectable => true;
2021-07-08 10:57:20 +02:00
@override
get isDraggable => true;
@override
get onDropBefore => _onDropBefore;
@override
get onDropAfter => _onDropAfter;
@override
get onDragStarted => _onDragStarted;
@override
get onDragEndedAny => _onDragEndedAny;
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
toString() {
return "$runtimeType {"
"index: $index, "
"}";
}
final int index;
2021-07-23 22:05:57 +02:00
final VoidCallback? _onTap;
final DragTargetAccept<DraggableItem>? _onDropBefore;
final DragTargetAccept<DraggableItem>? _onDropAfter;
final VoidCallback? _onDragStarted;
final VoidCallback? _onDragEndedAny;
2021-07-06 15:03:29 +02:00
}
abstract class _FileListItem extends _ListItem {
_FileListItem({
2021-07-23 22:05:57 +02:00
required int index,
required this.file,
VoidCallback? onTap,
DragTargetAccept<DraggableItem>? onDropBefore,
DragTargetAccept<DraggableItem>? onDropAfter,
VoidCallback? onDragStarted,
VoidCallback? onDragEndedAny,
}) : super(
index: index,
onTap: onTap,
onDropBefore: onDropBefore,
onDropAfter: onDropAfter,
onDragStarted: onDragStarted,
onDragEndedAny: onDragEndedAny,
);
final File file;
}
class _ImageListItem extends _FileListItem {
_ImageListItem({
2021-07-23 22:05:57 +02:00
required int index,
required File file,
required this.account,
required this.previewUrl,
VoidCallback? onTap,
DragTargetAccept<DraggableItem>? onDropBefore,
DragTargetAccept<DraggableItem>? onDropAfter,
VoidCallback? onDragStarted,
VoidCallback? onDragEndedAny,
2021-07-08 10:57:20 +02:00
}) : super(
index: index,
file: file,
2021-07-08 10:57:20 +02:00
onTap: onTap,
onDropBefore: onDropBefore,
onDropAfter: onDropAfter,
onDragStarted: onDragStarted,
onDragEndedAny: onDragEndedAny,
);
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-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({
2021-07-23 22:05:57 +02:00
required int index,
required File file,
required this.account,
required this.previewUrl,
VoidCallback? onTap,
DragTargetAccept<DraggableItem>? onDropBefore,
DragTargetAccept<DraggableItem>? onDropAfter,
VoidCallback? onDragStarted,
VoidCallback? onDragEndedAny,
2021-07-08 10:57:20 +02:00
}) : super(
index: index,
file: file,
2021-07-08 10:57:20 +02:00
onTap: onTap,
onDropBefore: onDropBefore,
onDropAfter: onDropAfter,
onDragStarted: onDragStarted,
onDragEndedAny: onDragEndedAny,
);
2021-05-06 13:36:20 +02:00
@override
buildWidget(BuildContext context) {
return PhotoListVideo(
account: account,
previewUrl: previewUrl,
);
}
final Account account;
final String previewUrl;
}
2021-07-09 12:07:37 +02:00
class _LabelListItem extends _ListItem {
_LabelListItem({
2021-07-23 22:05:57 +02:00
required int index,
required this.text,
DragTargetAccept<DraggableItem>? onDropBefore,
DragTargetAccept<DraggableItem>? onDropAfter,
VoidCallback? onDragStarted,
VoidCallback? onDragEndedAny,
2021-07-09 12:07:37 +02:00
}) : super(
index: index,
onDropBefore: onDropBefore,
onDropAfter: onDropAfter,
onDragStarted: onDragStarted,
onDragEndedAny: onDragEndedAny,
);
@override
get staggeredTile => const StaggeredTile.extent(99, 56);
@override
buildWidget(BuildContext context) {
2021-08-10 12:10:22 +02:00
return PhotoListLabel(
text: text,
2021-07-09 12:07:37 +02:00
);
}
final String text;
}
class _EditLabelListItem extends _LabelListItem {
_EditLabelListItem({
2021-07-23 22:05:57 +02:00
required int index,
required String text,
required this.onEditPressed,
DragTargetAccept<DraggableItem>? onDropBefore,
DragTargetAccept<DraggableItem>? onDropAfter,
VoidCallback? onDragStarted,
VoidCallback? onDragEndedAny,
2021-07-09 12:07:37 +02:00
}) : super(
index: index,
text: text,
onDropBefore: onDropBefore,
onDropAfter: onDropAfter,
onDragStarted: onDragStarted,
onDragEndedAny: onDragEndedAny,
);
@override
buildWidget(BuildContext context) {
2021-08-10 12:10:22 +02:00
return 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
buildWidget(BuildContext context) {
return PhotoListDate(
date: date,
);
}
final DateTime date;
}