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

View file

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