diff --git a/lib/services/collections_service.dart b/lib/services/collections_service.dart index 19044098a..6309fe50d 100644 --- a/lib/services/collections_service.dart +++ b/lib/services/collections_service.dart @@ -214,11 +214,6 @@ class CollectionsService { 0; } - Future> _getLatestCollectionFiles() { - _cachedLatestFiles ??= _filesDB.getLatestCollectionFiles(); - return _cachedLatestFiles!; - } - Future> getCollectionIDToNewestFileTime() { _collectionIDToNewestFileTime ??= _filesDB.getCollectionIDToMaxCreationTime(); diff --git a/lib/ui/tabs/user_collections_tab.dart b/lib/ui/tabs/user_collections_tab.dart index 6474063f7..04a2abd1c 100644 --- a/lib/ui/tabs/user_collections_tab.dart +++ b/lib/ui/tabs/user_collections_tab.dart @@ -13,7 +13,6 @@ import 'package:photos/extensions/list.dart'; import "package:photos/generated/l10n.dart"; import 'package:photos/models/collection.dart'; import 'package:photos/services/collections_service.dart'; -import "package:photos/services/remote_sync_service.dart"; import "package:photos/ui/collections/button/archived_button.dart"; import "package:photos/ui/collections/button/hidden_button.dart"; import "package:photos/ui/collections/button/trash_button.dart"; @@ -136,8 +135,6 @@ class _UserCollectionsTabState extends State Widget _getCollectionsGalleryWidget( List? collections, ) { - final bool showDeleteAlbumsButton = - RemoteSyncService.instance.isFirstRemoteSyncDone(); final TextStyle trashAndHiddenTextStyle = Theme.of(context).textTheme.titleMedium!.copyWith( color: Theme.of(context) @@ -163,12 +160,7 @@ class _UserCollectionsTabState extends State _sortMenu(), ], ), - showDeleteAlbumsButton - ? const Padding( - padding: EdgeInsets.only(top: 2, left: 8.5, right: 48), - child: DeleteEmptyAlbums(), - ) - : const SizedBox.shrink(), + DeleteEmptyAlbums(collections ?? []), Configuration.instance.hasConfiguredAccount() ? RemoteCollectionsGridViewWidget(collections) : const EmptyState(), diff --git a/lib/ui/viewer/actions/delete_empty_albums.dart b/lib/ui/viewer/actions/delete_empty_albums.dart index ab90e8c34..5264ceb21 100644 --- a/lib/ui/viewer/actions/delete_empty_albums.dart +++ b/lib/ui/viewer/actions/delete_empty_albums.dart @@ -6,12 +6,14 @@ import "package:photos/generated/l10n.dart"; import 'package:photos/models/collection.dart'; import 'package:photos/models/file.dart'; import 'package:photos/services/collections_service.dart'; +import "package:photos/services/remote_sync_service.dart"; import 'package:photos/ui/components/action_sheet_widget.dart'; import 'package:photos/ui/components/buttons/button_widget.dart'; import 'package:photos/ui/components/models/button_type.dart'; class DeleteEmptyAlbums extends StatefulWidget { - const DeleteEmptyAlbums({Key? key}) : super(key: key); + final List collections; + const DeleteEmptyAlbums(this.collections, {Key? key}) : super(key: key); @override State createState() => _DeleteEmptyAlbumsState(); @@ -27,10 +29,39 @@ class _DeleteEmptyAlbumsState extends State { super.dispose(); } + Future _showDeleteButton() async { + if (!RemoteSyncService.instance.isFirstRemoteSyncDone()) { + return Future.value(false); + } + final Map collectionIDToLatestTimeCount = + await CollectionsService.instance.getCollectionIDToNewestFileTime(); + final emptyAlbumCount = widget.collections + .where((collection) { + final latestTimeCount = collectionIDToLatestTimeCount[collection.id]; + return latestTimeCount == null; + }) + .toList() + .length; + debugPrint("Empty albums count $emptyAlbumCount"); + return emptyAlbumCount > 2; + } + @override Widget build(BuildContext context) { + return FutureBuilder( + future: _showDeleteButton(), + builder: (context, snapshot) { + if (snapshot.hasData && snapshot.data!) { + return _buildDeleteButton(); + } + return const SizedBox.shrink(); + }, + ); + } + + Widget _buildDeleteButton() { return Padding( - padding: const EdgeInsets.fromLTRB(0, 2, 8, 12), + padding: const EdgeInsets.fromLTRB(8.5, 4, 8, 12), child: Align( alignment: Alignment.centerLeft, child: ButtonWidget(