Unset album cover

This commit is contained in:
Ming Ming 2021-08-31 20:05:10 +08:00
parent 12aedf4fbe
commit db73464232
4 changed files with 106 additions and 29 deletions

View file

@ -735,6 +735,22 @@
"@emptyTrashbinConfirmationDialogContent": {
"description": "Make sure the user wants to delete all items"
},
"unsetAlbumCoverTooltip": "Unset cover",
"@unsetAlbumCoverTooltip": {
"description": "Unset the cover of the opened album"
},
"unsetAlbumCoverProcessingNotification": "Unsetting cover",
"@unsetAlbumCoverProcessingNotification": {
"description": "Unsetting the cover of the opened album"
},
"unsetAlbumCoverSuccessNotification": "Unset cover successfully",
"@unsetAlbumCoverSuccessNotification": {
"description": "Unset the cover of the opened album successfully"
},
"unsetAlbumCoverFailureNotification": "Failed unsetting cover",
"@unsetAlbumCoverFailureNotification": {
"description": "Cannot unset the cover of the opened album"
},
"errorUnauthenticated": "Unauthenticated access. Please sign-in again if the problem continues",
"@errorUnauthenticated": {

View file

@ -36,7 +36,11 @@
"fileSharedByDescription",
"emptyTrashbinTooltip",
"emptyTrashbinConfirmationDialogTitle",
"emptyTrashbinConfirmationDialogContent"
"emptyTrashbinConfirmationDialogContent",
"unsetAlbumCoverTooltip",
"unsetAlbumCoverProcessingNotification",
"unsetAlbumCoverSuccessNotification",
"unsetAlbumCoverFailureNotification"
],
"es": [
@ -45,7 +49,11 @@
"settingsFollowSystemThemeTitle",
"settingsUseBlackInDarkThemeTitle",
"settingsUseBlackInDarkThemeTrueDescription",
"settingsUseBlackInDarkThemeFalseDescription"
"settingsUseBlackInDarkThemeFalseDescription",
"unsetAlbumCoverTooltip",
"unsetAlbumCoverProcessingNotification",
"unsetAlbumCoverSuccessNotification",
"unsetAlbumCoverFailureNotification"
],
"fr": [
@ -65,7 +73,11 @@
"fileSharedByDescription",
"emptyTrashbinTooltip",
"emptyTrashbinConfirmationDialogTitle",
"emptyTrashbinConfirmationDialogContent"
"emptyTrashbinConfirmationDialogContent",
"unsetAlbumCoverTooltip",
"unsetAlbumCoverProcessingNotification",
"unsetAlbumCoverSuccessNotification",
"unsetAlbumCoverFailureNotification"
],
"ru": [
@ -85,6 +97,10 @@
"fileSharedByDescription",
"emptyTrashbinTooltip",
"emptyTrashbinConfirmationDialogTitle",
"emptyTrashbinConfirmationDialogContent"
"emptyTrashbinConfirmationDialogContent",
"unsetAlbumCoverTooltip",
"unsetAlbumCoverProcessingNotification",
"unsetAlbumCoverSuccessNotification",
"unsetAlbumCoverFailureNotification"
]
}

View file

