nc-photos/app/lib/widget/album_browser_mixin.dart

263 lines
7.6 KiB
Dart
Raw Normal View History

2022-07-28 18:59:26 +02:00
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:kiwi/kiwi.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/di_container.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/album/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/album_browser_util.dart' as album_browser_util;
2021-12-19 12:44:41 +01:00
import 'package:nc_photos/widget/photo_list_util.dart' as photo_list_util;
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';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
2022-12-16 16:01:04 +01:00
part 'album_browser_mixin.g.dart';
@npLog
mixin AlbumBrowserMixin<T extends StatefulWidget>
on SelectableItemStreamListMixin<T> {
@override
initState() {
super.initState();
2021-10-27 22:40:54 +02:00
_thumbZoomLevel = Pref().getAlbumBrowserZoomLevelOr(0);
}
@protected
void initCover(Account account, Album album) {
try {
final coverFile = album.coverProvider.getCover(album);
_coverPreviewUrl = api_util.getFilePreviewUrl(account, coverFile!,
width: k.coverSize, height: k.coverSize, isKeepAspectRatio: false);
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();
});
2021-10-27 22:40:54 +02:00
Pref().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
2021-12-19 12:44:41 +01:00
int get thumbSize => photo_list_util.getThumbSize(_thumbZoomLevel);
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}'");
final c = KiwiContainer().resolve<DiContainer>();
try {
await NotifiedAction(
() async {
final albumRepo = AlbumRepo(AlbumCachedDataSource(c));
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
}
2022-07-28 20:45:43 +02:00
Future<void> _onAddToCollectionPressed(
BuildContext context, Account account, Album album) async {
Album? newAlbum;
try {
2021-10-17 12:00:54 +02:00
await NotifiedAction(
() async {
newAlbum = await ImportPendingSharedAlbum(
KiwiContainer().resolve<DiContainer>())(account, album);
2021-10-17 12:00:54 +02:00
},
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(
2022-07-08 16:52:18 +02:00
"[_onAddToCollectionPressed] Failed while _onAddToCollectionPressed: ${logFilename(album.albumFile?.path)}",
e,
stackTrace);
}
2022-07-08 16:52:18 +02:00
if (newAlbum != null && mounted) {
2023-04-16 12:29:23 +02:00
album_browser_util.pushReplacement(context, account, newAlbum!);
}
}
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 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;
}