nc-photos/lib/widget/album_browser_mixin.dart

266 lines
7.4 KiB
Dart
Raw Normal View History

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2021-07-02 13:55:52 +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-04 14:35:04 +02:00
import 'package:nc_photos/debug_util.dart';
import 'package:nc_photos/entity/album.dart';
2021-08-31 14:05:10 +02:00
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/k.dart' as k;
2021-08-31 14:05:10 +02:00
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/use_case/import_pending_shared_album.dart';
2021-08-31 14:05:10 +02:00
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';
2021-07-25 16:27:19 +02:00
import 'package:nc_photos/widget/selection_app_bar.dart';
2021-07-25 10:52:24 +02:00
import 'package:nc_photos/widget/zoom_menu_button.dart';
mixin AlbumBrowserMixin<T extends StatefulWidget>
on SelectableItemStreamListMixin<T> {
@override
initState() {
super.initState();
_thumbZoomLevel = Pref.inst().getAlbumBrowserZoomLevelOr(0);
}
@protected
void initCover(Account account, Album album) {
try {
final coverFile = album.coverProvider.getCover(album);
_coverPreviewUrl = api_util.getFilePreviewUrl(account, coverFile!,
2021-09-16 12:25:08 +02:00
width: k.coverSize, height: k.coverSize);
2021-08-13 22:04:17 +02:00
} catch (_) {}
}
@protected
2021-06-30 07:11:02 +02:00
Widget buildNormalAppBar(
BuildContext context,
Account account,
Album album, {
2021-07-23 22:05:57 +02:00
List<Widget>? actions,
List<PopupMenuEntry<int>> Function(BuildContext)? menuItemBuilder,
void Function(int)? onSelectedMenuItem,
2021-06-30 07:11:02 +02:00
}) {
2021-08-31 14:05:10 +02:00
final menuItems = [
if (canEdit)
PopupMenuItem(
value: _menuValueEdit,
child: Text(L10n.global().editAlbumMenuLabel),
),
if (canEdit && album.coverProvider is AlbumManualCoverProvider)
2021-08-31 14:05:10 +02:00
PopupMenuItem(
value: _menuValueUnsetCover,
child: Text(L10n.global().unsetAlbumCoverTooltip),
),
];
return AlbumBrowserAppBar(
2021-07-25 10:42:27 +02:00
account: account,
album: album,
coverPreviewUrl: _coverPreviewUrl,
actions: [
2021-07-25 10:52:24 +02:00
ZoomMenuButton(
initialZoom: _thumbZoomLevel,
minZoom: 0,
maxZoom: 2,
onZoomChanged: (value) {
setState(() {
_thumbZoomLevel = value.round();
});
Pref.inst().setAlbumBrowserZoomLevel(_thumbZoomLevel);
2021-07-25 10:52:24 +02:00
},
),
if (album.albumFile?.path.startsWith(
remote_storage_util.getRemotePendingSharedAlbumsDir(account)) ==
true)
IconButton(
onPressed: () => _onAddToCollectionPressed(context, account, album),
icon: const Icon(Icons.library_add),
2021-10-17 12:14:59 +02:00
tooltip: L10n.global().addToCollectionTooltip,
),
2021-06-30 07:11:02 +02:00
...(actions ?? []),
2021-08-31 14:05:10 +02:00
if (menuItemBuilder != null || menuItems.isNotEmpty)
PopupMenuButton<int>(
tooltip: MaterialLocalizations.of(context).moreButtonTooltip,
itemBuilder: (context) => [
2021-08-31 14:05:10 +02:00
...menuItems,
...(menuItemBuilder?.call(context) ?? []),
],
2021-08-31 14:05:10 +02:00
onSelected: (option) => _onMenuOptionSelected(
option, account, album, onSelectedMenuItem),
),
],
);
}
2021-07-02 13:54:18 +02:00
@protected
Widget buildSelectionAppBar(BuildContext context, List<Widget> actions) {
2021-07-25 16:27:19 +02:00
return SelectionAppBar(
count: selectedListItems.length,
onClosePressed: () {
setState(() {
clearSelectedItems();
});
},
actions: actions,
);
}
2021-07-02 13:55:52 +02:00
@protected
Widget buildEditAppBar(
BuildContext context,
Account account,
Album album, {
2021-07-23 22:05:57 +02:00
List<Widget>? actions,
2021-07-02 13:55:52 +02:00
}) {
return AlbumBrowserEditAppBar(
2021-07-25 10:42:27 +02:00
account: account,
album: album,
coverPreviewUrl: _coverPreviewUrl,
2021-07-02 13:55:52 +02:00
actions: actions,
2021-07-25 10:42:27 +02:00
onDonePressed: () {
if (validateEditMode()) {
setState(() {
_isEditMode = false;
});
doneEditMode();
}
},
onAlbumNameSaved: (value) {
_editFormValue.name = value;
},
2021-07-02 13:55:52 +02:00
);
}
@protected
2021-07-25 22:06:31 +02:00
bool get isEditMode => _isEditMode;
2021-07-02 13:55:52 +02:00
@protected
bool get canEdit => true;
@protected
@mustCallSuper
void enterEditMode() {}
/// Validates the pending modifications
2021-07-02 13:55:52 +02:00
@protected
bool validateEditMode() => true;
@protected
void doneEditMode() {}
2021-07-02 13:55:52 +02:00
/// Return a new album with the edits
@protected
Album makeEdited(Album album) {
return album.copyWith(
name: _editFormValue.name,
);
}
@protected
int get thumbSize {
switch (_thumbZoomLevel) {
case 1:
return 176;
case 2:
return 256;
case 0:
default:
return 112;
}
}
2021-08-31 14:05:10 +02:00
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) {
2021-07-02 13:55:52 +02:00
setState(() {
_isEditMode = true;
enterEditMode();
2021-07-02 13:55:52 +02:00
_editFormValue = _EditFormValue();
});
}
2021-08-31 14:05:10 +02:00
Future<void> _onUnsetCoverPressed(Account account, Album album) async {
_log.info("[_onUnsetCoverPressed] Unset album cover for '${album.name}'");
try {
await NotifiedAction(
() async {
final albumRepo = AlbumRepo(AlbumCachedDataSource());
2021-09-22 18:33:55 +02:00
await UpdateAlbum(albumRepo)(
2021-08-31 14:05:10 +02:00
account,
album.copyWith(
coverProvider: AlbumAutoCoverProvider(),
));
},
L10n.global().unsetAlbumCoverProcessingNotification,
L10n.global().unsetAlbumCoverSuccessNotification,
failureText: L10n.global().unsetAlbumCoverFailureNotification,
)();
} catch (e, stackTrace) {
_log.shout(
"[_onUnsetCoverPressed] Failed while updating album", e, stackTrace);
}
2021-08-31 14:05:10 +02:00
}
void _onAddToCollectionPressed(
BuildContext context, Account account, Album album) async {
Navigator.of(context).pop();
try {
2021-10-17 12:00:54 +02:00
await NotifiedAction(
() async {
const fileRepo = FileRepo(FileWebdavDataSource());
await ImportPendingSharedAlbum(fileRepo)(account, album.albumFile!);
},
2021-10-17 12:14:59 +02:00
L10n.global().addToCollectionProcessingNotification(album.name),
L10n.global().addToCollectionSuccessNotification(album.name),
2021-10-17 12:00:54 +02:00
)();
} catch (e, stackTrace) {
_log.shout(
2021-10-17 12:00:54 +02:00
"[_onAddToCollectionPressed] Failed while ImportPendingSharedAlbum" +
2021-09-04 14:35:04 +02:00
(shouldLogFileName ? ": ${album.albumFile?.path}" : ""),
e,
stackTrace);
}
}
2021-07-23 22:05:57 +02:00
String? _coverPreviewUrl;
var _thumbZoomLevel = 0;
2021-07-02 13:55:52 +02:00
var _isEditMode = false;
var _editFormValue = _EditFormValue();
static final _log = Logger("widget.album_browser_mixin.AlbumBrowserMixin");
2021-07-02 13:55:52 +02:00
static const _menuValueEdit = -1;
2021-08-31 14:05:10 +02:00
static const _menuValueUnsetCover = -2;
2021-07-02 13:55:52 +02:00
}
class _EditFormValue {
2021-07-23 22:05:57 +02:00
late String name;
}