Can only remove owned items from album

This commit is contained in:
Ming Ming 2021-11-18 16:05:33 +08:00
parent 31baba7a8a
commit 0eef1a88cc
2 changed files with 50 additions and 12 deletions

View file

@ -284,6 +284,7 @@ class _AlbumBrowserState extends State<AlbumBrowser>
PopupMenuButton<_SelectionMenuOption>(
tooltip: MaterialLocalizations.of(context).moreButtonTooltip,
itemBuilder: (context) => [
if (_canRemoveSelection)
PopupMenuItem(
value: _SelectionMenuOption.removeFromAlbum,
child: Text(L10n.global().removeFromAlbumTooltip),
@ -424,8 +425,14 @@ class _AlbumBrowserState extends State<AlbumBrowser>
Future<void> _onSelectionRemovePressed() async {
final selectedIndexes =
selectedListItems.map((e) => (e as _ListItem).index).toList();
final selectedItems = _sortedItems.takeIndex(selectedIndexes).toList();
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.username) == true ||
element.addedBy == widget.account.username)
.toList();
setState(() {
clearSelectedItems();
});
@ -437,8 +444,8 @@ class _AlbumBrowserState extends State<AlbumBrowser>
await RemoveFromAlbum(albumRepo, shareRepo, fileRepo, AppDb(), Pref())(
widget.account, _album!, selectedItems);
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().removeSelectedFromAlbumSuccessNotification(
selectedIndexes.length)),
content: Text(L10n.global()
.removeSelectedFromAlbumSuccessNotification(selectedItems.length)),
duration: k.snackBarDurationNormal,
));
} catch (e, stackTrace) {
@ -772,6 +779,17 @@ class _AlbumBrowserState extends State<AlbumBrowser>
widget.account, album, items);
}
bool get _canRemoveSelection {
if (_album!.albumFile!.isOwned(widget.account.username) == true) {
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.username);
}
static List<AlbumItem> _getAlbumItemsOf(Album a) =>
AlbumStaticProvider.of(a).items;

View file

@ -123,10 +123,7 @@ class _ViewerDetailPaneState extends State<ViewerDetailPane> {
scrollDirection: Axis.horizontal,
child: Row(
children: [
if (widget.album != null &&
widget.album!.albumFile?.isOwned(widget.account.username) ==
true &&
widget.album!.provider is AlbumStaticProvider)
if (_canRemoveFromAlbum)
_DetailPaneButton(
icon: Icons.remove_outlined,
label: L10n.global().removeFromAlbumTooltip,
@ -460,6 +457,27 @@ class _ViewerDetailPaneState extends State<ViewerDetailPane> {
return product;
}
bool _checkCanRemoveFromAlbum() {
if (widget.album == null ||
widget.album!.provider is! AlbumStaticProvider) {
return false;
}
if (widget.album!.albumFile?.isOwned(widget.account.username) == true) {
return true;
}
try {
final thisItem = AlbumStaticProvider.of(widget.album!)
.items
.whereType<AlbumFileItem>()
.firstWhere(
(element) => element.file.compareServerIdentity(widget.file));
if (thisItem.addedBy == widget.account.username) {
return true;
}
} catch (_) {}
return false;
}
late DateTime _dateTime;
// EXIF data
String? _model;
@ -469,6 +487,8 @@ class _ViewerDetailPaneState extends State<ViewerDetailPane> {
int? _isoSpeedRatings;
Tuple2<double, double>? _gps;
late final bool _canRemoveFromAlbum = _checkCanRemoveFromAlbum();
static final _log =
Logger("widget.viewer_detail_pane._ViewerDetailPaneState");
}