Refactor DeleteEmptyButtonWidget

This commit is contained in:
Neeraj Gupta 2023-06-22 14:19:42 +05:30
parent d16e903a45
commit ed046c0301
3 changed files with 34 additions and 16 deletions

View file

@ -214,11 +214,6 @@ class CollectionsService {
0;
}
Future<List<File>> _getLatestCollectionFiles() {
_cachedLatestFiles ??= _filesDB.getLatestCollectionFiles();
return _cachedLatestFiles!;
}
Future<Map<int, int>> getCollectionIDToNewestFileTime() {
_collectionIDToNewestFileTime ??=
_filesDB.getCollectionIDToMaxCreationTime();

View file

@ -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<UserCollectionsTab>
Widget _getCollectionsGalleryWidget(
List<Collection>? 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<UserCollectionsTab>
_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(),

View file

@ -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<Collection> collections;
const DeleteEmptyAlbums(this.collections, {Key? key}) : super(key: key);
@override
State<DeleteEmptyAlbums> createState() => _DeleteEmptyAlbumsState();
@ -27,10 +29,39 @@ class _DeleteEmptyAlbumsState extends State<DeleteEmptyAlbums> {
super.dispose();
}
Future<bool> _showDeleteButton() async {
if (!RemoteSyncService.instance.isFirstRemoteSyncDone()) {
return Future.value(false);
}
final Map<int, int> 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<bool>(
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(