@ -6,15 +6,18 @@ import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api_util.dart' as api_util;
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/cover_provider.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file/data_source.dart';
import 'package:nc_photos/exception_util.dart' as exception_util;
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/lab.dart';
import 'package:nc_photos/notified_action.dart';
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/remote_storage_util.dart' as remote_storage_util;
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/use_case/import_pending_shared_album.dart';
import 'package:nc_photos/use_case/update_album.dart';
import 'package:nc_photos/widget/album_browser_app_bar.dart';
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
import 'package:nc_photos/widget/selection_app_bar.dart';
@ -47,6 +50,18 @@ mixin AlbumBrowserMixin<T extends StatefulWidget>
List<PopupMenuEntry<int>> Function(BuildContext)? menuItemBuilder,
void Function(int)? onSelectedMenuItem,
}) {
final menuItems = [
if (canEdit)
PopupMenuItem(
value: _menuValueEdit,
child: Text(L10n.global().editAlbumMenuLabel),
),
if (album.coverProvider is AlbumManualCoverProvider)
PopupMenuItem(
value: _menuValueUnsetCover,
child: Text(L10n.global().unsetAlbumCoverTooltip),
),
];
return AlbumBrowserAppBar(
account: account,
album: album,
@ -79,32 +94,15 @@ mixin AlbumBrowserMixin<T extends StatefulWidget>
tooltip: "Add to collection",
),
...(actions ?? []),
if (menuItemBuilder != null || canEdit)
if (menuItemBuilder != null || menuItems.isNotEmpty)
PopupMenuButton<int>(
tooltip: MaterialLocalizations.of(context).moreButtonTooltip,
itemBuilder: (context) => [
if (canEdit)
PopupMenuItem(
value: -1,
child: Text(L10n.global().editAlbumMenuLabel),
),
...menuItems,
...(menuItemBuilder?.call(context) ?? []),
],
onSelected: (option) {
if (option >= 0) {
onSelectedMenuItem?.call(option);
} else {
switch (option) {
case _menuValueEdit:
_onAppBarEditPressed(context, album);
break;
default:
_log.shout("[buildNormalAppBar] Unknown value: $option");
break;
}
}
},
onSelected: (option) => _onMenuOptionSelected(
option, account, album, onSelectedMenuItem),
),
],
);
@ -189,7 +187,28 @@ mixin AlbumBrowserMixin<T extends StatefulWidget>
}
}
void _onAppBarEditPressed(BuildContext context, Album album) {
void _onMenuOptionSelected(int option, Account account, Album album,
void Function(int)? onSelectedMenuItem) {
if (option >= 0) {
onSelectedMenuItem?.call(option);
} else {
switch (option) {
case _menuValueEdit:
_onAppBarEditPressed(album);
break;
case _menuValueUnsetCover:
_onUnsetCoverPressed(account, album);
break;
default:
_log.shout("[_onMenuOptionSelected] Unknown value: $option");
break;
}
}
}
void _onAppBarEditPressed(Album album) {
setState(() {
_isEditMode = true;
enterEditMode();
@ -197,6 +216,29 @@ mixin AlbumBrowserMixin<T extends StatefulWidget>
});
}
Future<void> _onUnsetCoverPressed(Account account, Album album) async {
_log.info("[_onUnsetCoverPressed] Unset album cover for '${album.name}'");
await NotifiedAction(
() async {
final albumRepo = AlbumRepo(AlbumCachedDataSource());
try {
await UpdateAlbum(albumRepo).call(
account,
album.copyWith(
coverProvider: AlbumAutoCoverProvider(),
));
} catch (e, stackTrace) {
_log.shout("[_onUnsetCoverPressed] Failed while updating album", e,
stackTrace);
rethrow;
}
},
L10n.global().unsetAlbumCoverProcessingNotification,
L10n.global().unsetAlbumCoverSuccessNotification,
failureText: L10n.global().unsetAlbumCoverFailureNotification,
)();
}
void _onSharePressed(BuildContext context, Account account, Album album) {
showDialog(
context: context,
@ -247,6 +289,7 @@ mixin AlbumBrowserMixin<T extends StatefulWidget>
static final _log = Logger("widget.album_browser_mixin.AlbumBrowserMixin");
static const _menuValueEdit = -1;
static const _menuValueUnsetCover = -2;
}
class _EditFormValue {

View file

@ -168,8 +168,8 @@ class _DynamicAlbumBrowserState extends State<DynamicAlbumBrowser>
});
}
void _updateAlbumPostPopulate(List<AlbumItem> items) {
List<File> timeDescSortedFiles;
Future<void> _updateAlbumPostPopulate(List<AlbumItem> items) async {
final List<File> timeDescSortedFiles;
if (widget.album.sortProvider is AlbumTimeSortProvider) {
if ((widget.album.sortProvider as AlbumTimeSortProvider).isAscending) {
timeDescSortedFiles = _backingFiles.reversed.toList();
@ -204,7 +204,8 @@ class _DynamicAlbumBrowserState extends State<DynamicAlbumBrowser>
_album = albumUpdatedTime;
if (shouldUpdate) {
UpdateAlbum(AlbumRepo(AlbumCachedDataSource()))(widget.account, _album!);
await UpdateAlbum(AlbumRepo(AlbumCachedDataSource()))(
widget.account, _album!);
}
}
@ -509,6 +510,7 @@ class _DynamicAlbumBrowserState extends State<DynamicAlbumBrowser>
setState(() {
_album = ev.album;
initCover(widget.account, ev.album);
_updateAlbumPostPopulate(_sortedItems);
});
}
}