2022-07-28 18:59:26 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-12-07 19:42:25 +01:00
|
|
|
import 'package:kiwi/kiwi.dart';
|
2021-07-02 13:55:52 +02:00
|
|
|
import 'package:logging/logging.dart';
|
2021-06-26 16:28:21 +02:00
|
|
|
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';
|
2021-12-07 19:42:25 +01:00
|
|
|
import 'package:nc_photos/di_container.dart';
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:nc_photos/entity/album.dart';
|
2021-08-31 14:05:10 +02:00
|
|
|
import 'package:nc_photos/entity/album/cover_provider.dart';
|
2022-07-05 22:20:24 +02:00
|
|
|
import 'package:nc_photos/entity/album/data_source.dart';
|
2021-08-21 11:46:00 +02:00
|
|
|
import 'package:nc_photos/k.dart' as k;
|
2021-08-31 14:05:10 +02:00
|
|
|
import 'package:nc_photos/notified_action.dart';
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:nc_photos/pref.dart';
|
2021-08-21 11:46:00 +02:00
|
|
|
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';
|
2021-07-31 19:16:14 +02:00
|
|
|
import 'package:nc_photos/widget/album_browser_app_bar.dart';
|
2021-11-21 14:52:10 +01:00
|
|
|
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;
|
2021-06-26 16:28:21 +02:00
|
|
|
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';
|
2021-06-26 16:28:21 +02:00
|
|
|
|
2022-12-16 16:01:04 +01:00
|
|
|
part 'album_browser_mixin.g.dart';
|
|
|
|
|
|
|
|
@npLog
|
2021-07-31 19:16:14 +02:00
|
|
|
mixin AlbumBrowserMixin<T extends StatefulWidget>
|
2021-06-26 16:28:21 +02:00
|
|
|
on SelectableItemStreamListMixin<T> {
|
|
|
|
@override
|
|
|
|
initState() {
|
|
|
|
super.initState();
|
2021-10-27 22:40:54 +02:00
|
|
|
_thumbZoomLevel = Pref().getAlbumBrowserZoomLevelOr(0);
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@protected
|
2021-08-13 12:41:02 +02:00
|
|
|
void initCover(Account account, Album album) {
|
2021-06-26 16:28:21 +02:00
|
|
|
try {
|
2021-08-13 12:41:02 +02:00
|
|
|
final coverFile = album.coverProvider.getCover(album);
|
|
|
|
_coverPreviewUrl = api_util.getFilePreviewUrl(account, coverFile!,
|
2022-12-18 07:35:54 +01:00
|
|
|
width: k.coverSize, height: k.coverSize, isKeepAspectRatio: false);
|
2021-08-13 22:04:17 +02:00
|
|
|
} catch (_) {}
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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),
|
|
|
|
),
|
2021-09-05 13:16:04 +02:00
|
|
|
if (canEdit && album.coverProvider is AlbumManualCoverProvider)
|
2021-08-31 14:05:10 +02:00
|
|
|
PopupMenuItem(
|
|
|
|
value: _menuValueUnsetCover,
|
|
|
|
child: Text(L10n.global().unsetAlbumCoverTooltip),
|
|
|
|
),
|
|
|
|
];
|
2021-07-31 19:16:14 +02:00
|
|
|
return AlbumBrowserAppBar(
|
2021-07-25 10:42:27 +02:00
|
|
|
account: account,
|
|
|
|
album: album,
|
|
|
|
coverPreviewUrl: _coverPreviewUrl,
|
2021-06-26 16:28:21 +02:00
|
|
|
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
|
|
|
},
|
2021-06-26 16:28:21 +02:00
|
|
|
),
|
2021-08-21 11:46:00 +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-08-21 11:46:00 +02:00
|
|
|
),
|
2021-06-30 07:11:02 +02:00
|
|
|
...(actions ?? []),
|
2021-08-31 14:05:10 +02:00
|
|
|
if (menuItemBuilder != null || menuItems.isNotEmpty)
|
2021-07-25 22:25:16 +02:00
|
|
|
PopupMenuButton<int>(
|
|
|
|
tooltip: MaterialLocalizations.of(context).moreButtonTooltip,
|
|
|
|
itemBuilder: (context) => [
|
2021-08-31 14:05:10 +02:00
|
|
|
...menuItems,
|
2021-07-25 22:25:16 +02:00
|
|
|
...(menuItemBuilder?.call(context) ?? []),
|
|
|
|
],
|
2021-08-31 14:05:10 +02:00
|
|
|
onSelected: (option) => _onMenuOptionSelected(
|
|
|
|
option, account, album, onSelectedMenuItem),
|
2021-07-25 22:25:16 +02:00
|
|
|
),
|
2021-06-26 16:28:21 +02:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-02 13:54:18 +02:00
|
|
|
@protected
|
2021-06-26 16:28:21 +02:00
|
|
|
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-06-26 16:28:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}) {
|
2021-07-31 19:16:14 +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
|
|
|
|
2021-07-25 22:25:16 +02:00
|
|
|
@protected
|
|
|
|
bool get canEdit => true;
|
|
|
|
|
2021-07-08 06:13:36 +02:00
|
|
|
@protected
|
|
|
|
@mustCallSuper
|
|
|
|
void enterEditMode() {}
|
|
|
|
|
2021-07-08 21:26:14 +02:00
|
|
|
/// Validates the pending modifications
|
2021-07-02 13:55:52 +02:00
|
|
|
@protected
|
2021-08-10 12:11:28 +02:00
|
|
|
bool validateEditMode() => true;
|
2021-07-08 21:26:14 +02:00
|
|
|
|
|
|
|
@protected
|
2021-08-10 12:11:28 +02:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-26 16:28:21 +02:00
|
|
|
@protected
|
2021-12-19 12:44:41 +01:00
|
|
|
int get thumbSize => photo_list_util.getThumbSize(_thumbZoomLevel);
|
2021-06-26 16:28:21 +02:00
|
|
|
|
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;
|
2021-07-08 06:13:36 +02:00
|
|
|
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}'");
|
2022-07-05 22:20:24 +02:00
|
|
|
final c = KiwiContainer().resolve<DiContainer>();
|
2021-09-08 11:31:24 +02:00
|
|
|
try {
|
|
|
|
await NotifiedAction(
|
|
|
|
() async {
|
2022-07-05 22:20:24 +02:00
|
|
|
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(),
|
|
|
|
));
|
2021-09-08 11:31:24 +02:00
|
|
|
},
|
|
|
|
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(
|
2021-08-21 11:46:00 +02:00
|
|
|
BuildContext context, Account account, Album album) async {
|
2021-11-21 14:52:10 +01:00
|
|
|
Album? newAlbum;
|
2021-08-21 11:46:00 +02:00
|
|
|
try {
|
2021-10-17 12:00:54 +02:00
|
|
|
await NotifiedAction(
|
|
|
|
() async {
|
2021-12-07 19:42:25 +01:00
|
|
|
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
|
|
|
)();
|
2021-08-21 11:46:00 +02:00
|
|
|
} catch (e, stackTrace) {
|
|
|
|
_log.shout(
|
2022-07-08 16:52:18 +02:00
|
|
|
"[_onAddToCollectionPressed] Failed while _onAddToCollectionPressed: ${logFilename(album.albumFile?.path)}",
|
2021-08-21 11:46:00 +02:00
|
|
|
e,
|
|
|
|
stackTrace);
|
|
|
|
}
|
2022-07-08 16:52:18 +02:00
|
|
|
if (newAlbum != null && mounted) {
|
2022-07-28 18:59:26 +02:00
|
|
|
unawaited(
|
|
|
|
album_browser_util.pushReplacement(context, account, newAlbum!),
|
|
|
|
);
|
2021-11-21 14:52:10 +01:00
|
|
|
}
|
2021-08-21 11:46:00 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
String? _coverPreviewUrl;
|
2021-06-26 16:28:21 +02:00
|
|
|
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;
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|