2021-06-26 16:28:21 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/widgets.dart';
|
2021-07-06 15:03:29 +02:00
|
|
|
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
|
2021-06-26 16:28:21 +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';
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:nc_photos/entity/album.dart';
|
|
|
|
import 'package:nc_photos/entity/album/cover_provider.dart';
|
2021-07-05 09:54:01 +02:00
|
|
|
import 'package:nc_photos/entity/album/item.dart';
|
2021-06-27 17:35:40 +02:00
|
|
|
import 'package:nc_photos/entity/album/provider.dart';
|
2021-07-11 06:21:35 +02:00
|
|
|
import 'package:nc_photos/entity/album/sort_provider.dart';
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:nc_photos/entity/file.dart';
|
|
|
|
import 'package:nc_photos/entity/file/data_source.dart';
|
|
|
|
import 'package:nc_photos/entity/file_util.dart' as file_util;
|
2021-08-13 22:18:35 +02:00
|
|
|
import 'package:nc_photos/event/event.dart';
|
2021-06-30 07:11:02 +02:00
|
|
|
import 'package:nc_photos/exception_util.dart' as exception_util;
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:nc_photos/iterable_extension.dart';
|
|
|
|
import 'package:nc_photos/k.dart' as k;
|
2021-07-22 08:15:41 +02:00
|
|
|
import 'package:nc_photos/or_null.dart';
|
2021-07-10 17:06:04 +02:00
|
|
|
import 'package:nc_photos/platform/k.dart' as platform_k;
|
|
|
|
import 'package:nc_photos/share_handler.dart';
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:nc_photos/snack_bar_manager.dart';
|
|
|
|
import 'package:nc_photos/theme.dart';
|
|
|
|
import 'package:nc_photos/use_case/populate_album.dart';
|
|
|
|
import 'package:nc_photos/use_case/remove.dart';
|
|
|
|
import 'package:nc_photos/use_case/update_album.dart';
|
2021-07-01 12:27:37 +02:00
|
|
|
import 'package:nc_photos/use_case/update_dynamic_album_cover.dart';
|
|
|
|
import 'package:nc_photos/use_case/update_dynamic_album_time.dart';
|
2021-07-31 19:16:14 +02:00
|
|
|
import 'package:nc_photos/widget/album_browser_mixin.dart';
|
2021-07-11 06:21:35 +02:00
|
|
|
import 'package:nc_photos/widget/fancy_option_picker.dart';
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:nc_photos/widget/photo_list_item.dart';
|
|
|
|
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
|
|
|
|
import 'package:nc_photos/widget/viewer.dart';
|
|
|
|
|
2021-07-31 19:16:14 +02:00
|
|
|
class DynamicAlbumBrowserArguments {
|
|
|
|
DynamicAlbumBrowserArguments(this.account, this.album);
|
2021-06-26 16:28:21 +02:00
|
|
|
|
|
|
|
final Account account;
|
|
|
|
final Album album;
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:16:14 +02:00
|
|
|
class DynamicAlbumBrowser extends StatefulWidget {
|
|
|
|
static const routeName = "/dynamic-album-browser";
|
2021-06-26 16:28:21 +02:00
|
|
|
|
2021-07-31 19:16:14 +02:00
|
|
|
static Route buildRoute(DynamicAlbumBrowserArguments args) =>
|
2021-07-23 22:05:57 +02:00
|
|
|
MaterialPageRoute(
|
2021-07-31 19:16:14 +02:00
|
|
|
builder: (context) => DynamicAlbumBrowser.fromArgs(args),
|
2021-07-23 22:05:57 +02:00
|
|
|
);
|
|
|
|
|
2021-09-15 08:58:06 +02:00
|
|
|
const DynamicAlbumBrowser({
|
2021-07-23 22:05:57 +02:00
|
|
|
Key? key,
|
|
|
|
required this.account,
|
|
|
|
required this.album,
|
2021-06-26 16:28:21 +02:00
|
|
|
}) : super(key: key);
|
|
|
|
|
2021-07-31 19:16:14 +02:00
|
|
|
DynamicAlbumBrowser.fromArgs(DynamicAlbumBrowserArguments args, {Key? key})
|
2021-06-26 16:28:21 +02:00
|
|
|
: this(
|
|
|
|
key: key,
|
|
|
|
account: args.account,
|
|
|
|
album: args.album,
|
|
|
|
);
|
|
|
|
|
|
|
|
@override
|
2021-07-31 19:16:14 +02:00
|
|
|
createState() => _DynamicAlbumBrowserState();
|
2021-06-26 16:28:21 +02:00
|
|
|
|
|
|
|
final Account account;
|
|
|
|
final Album album;
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:16:14 +02:00
|
|
|
class _DynamicAlbumBrowserState extends State<DynamicAlbumBrowser>
|
2021-06-26 16:28:21 +02:00
|
|
|
with
|
2021-07-31 19:16:14 +02:00
|
|
|
SelectableItemStreamListMixin<DynamicAlbumBrowser>,
|
|
|
|
AlbumBrowserMixin<DynamicAlbumBrowser> {
|
2021-06-26 16:28:21 +02:00
|
|
|
@override
|
|
|
|
initState() {
|
|
|
|
super.initState();
|
|
|
|
_initAlbum();
|
2021-08-13 22:18:35 +02:00
|
|
|
|
|
|
|
_albumUpdatedListener =
|
|
|
|
AppEventListener<AlbumUpdatedEvent>(_onAlbumUpdatedEvent);
|
|
|
|
_albumUpdatedListener.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
dispose() {
|
|
|
|
super.dispose();
|
|
|
|
_albumUpdatedListener.end();
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
build(BuildContext context) {
|
|
|
|
return AppTheme(
|
|
|
|
child: Scaffold(
|
2021-07-02 13:55:52 +02:00
|
|
|
body: Builder(
|
|
|
|
builder: (context) {
|
|
|
|
if (isEditMode) {
|
|
|
|
return Form(
|
|
|
|
key: _editFormKey,
|
|
|
|
child: _buildContent(context),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return _buildContent(context);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2021-06-26 16:28:21 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-15 08:58:06 +02:00
|
|
|
@override
|
2021-07-25 22:25:16 +02:00
|
|
|
@protected
|
2021-08-20 19:02:13 +02:00
|
|
|
get canEdit => _album?.albumFile?.isOwned(widget.account.username) == true;
|
2021-07-25 22:25:16 +02:00
|
|
|
|
2021-07-11 06:21:35 +02:00
|
|
|
@override
|
|
|
|
enterEditMode() {
|
|
|
|
super.enterEditMode();
|
2021-07-23 22:05:57 +02:00
|
|
|
_editAlbum = _album!.copyWith();
|
2021-07-11 06:21:35 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 21:26:14 +02:00
|
|
|
@override
|
2021-07-23 22:05:57 +02:00
|
|
|
validateEditMode() => _editFormKey.currentState?.validate() == true;
|
2021-07-08 21:26:14 +02:00
|
|
|
|
2021-07-02 13:55:52 +02:00
|
|
|
@override
|
|
|
|
doneEditMode() {
|
2021-07-11 06:21:35 +02:00
|
|
|
try {
|
|
|
|
// persist the changes
|
2021-07-23 22:05:57 +02:00
|
|
|
_editFormKey.currentState!.save();
|
|
|
|
final newAlbum = makeEdited(_editAlbum!);
|
|
|
|
if (newAlbum.copyWith(lastUpdated: OrNull(_album!.lastUpdated)) !=
|
2021-07-22 08:15:41 +02:00
|
|
|
_album) {
|
2021-07-11 06:21:35 +02:00
|
|
|
_log.info("[doneEditMode] Album modified: $newAlbum");
|
|
|
|
final albumRepo = AlbumRepo(AlbumCachedDataSource());
|
|
|
|
setState(() {
|
|
|
|
_album = newAlbum;
|
|
|
|
});
|
|
|
|
UpdateAlbum(albumRepo)(widget.account, newAlbum)
|
|
|
|
.catchError((e, stacktrace) {
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
2021-08-29 13:51:43 +02:00
|
|
|
content: Text(exception_util.toUserString(e)),
|
2021-07-11 06:21:35 +02:00
|
|
|
duration: k.snackBarDurationNormal,
|
|
|
|
));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_log.fine("[doneEditMode] Album not modified");
|
|
|
|
}
|
|
|
|
} finally {
|
2021-07-08 21:26:14 +02:00
|
|
|
setState(() {
|
2021-07-11 06:21:35 +02:00
|
|
|
// reset edits
|
|
|
|
_editAlbum = null;
|
|
|
|
// update the list to show the real album
|
|
|
|
_transformItems(_sortedItems);
|
2021-07-08 21:26:14 +02:00
|
|
|
});
|
2021-07-02 13:55:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 16:28:21 +02:00
|
|
|
void _initAlbum() {
|
2021-06-27 17:35:40 +02:00
|
|
|
assert(widget.album.provider is AlbumDynamicProvider);
|
2021-06-26 16:28:21 +02:00
|
|
|
PopulateAlbum()(widget.account, widget.album).then((items) {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
_album = widget.album;
|
|
|
|
_transformItems(items);
|
2021-08-13 12:41:02 +02:00
|
|
|
initCover(widget.account, widget.album);
|
2021-07-23 22:05:57 +02:00
|
|
|
_updateAlbumPostPopulate(items);
|
2021-06-26 16:28:21 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-31 14:05:10 +02:00
|
|
|
Future<void> _updateAlbumPostPopulate(List<AlbumItem> items) async {
|
|
|
|
final List<File> timeDescSortedFiles;
|
2021-07-11 06:21:35 +02:00
|
|
|
if (widget.album.sortProvider is AlbumTimeSortProvider) {
|
|
|
|
if ((widget.album.sortProvider as AlbumTimeSortProvider).isAscending) {
|
|
|
|
timeDescSortedFiles = _backingFiles.reversed.toList();
|
|
|
|
} else {
|
|
|
|
timeDescSortedFiles = _backingFiles;
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-15 08:58:06 +02:00
|
|
|
timeDescSortedFiles = const AlbumTimeSortProvider(isAscending: false)
|
2021-07-11 06:21:35 +02:00
|
|
|
.sort(items)
|
|
|
|
.whereType<AlbumFileItem>()
|
|
|
|
.map((e) => e.file)
|
|
|
|
.where((element) => file_util.isSupportedFormat(element))
|
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
|
2021-06-27 17:35:40 +02:00
|
|
|
bool shouldUpdate = false;
|
2021-07-11 06:21:35 +02:00
|
|
|
final albumUpdatedCover = UpdateDynamicAlbumCover()
|
2021-07-23 22:05:57 +02:00
|
|
|
.updateWithSortedFiles(_album!, timeDescSortedFiles);
|
2021-07-01 12:27:37 +02:00
|
|
|
if (!identical(albumUpdatedCover, _album)) {
|
|
|
|
_log.info("[_updateAlbumPostPopulate] Update album cover");
|
|
|
|
shouldUpdate = true;
|
2021-06-27 17:35:40 +02:00
|
|
|
}
|
2021-07-01 12:27:37 +02:00
|
|
|
_album = albumUpdatedCover;
|
|
|
|
|
2021-07-11 06:21:35 +02:00
|
|
|
final albumUpdatedTime = UpdateDynamicAlbumTime()
|
2021-07-23 22:05:57 +02:00
|
|
|
.updateWithSortedFiles(_album!, timeDescSortedFiles);
|
2021-07-01 12:27:37 +02:00
|
|
|
if (!identical(albumUpdatedTime, _album)) {
|
2021-06-27 17:35:40 +02:00
|
|
|
_log.info(
|
2021-07-01 12:27:37 +02:00
|
|
|
"[_updateAlbumPostPopulate] Update album time: ${albumUpdatedTime.provider.latestItemTime}");
|
2021-06-27 17:35:40 +02:00
|
|
|
shouldUpdate = true;
|
|
|
|
}
|
2021-07-01 12:27:37 +02:00
|
|
|
_album = albumUpdatedTime;
|
|
|
|
|
2021-06-27 17:35:40 +02:00
|
|
|
if (shouldUpdate) {
|
2021-08-31 14:05:10 +02:00
|
|
|
await UpdateAlbum(AlbumRepo(AlbumCachedDataSource()))(
|
|
|
|
widget.account, _album!);
|
2021-06-27 17:35:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 16:28:21 +02:00
|
|
|
Widget _buildContent(BuildContext context) {
|
|
|
|
if (_album == null) {
|
|
|
|
return CustomScrollView(
|
|
|
|
slivers: [
|
|
|
|
buildNormalAppBar(context, widget.account, widget.album),
|
|
|
|
const SliverToBoxAdapter(
|
2021-09-15 08:58:06 +02:00
|
|
|
child: LinearProgressIndicator(),
|
2021-06-26 16:28:21 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return buildItemStreamListOuter(
|
|
|
|
context,
|
|
|
|
child: Theme(
|
|
|
|
data: Theme.of(context).copyWith(
|
2021-09-15 18:29:37 +02:00
|
|
|
colorScheme: Theme.of(context).colorScheme.copyWith(
|
|
|
|
secondary: AppTheme.getOverscrollIndicatorColor(context),
|
|
|
|
),
|
2021-06-26 16:28:21 +02:00
|
|
|
),
|
|
|
|
child: CustomScrollView(
|
|
|
|
slivers: [
|
|
|
|
_buildAppBar(context),
|
|
|
|
SliverPadding(
|
2021-07-12 20:24:54 +02:00
|
|
|
padding: const EdgeInsets.only(top: 8),
|
2021-07-02 13:55:52 +02:00
|
|
|
sliver: SliverIgnorePointer(
|
|
|
|
ignoring: isEditMode,
|
|
|
|
sliver: SliverOpacity(
|
|
|
|
opacity: isEditMode ? .25 : 1,
|
2021-07-06 07:10:36 +02:00
|
|
|
sliver: buildItemStreamList(
|
|
|
|
maxCrossAxisExtent: thumbSize.toDouble(),
|
|
|
|
),
|
2021-07-02 13:55:52 +02:00
|
|
|
),
|
|
|
|
),
|
2021-06-26 16:28:21 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-02 13:55:52 +02:00
|
|
|
Widget _buildAppBar(BuildContext context) {
|
|
|
|
if (isEditMode) {
|
|
|
|
return _buildEditAppBar(context);
|
|
|
|
} else if (isSelectionMode) {
|
|
|
|
return _buildSelectionAppBar(context);
|
|
|
|
} else {
|
|
|
|
return _buildNormalAppBar(context);
|
|
|
|
}
|
|
|
|
}
|
2021-06-30 07:11:02 +02:00
|
|
|
|
|
|
|
Widget _buildNormalAppBar(BuildContext context) {
|
|
|
|
return buildNormalAppBar(
|
|
|
|
context,
|
|
|
|
widget.account,
|
2021-07-23 22:05:57 +02:00
|
|
|
_album!,
|
2021-08-20 19:02:13 +02:00
|
|
|
menuItemBuilder: canEdit
|
|
|
|
? (context) => [
|
|
|
|
PopupMenuItem(
|
|
|
|
value: _menuValueConvertBasic,
|
2021-08-29 13:51:43 +02:00
|
|
|
child: Text(L10n.global().convertBasicAlbumMenuLabel),
|
2021-08-20 19:02:13 +02:00
|
|
|
),
|
|
|
|
]
|
|
|
|
: null,
|
2021-07-02 13:55:52 +02:00
|
|
|
onSelectedMenuItem: (option) {
|
|
|
|
switch (option) {
|
|
|
|
case _menuValueConvertBasic:
|
|
|
|
_onAppBarConvertBasicPressed(context);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
_log.shout("[_buildNormalAppBar] Unknown value: $option");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
2021-06-30 07:11:02 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildSelectionAppBar(BuildContext context) {
|
|
|
|
return buildSelectionAppBar(context, [
|
2021-07-10 17:06:04 +02:00
|
|
|
if (platform_k.isAndroid)
|
|
|
|
IconButton(
|
|
|
|
icon: const Icon(Icons.share),
|
2021-08-29 13:51:43 +02:00
|
|
|
tooltip: L10n.global().shareTooltip,
|
2021-07-10 17:06:04 +02:00
|
|
|
onPressed: () {
|
|
|
|
_onSelectionAppBarSharePressed(context);
|
|
|
|
},
|
|
|
|
),
|
2021-06-30 07:11:02 +02:00
|
|
|
PopupMenuButton(
|
|
|
|
tooltip: MaterialLocalizations.of(context).moreButtonTooltip,
|
|
|
|
itemBuilder: (context) => [
|
|
|
|
PopupMenuItem(
|
|
|
|
value: _SelectionAppBarOption.delete,
|
2021-08-29 13:51:43 +02:00
|
|
|
child: Text(L10n.global().deleteTooltip),
|
2021-06-30 07:11:02 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
onSelected: (option) {
|
|
|
|
if (option == _SelectionAppBarOption.delete) {
|
|
|
|
_onSelectionAppBarDeletePressed();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]);
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 13:55:52 +02:00
|
|
|
Widget _buildEditAppBar(BuildContext context) {
|
2021-07-11 06:21:35 +02:00
|
|
|
return buildEditAppBar(context, widget.account, widget.album, actions: [
|
|
|
|
IconButton(
|
2021-09-15 08:58:06 +02:00
|
|
|
icon: const Icon(Icons.sort_by_alpha),
|
2021-08-29 13:51:43 +02:00
|
|
|
tooltip: L10n.global().sortTooltip,
|
2021-07-11 06:21:35 +02:00
|
|
|
onPressed: _onEditAppBarSortPressed,
|
|
|
|
),
|
|
|
|
]);
|
2021-07-02 13:55:52 +02:00
|
|
|
}
|
|
|
|
|
2021-06-26 16:28:21 +02:00
|
|
|
void _onItemTap(int index) {
|
2021-07-11 06:21:35 +02:00
|
|
|
// convert item index to file index
|
|
|
|
var fileIndex = index;
|
|
|
|
for (int i = 0; i < index; ++i) {
|
|
|
|
if (_sortedItems[i] is! AlbumFileItem ||
|
|
|
|
!file_util
|
|
|
|
.isSupportedFormat((_sortedItems[i] as AlbumFileItem).file)) {
|
|
|
|
--fileIndex;
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 16:28:21 +02:00
|
|
|
Navigator.pushNamed(context, Viewer.routeName,
|
2021-08-13 22:18:35 +02:00
|
|
|
arguments: ViewerArguments(widget.account, _backingFiles, fileIndex,
|
|
|
|
album: widget.album));
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-25 08:02:52 +02:00
|
|
|
Future<void> _onAppBarConvertBasicPressed(BuildContext context) async {
|
|
|
|
final result = await showDialog<bool>(
|
2021-06-30 07:11:02 +02:00
|
|
|
context: context,
|
|
|
|
builder: (context) => AlertDialog(
|
2021-08-29 13:51:43 +02:00
|
|
|
title: Text(L10n.global().convertBasicAlbumConfirmationDialogTitle),
|
|
|
|
content: Text(L10n.global().convertBasicAlbumConfirmationDialogContent),
|
2021-06-30 07:11:02 +02:00
|
|
|
actions: <Widget>[
|
|
|
|
TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
child: Text(MaterialLocalizations.of(context).cancelButtonLabel),
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop(true);
|
|
|
|
},
|
|
|
|
child: Text(MaterialLocalizations.of(context).okButtonLabel),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2021-09-25 08:02:52 +02:00
|
|
|
);
|
|
|
|
if (result != true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_log.info(
|
|
|
|
"[_onAppBarConvertBasicPressed] Converting album '${_album!.name}' to static");
|
|
|
|
final albumRepo = AlbumRepo(AlbumCachedDataSource());
|
|
|
|
try {
|
|
|
|
await UpdateAlbum(albumRepo)(
|
|
|
|
widget.account,
|
|
|
|
_album!.copyWith(
|
|
|
|
provider: AlbumStaticProvider(
|
|
|
|
items: _sortedItems,
|
|
|
|
),
|
|
|
|
coverProvider: AlbumAutoCoverProvider(),
|
|
|
|
));
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
|
|
|
content: Text(L10n.global().convertBasicAlbumSuccessNotification),
|
|
|
|
duration: k.snackBarDurationNormal,
|
|
|
|
));
|
|
|
|
if (mounted) {
|
|
|
|
Navigator.of(context).pop();
|
2021-06-30 07:11:02 +02:00
|
|
|
}
|
2021-09-25 08:02:52 +02:00
|
|
|
} catch (e, stackTrace) {
|
|
|
|
_log.shout(
|
|
|
|
"[_onAppBarConvertBasicPressed] Failed while converting to basic album",
|
|
|
|
e,
|
|
|
|
stackTrace);
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
|
|
|
content: Text(exception_util.toUserString(e)),
|
|
|
|
duration: k.snackBarDurationNormal,
|
|
|
|
));
|
|
|
|
}
|
2021-06-30 07:11:02 +02:00
|
|
|
}
|
|
|
|
|
2021-07-10 17:06:04 +02:00
|
|
|
void _onSelectionAppBarSharePressed(BuildContext context) {
|
|
|
|
assert(platform_k.isAndroid);
|
|
|
|
final selected = selectedListItems
|
|
|
|
.whereType<_FileListItem>()
|
|
|
|
.map((e) => e.file)
|
|
|
|
.toList();
|
|
|
|
ShareHandler().shareFiles(context, widget.account, selected).then((_) {
|
|
|
|
setState(() {
|
|
|
|
clearSelectedItems();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-26 16:28:21 +02:00
|
|
|
void _onSelectionAppBarDeletePressed() async {
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
2021-08-29 13:51:43 +02:00
|
|
|
content: Text(L10n.global()
|
2021-06-26 16:28:21 +02:00
|
|
|
.deleteSelectedProcessingNotification(selectedListItems.length)),
|
|
|
|
duration: k.snackBarDurationShort,
|
|
|
|
));
|
|
|
|
|
2021-07-11 06:21:35 +02:00
|
|
|
final selected = selectedListItems.whereType<_FileListItem>().toList();
|
2021-06-26 16:28:21 +02:00
|
|
|
setState(() {
|
|
|
|
clearSelectedItems();
|
|
|
|
});
|
|
|
|
|
|
|
|
final fileRepo = FileRepo(FileCachedDataSource());
|
|
|
|
final albumRepo = AlbumRepo(AlbumCachedDataSource());
|
2021-07-11 06:21:35 +02:00
|
|
|
final successes = <_FileListItem>[];
|
|
|
|
final failures = <_FileListItem>[];
|
|
|
|
for (final item in selected) {
|
2021-06-26 16:28:21 +02:00
|
|
|
try {
|
2021-07-11 06:21:35 +02:00
|
|
|
await Remove(fileRepo, albumRepo).call(widget.account, item.file);
|
|
|
|
successes.add(item);
|
2021-06-26 16:28:21 +02:00
|
|
|
} catch (e, stacktrace) {
|
|
|
|
_log.shout(
|
|
|
|
"[_onSelectionAppBarDeletePressed] Failed while removing file" +
|
2021-09-04 14:35:04 +02:00
|
|
|
(shouldLogFileName ? ": ${item.file.path}" : ""),
|
2021-06-26 16:28:21 +02:00
|
|
|
e,
|
|
|
|
stacktrace);
|
2021-07-11 06:21:35 +02:00
|
|
|
failures.add(item);
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failures.isEmpty) {
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
2021-08-29 13:51:43 +02:00
|
|
|
content: Text(L10n.global().deleteSelectedSuccessNotification),
|
2021-06-26 16:28:21 +02:00
|
|
|
duration: k.snackBarDurationNormal,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
2021-08-29 13:51:43 +02:00
|
|
|
content: Text(
|
|
|
|
L10n.global().deleteSelectedFailureNotification(failures.length)),
|
2021-06-26 16:28:21 +02:00
|
|
|
duration: k.snackBarDurationNormal,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if (successes.isNotEmpty) {
|
2021-07-11 06:21:35 +02:00
|
|
|
final indexes = successes.map((e) => e.index).sorted();
|
2021-06-26 16:28:21 +02:00
|
|
|
setState(() {
|
2021-07-11 06:21:35 +02:00
|
|
|
for (final i in indexes.reversed) {
|
|
|
|
_sortedItems.removeAt(i);
|
|
|
|
}
|
|
|
|
_onSortedItemsUpdated();
|
2021-06-26 16:28:21 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 06:21:35 +02:00
|
|
|
void _onEditAppBarSortPressed() {
|
2021-07-23 22:05:57 +02:00
|
|
|
final sortProvider = _editAlbum!.sortProvider;
|
2021-07-11 06:21:35 +02:00
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => FancyOptionPicker(
|
2021-08-29 13:51:43 +02:00
|
|
|
title: L10n.global().sortOptionDialogTitle,
|
2021-07-11 06:21:35 +02:00
|
|
|
items: [
|
|
|
|
FancyOptionPickerItem(
|
2021-09-08 17:39:16 +02:00
|
|
|
label: L10n.global().sortOptionTimeDescendingLabel,
|
2021-07-11 06:21:35 +02:00
|
|
|
isSelected: sortProvider is AlbumTimeSortProvider &&
|
2021-09-08 17:39:16 +02:00
|
|
|
!sortProvider.isAscending,
|
2021-07-11 06:21:35 +02:00
|
|
|
onSelect: () {
|
2021-09-08 17:39:16 +02:00
|
|
|
_onSortNewestPressed();
|
2021-07-11 06:21:35 +02:00
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
FancyOptionPickerItem(
|
2021-09-08 17:39:16 +02:00
|
|
|
label: L10n.global().sortOptionTimeAscendingLabel,
|
2021-07-11 06:21:35 +02:00
|
|
|
isSelected: sortProvider is AlbumTimeSortProvider &&
|
2021-09-08 17:39:16 +02:00
|
|
|
sortProvider.isAscending,
|
2021-07-11 06:21:35 +02:00
|
|
|
onSelect: () {
|
2021-09-08 17:39:16 +02:00
|
|
|
_onSortOldestPressed();
|
2021-07-11 06:21:35 +02:00
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSortOldestPressed() {
|
2021-07-23 22:05:57 +02:00
|
|
|
_editAlbum = _editAlbum!.copyWith(
|
2021-09-15 08:58:06 +02:00
|
|
|
sortProvider: const AlbumTimeSortProvider(isAscending: true),
|
2021-07-11 06:21:35 +02:00
|
|
|
);
|
|
|
|
setState(() {
|
|
|
|
_transformItems(_sortedItems);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSortNewestPressed() {
|
2021-07-23 22:05:57 +02:00
|
|
|
_editAlbum = _editAlbum!.copyWith(
|
2021-09-15 08:58:06 +02:00
|
|
|
sortProvider: const AlbumTimeSortProvider(isAscending: false),
|
2021-07-11 06:21:35 +02:00
|
|
|
);
|
|
|
|
setState(() {
|
|
|
|
_transformItems(_sortedItems);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-13 22:18:35 +02:00
|
|
|
void _onAlbumUpdatedEvent(AlbumUpdatedEvent ev) {
|
|
|
|
if (ev.album.albumFile!.path == _album?.albumFile?.path) {
|
|
|
|
setState(() {
|
|
|
|
_album = ev.album;
|
|
|
|
initCover(widget.account, ev.album);
|
2021-08-31 14:05:10 +02:00
|
|
|
_updateAlbumPostPopulate(_sortedItems);
|
2021-08-13 22:18:35 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 16:28:21 +02:00
|
|
|
void _transformItems(List<AlbumItem> items) {
|
2021-07-11 06:21:35 +02:00
|
|
|
if (_editAlbum != null) {
|
|
|
|
// edit mode
|
2021-07-23 22:05:57 +02:00
|
|
|
_sortedItems = _editAlbum!.sortProvider.sort(items);
|
2021-07-11 06:21:35 +02:00
|
|
|
} else {
|
2021-07-23 22:05:57 +02:00
|
|
|
_sortedItems = _album!.sortProvider.sort(items);
|
2021-07-11 06:21:35 +02:00
|
|
|
}
|
|
|
|
_onSortedItemsUpdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSortedItemsUpdated() {
|
|
|
|
_backingFiles = _sortedItems
|
2021-06-26 16:28:21 +02:00
|
|
|
.whereType<AlbumFileItem>()
|
|
|
|
.map((e) => e.file)
|
|
|
|
.where((element) => file_util.isSupportedFormat(element))
|
2021-07-11 06:21:35 +02:00
|
|
|
.toList();
|
2021-06-26 16:28:21 +02:00
|
|
|
itemStreamListItems = () sync* {
|
2021-07-11 06:21:35 +02:00
|
|
|
for (int i = 0; i < _sortedItems.length; ++i) {
|
|
|
|
final item = _sortedItems[i];
|
|
|
|
if (item is AlbumFileItem) {
|
|
|
|
final previewUrl = api_util.getFilePreviewUrl(
|
|
|
|
widget.account,
|
|
|
|
item.file,
|
2021-09-16 12:25:08 +02:00
|
|
|
width: k.photoThumbSize,
|
|
|
|
height: k.photoThumbSize,
|
2021-06-26 16:28:21 +02:00
|
|
|
);
|
2021-07-11 06:21:35 +02:00
|
|
|
if (file_util.isSupportedImageFormat(item.file)) {
|
|
|
|
yield _ImageListItem(
|
|
|
|
index: i,
|
|
|
|
file: item.file,
|
|
|
|
account: widget.account,
|
|
|
|
previewUrl: previewUrl,
|
|
|
|
onTap: () => _onItemTap(i),
|
|
|
|
);
|
|
|
|
} else if (file_util.isSupportedVideoFormat(item.file)) {
|
|
|
|
yield _VideoListItem(
|
|
|
|
index: i,
|
|
|
|
file: item.file,
|
|
|
|
account: widget.account,
|
|
|
|
previewUrl: previewUrl,
|
|
|
|
onTap: () => _onItemTap(i),
|
|
|
|
);
|
|
|
|
}
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-07 20:51:34 +02:00
|
|
|
}()
|
|
|
|
.toList();
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Album? _album;
|
2021-07-11 06:21:35 +02:00
|
|
|
var _sortedItems = <AlbumItem>[];
|
2021-06-26 16:28:21 +02:00
|
|
|
var _backingFiles = <File>[];
|
|
|
|
|
2021-07-02 13:55:52 +02:00
|
|
|
final _editFormKey = GlobalKey<FormState>();
|
2021-07-23 22:05:57 +02:00
|
|
|
Album? _editAlbum;
|
2021-07-02 13:55:52 +02:00
|
|
|
|
2021-08-13 22:18:35 +02:00
|
|
|
late AppEventListener<AlbumUpdatedEvent> _albumUpdatedListener;
|
|
|
|
|
2021-06-26 16:28:21 +02:00
|
|
|
static final _log =
|
2021-07-31 19:16:14 +02:00
|
|
|
Logger("widget.dynamic_album_browser._DynamicAlbumBrowserState");
|
2021-07-02 13:55:52 +02:00
|
|
|
static const _menuValueConvertBasic = 0;
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 15:03:29 +02:00
|
|
|
abstract class _ListItem implements SelectableItem {
|
|
|
|
_ListItem({
|
2021-07-23 22:05:57 +02:00
|
|
|
required this.index,
|
|
|
|
VoidCallback? onTap,
|
2021-07-06 15:03:29 +02:00
|
|
|
}) : _onTap = onTap;
|
|
|
|
|
|
|
|
@override
|
|
|
|
get onTap => _onTap;
|
|
|
|
|
|
|
|
@override
|
|
|
|
get isSelectable => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
get staggeredTile => const StaggeredTile.count(1, 1);
|
|
|
|
|
2021-07-11 06:21:35 +02:00
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
|
|
|
"index: $index, "
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
final int index;
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
final VoidCallback? _onTap;
|
2021-07-06 15:03:29 +02:00
|
|
|
}
|
|
|
|
|
2021-07-10 17:06:04 +02:00
|
|
|
abstract class _FileListItem extends _ListItem {
|
|
|
|
_FileListItem({
|
2021-07-23 22:05:57 +02:00
|
|
|
required int index,
|
|
|
|
required this.file,
|
|
|
|
VoidCallback? onTap,
|
2021-07-10 17:06:04 +02:00
|
|
|
}) : super(
|
2021-07-11 06:21:35 +02:00
|
|
|
index: index,
|
2021-07-10 17:06:04 +02:00
|
|
|
onTap: onTap,
|
|
|
|
);
|
|
|
|
|
|
|
|
final File file;
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ImageListItem extends _FileListItem {
|
|
|
|
_ImageListItem({
|
2021-07-23 22:05:57 +02:00
|
|
|
required int index,
|
|
|
|
required File file,
|
|
|
|
required this.account,
|
|
|
|
required this.previewUrl,
|
|
|
|
VoidCallback? onTap,
|
2021-07-10 17:06:04 +02:00
|
|
|
}) : super(
|
2021-07-11 06:21:35 +02:00
|
|
|
index: index,
|
2021-07-10 17:06:04 +02:00
|
|
|
file: file,
|
|
|
|
onTap: onTap,
|
|
|
|
);
|
2021-06-26 16:28:21 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
buildWidget(BuildContext context) {
|
|
|
|
return PhotoListImage(
|
|
|
|
account: account,
|
|
|
|
previewUrl: previewUrl,
|
|
|
|
isGif: file.contentType == "image/gif",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
final String previewUrl;
|
|
|
|
}
|
|
|
|
|
2021-07-10 17:06:04 +02:00
|
|
|
class _VideoListItem extends _FileListItem {
|
2021-06-26 16:28:21 +02:00
|
|
|
_VideoListItem({
|
2021-07-23 22:05:57 +02:00
|
|
|
required int index,
|
|
|
|
required File file,
|
|
|
|
required this.account,
|
|
|
|
required this.previewUrl,
|
|
|
|
VoidCallback? onTap,
|
2021-07-10 17:06:04 +02:00
|
|
|
}) : super(
|
2021-07-11 06:21:35 +02:00
|
|
|
index: index,
|
2021-07-10 17:06:04 +02:00
|
|
|
file: file,
|
|
|
|
onTap: onTap,
|
|
|
|
);
|
2021-06-26 16:28:21 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
buildWidget(BuildContext context) {
|
|
|
|
return PhotoListVideo(
|
|
|
|
account: account,
|
|
|
|
previewUrl: previewUrl,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
final String previewUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum _SelectionAppBarOption {
|
|
|
|
delete,
|
|
|
|
}
|