2020-10-24 23:10:37 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-09-16 04:45:32 +00:00
|
|
|
import 'package:collection/collection.dart';
|
2020-10-24 23:10:37 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2020-10-28 15:25:32 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:photos/core/configuration.dart';
|
2022-10-19 11:28:03 +00:00
|
|
|
import 'package:photos/core/constants.dart';
|
2020-10-24 23:10:37 +00:00
|
|
|
import 'package:photos/core/event_bus.dart';
|
2020-10-28 12:03:28 +00:00
|
|
|
import 'package:photos/events/collection_updated_event.dart';
|
2020-10-24 23:10:37 +00:00
|
|
|
import 'package:photos/events/local_photos_updated_event.dart';
|
2021-03-17 21:07:17 +00:00
|
|
|
import 'package:photos/events/user_logged_out_event.dart';
|
2022-11-16 17:01:57 +00:00
|
|
|
import 'package:photos/extensions/list.dart';
|
2022-09-15 08:14:08 +00:00
|
|
|
import 'package:photos/models/collection.dart';
|
2020-10-29 00:30:02 +00:00
|
|
|
import 'package:photos/models/collection_items.dart';
|
2021-07-23 12:28:02 +00:00
|
|
|
import 'package:photos/services/collections_service.dart';
|
2022-10-27 07:23:45 +00:00
|
|
|
import 'package:photos/ui/collections/archived_collections_button_widget.dart';
|
2022-08-11 12:28:06 +00:00
|
|
|
import 'package:photos/ui/collections/device_folders_grid_view_widget.dart';
|
|
|
|
import 'package:photos/ui/collections/hidden_collections_button_widget.dart';
|
|
|
|
import 'package:photos/ui/collections/remote_collections_grid_view_widget.dart';
|
|
|
|
import 'package:photos/ui/collections/section_title.dart';
|
|
|
|
import 'package:photos/ui/collections/trash_button_widget.dart';
|
2022-07-01 14:18:05 +00:00
|
|
|
import 'package:photos/ui/common/loading_widget.dart';
|
2023-01-03 08:29:26 +00:00
|
|
|
import 'package:photos/ui/viewer/actions/delete_empty_albums.dart';
|
2022-09-12 12:16:34 +00:00
|
|
|
import 'package:photos/ui/viewer/gallery/empty_state.dart';
|
2021-09-08 20:06:37 +00:00
|
|
|
import 'package:photos/utils/local_settings.dart';
|
2021-09-17 04:44:10 +00:00
|
|
|
|
2020-10-24 23:10:37 +00:00
|
|
|
class CollectionsGalleryWidget extends StatefulWidget {
|
2022-12-30 12:10:17 +00:00
|
|
|
const CollectionsGalleryWidget({Key? key}) : super(key: key);
|
2020-10-24 23:10:37 +00:00
|
|
|
|
|
|
|
@override
|
2022-07-03 09:49:33 +00:00
|
|
|
State<CollectionsGalleryWidget> createState() =>
|
|
|
|
_CollectionsGalleryWidgetState();
|
2020-10-24 23:10:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 11:36:51 +00:00
|
|
|
class _CollectionsGalleryWidgetState extends State<CollectionsGalleryWidget>
|
|
|
|
with AutomaticKeepAliveClientMixin {
|
2022-11-11 10:44:12 +00:00
|
|
|
final _logger = Logger((_CollectionsGalleryWidgetState).toString());
|
2022-12-30 12:10:17 +00:00
|
|
|
late StreamSubscription<LocalPhotosUpdatedEvent> _localFilesSubscription;
|
2023-01-03 08:29:26 +00:00
|
|
|
late StreamSubscription<CollectionUpdatedEvent>
|
|
|
|
_collectionUpdatesSubscription;
|
2022-12-30 12:10:17 +00:00
|
|
|
late StreamSubscription<UserLoggedOutEvent> _loggedOutEvent;
|
|
|
|
AlbumSortKey? sortKey;
|
2022-05-05 06:46:31 +00:00
|
|
|
String _loadReason = "init";
|
2020-10-24 23:10:37 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2022-07-03 09:49:33 +00:00
|
|
|
_localFilesSubscription =
|
|
|
|
Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
|
2022-11-11 10:44:12 +00:00
|
|
|
_loadReason = event.reason;
|
2020-10-28 12:03:28 +00:00
|
|
|
setState(() {});
|
|
|
|
});
|
2022-07-03 09:49:33 +00:00
|
|
|
_collectionUpdatesSubscription =
|
|
|
|
Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
|
2022-11-11 10:44:12 +00:00
|
|
|
_loadReason = event.reason;
|
2020-10-24 23:10:37 +00:00
|
|
|
setState(() {});
|
|
|
|
});
|
2021-03-17 21:07:17 +00:00
|
|
|
_loggedOutEvent = Bus.instance.on<UserLoggedOutEvent>().listen((event) {
|
2022-11-11 10:44:12 +00:00
|
|
|
_loadReason = event.reason;
|
2021-03-17 21:07:17 +00:00
|
|
|
setState(() {});
|
|
|
|
});
|
2021-09-08 20:06:37 +00:00
|
|
|
sortKey = LocalSettings.instance.albumSortKey();
|
2020-10-24 23:10:37 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-11-16 08:00:31 +00:00
|
|
|
super.build(context);
|
2022-05-05 06:46:31 +00:00
|
|
|
_logger.info("Building, trigger: $_loadReason");
|
2022-09-12 12:11:42 +00:00
|
|
|
return FutureBuilder<List<CollectionWithThumbnail>>(
|
2020-10-24 23:10:37 +00:00
|
|
|
future: _getCollections(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
return _getCollectionsGalleryWidget(snapshot.data);
|
|
|
|
} else if (snapshot.hasError) {
|
|
|
|
return Text(snapshot.error.toString());
|
|
|
|
} else {
|
2022-07-03 06:04:42 +00:00
|
|
|
return const EnteLoadingWidget();
|
2020-10-24 23:10:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-12 12:11:42 +00:00
|
|
|
Future<List<CollectionWithThumbnail>> _getCollections() async {
|
2022-10-19 11:39:56 +00:00
|
|
|
final List<CollectionWithThumbnail> collectionsWithThumbnail =
|
|
|
|
await CollectionsService.instance.getCollectionsWithThumbnails();
|
2022-12-19 10:05:46 +00:00
|
|
|
final ListMatch<CollectionWithThumbnail> favMathResult =
|
|
|
|
collectionsWithThumbnail.splitMatch(
|
2022-11-16 17:01:57 +00:00
|
|
|
(element) => element.collection.type == CollectionType.favorites,
|
|
|
|
);
|
2022-12-19 10:05:46 +00:00
|
|
|
// Hide fav collection if it's empty and not shared
|
|
|
|
favMathResult.matched.removeWhere(
|
|
|
|
(element) =>
|
|
|
|
element.thumbnail == null &&
|
|
|
|
(element.collection.publicURLs?.isEmpty ?? false),
|
|
|
|
);
|
|
|
|
|
|
|
|
favMathResult.unmatched.sort(
|
2022-04-28 13:08:27 +00:00
|
|
|
(first, second) {
|
|
|
|
if (sortKey == AlbumSortKey.albumName) {
|
2022-09-16 04:45:32 +00:00
|
|
|
return compareAsciiLowerCaseNatural(
|
2022-12-30 12:10:17 +00:00
|
|
|
first.collection.name!,
|
|
|
|
second.collection.name!,
|
2022-09-16 04:45:32 +00:00
|
|
|
);
|
2022-04-28 13:08:27 +00:00
|
|
|
} else if (sortKey == AlbumSortKey.newestPhoto) {
|
2022-10-19 11:28:03 +00:00
|
|
|
return (second.thumbnail?.creationTime ?? -1 * intMaxValue)
|
|
|
|
.compareTo(first.thumbnail?.creationTime ?? -1 * intMaxValue);
|
2022-04-28 13:08:27 +00:00
|
|
|
} else {
|
2022-07-03 09:49:33 +00:00
|
|
|
return second.collection.updationTime
|
|
|
|
.compareTo(first.collection.updationTime);
|
2022-04-28 13:08:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2022-12-19 10:05:46 +00:00
|
|
|
// This is a way to identify collection which were automatically created
|
|
|
|
// during create link flow for selected files
|
|
|
|
final ListMatch<CollectionWithThumbnail> potentialSharedLinkCollection =
|
|
|
|
favMathResult.unmatched.splitMatch(
|
2022-12-23 02:29:27 +00:00
|
|
|
(e) => (e.collection.isSharedFilesCollection()),
|
2022-11-21 10:47:47 +00:00
|
|
|
);
|
2022-12-19 10:05:46 +00:00
|
|
|
|
2022-12-25 06:08:03 +00:00
|
|
|
return favMathResult.matched + potentialSharedLinkCollection.unmatched;
|
2020-10-24 23:10:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:11:42 +00:00
|
|
|
Widget _getCollectionsGalleryWidget(
|
2022-12-30 12:10:17 +00:00
|
|
|
List<CollectionWithThumbnail>? collections,
|
2022-09-12 12:11:42 +00:00
|
|
|
) {
|
2023-01-03 08:29:26 +00:00
|
|
|
final bool showDeleteAlbumsButton =
|
2023-01-03 08:36:45 +00:00
|
|
|
collections!.where((c) => c.thumbnail == null).length >= 3;
|
2022-07-03 09:49:33 +00:00
|
|
|
final TextStyle trashAndHiddenTextStyle = Theme.of(context)
|
|
|
|
.textTheme
|
2022-12-30 12:10:17 +00:00
|
|
|
.subtitle1!
|
2022-07-03 09:49:33 +00:00
|
|
|
.copyWith(
|
2022-12-30 12:10:17 +00:00
|
|
|
color: Theme.of(context).textTheme.subtitle1!.color!.withOpacity(0.5),
|
2022-06-11 08:23:52 +00:00
|
|
|
);
|
2022-08-11 12:28:06 +00:00
|
|
|
|
2020-12-12 01:11:12 +00:00
|
|
|
return SingleChildScrollView(
|
2021-02-08 09:26:49 +00:00
|
|
|
child: Container(
|
|
|
|
margin: const EdgeInsets.only(bottom: 50),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
2022-06-11 09:21:39 +00:00
|
|
|
const SizedBox(height: 12),
|
2022-10-19 15:52:10 +00:00
|
|
|
const SectionTitle(title: "On device"),
|
2022-06-12 16:39:28 +00:00
|
|
|
const SizedBox(height: 12),
|
2022-09-12 12:11:42 +00:00
|
|
|
const DeviceFoldersGridViewWidget(),
|
2022-04-30 12:18:26 +00:00
|
|
|
const Padding(padding: EdgeInsets.all(4)),
|
|
|
|
const Divider(),
|
2021-09-09 06:14:07 +00:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
|
|
children: [
|
2022-10-26 07:46:36 +00:00
|
|
|
SectionTitle(titleWithBrand: getOnEnteSection(context)),
|
2021-09-09 06:14:07 +00:00
|
|
|
_sortMenu(),
|
|
|
|
],
|
|
|
|
),
|
2023-01-03 08:29:26 +00:00
|
|
|
showDeleteAlbumsButton
|
|
|
|
? const Padding(
|
|
|
|
padding: EdgeInsets.only(top: 2, left: 8.5, right: 48),
|
|
|
|
child: DeleteEmptyAlbums(),
|
|
|
|
)
|
|
|
|
: const SizedBox.shrink(),
|
2022-06-12 16:39:28 +00:00
|
|
|
const SizedBox(height: 12),
|
2021-05-19 15:51:38 +00:00
|
|
|
Configuration.instance.hasConfiguredAccount()
|
2022-09-12 12:11:42 +00:00
|
|
|
? RemoteCollectionsGridViewWidget(collections)
|
2022-07-03 05:57:27 +00:00
|
|
|
: const EmptyState(),
|
2022-06-10 05:59:25 +00:00
|
|
|
const SizedBox(height: 10),
|
2022-04-30 12:18:26 +00:00
|
|
|
const Divider(),
|
2022-06-10 09:05:35 +00:00
|
|
|
const SizedBox(height: 16),
|
2022-04-28 13:08:27 +00:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
2022-10-27 07:23:45 +00:00
|
|
|
ArchivedCollectionsButtonWidget(trashAndHiddenTextStyle),
|
2022-10-31 09:56:59 +00:00
|
|
|
const SizedBox(height: 12),
|
2022-11-02 09:53:24 +00:00
|
|
|
HiddenCollectionsButtonWidget(trashAndHiddenTextStyle),
|
|
|
|
const SizedBox(height: 12),
|
2022-10-31 09:56:59 +00:00
|
|
|
TrashButtonWidget(trashAndHiddenTextStyle),
|
2022-04-28 13:08:27 +00:00
|
|
|
],
|
|
|
|
),
|
2021-10-13 12:05:20 +00:00
|
|
|
),
|
2022-07-04 06:02:17 +00:00
|
|
|
const Padding(padding: EdgeInsets.fromLTRB(12, 12, 12, 36)),
|
2021-02-08 09:26:49 +00:00
|
|
|
],
|
|
|
|
),
|
2020-12-12 01:11:12 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-09 06:14:07 +00:00
|
|
|
Widget _sortMenu() {
|
|
|
|
Text sortOptionText(AlbumSortKey key) {
|
|
|
|
String text = key.toString();
|
2021-09-08 20:19:25 +00:00
|
|
|
switch (key) {
|
|
|
|
case AlbumSortKey.albumName:
|
2022-04-28 13:08:27 +00:00
|
|
|
text = "Name";
|
2021-09-09 06:14:07 +00:00
|
|
|
break;
|
2022-04-28 13:08:27 +00:00
|
|
|
case AlbumSortKey.newestPhoto:
|
|
|
|
text = "Newest";
|
2021-09-09 06:14:07 +00:00
|
|
|
break;
|
2022-04-28 13:08:27 +00:00
|
|
|
case AlbumSortKey.lastUpdated:
|
|
|
|
text = "Last updated";
|
2021-09-08 20:19:25 +00:00
|
|
|
}
|
2022-06-11 08:23:52 +00:00
|
|
|
return Text(
|
|
|
|
text,
|
2022-12-30 12:10:17 +00:00
|
|
|
style: Theme.of(context).textTheme.subtitle1!.copyWith(
|
2022-04-28 13:08:27 +00:00
|
|
|
fontSize: 14,
|
2022-12-30 12:10:17 +00:00
|
|
|
color: Theme.of(context).iconTheme.color!.withOpacity(0.7),
|
2022-06-11 08:23:52 +00:00
|
|
|
),
|
|
|
|
);
|
2021-09-08 20:19:25 +00:00
|
|
|
}
|
2021-09-09 06:14:07 +00:00
|
|
|
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.only(right: 24),
|
|
|
|
child: PopupMenuButton(
|
2022-07-04 06:02:17 +00:00
|
|
|
offset: const Offset(10, 50),
|
2021-09-09 06:14:07 +00:00
|
|
|
initialValue: sortKey?.index ?? 0,
|
|
|
|
child: Align(
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
2022-07-04 06:02:17 +00:00
|
|
|
const Padding(
|
2022-04-28 13:08:27 +00:00
|
|
|
padding: EdgeInsets.only(left: 5.0),
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
width: 36,
|
|
|
|
height: 36,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Theme.of(context).hintColor.withOpacity(0.2),
|
|
|
|
borderRadius: BorderRadius.circular(18),
|
|
|
|
),
|
|
|
|
child: Icon(
|
|
|
|
Icons.sort,
|
|
|
|
color: Theme.of(context).iconTheme.color,
|
|
|
|
size: 20,
|
|
|
|
),
|
2021-09-09 06:14:07 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2021-09-11 07:01:47 +00:00
|
|
|
onSelected: (int index) async {
|
2021-09-11 07:12:53 +00:00
|
|
|
sortKey = AlbumSortKey.values[index];
|
2022-12-30 12:10:17 +00:00
|
|
|
await LocalSettings.instance.setAlbumSortKey(sortKey!);
|
2021-09-11 07:12:53 +00:00
|
|
|
setState(() {});
|
2021-09-09 06:14:07 +00:00
|
|
|
},
|
|
|
|
itemBuilder: (context) {
|
|
|
|
return List.generate(AlbumSortKey.values.length, (index) {
|
|
|
|
return PopupMenuItem(
|
|
|
|
value: index,
|
|
|
|
child: sortOptionText(AlbumSortKey.values[index]),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2021-09-08 20:19:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 15:29:34 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_localFilesSubscription.cancel();
|
|
|
|
_collectionUpdatesSubscription.cancel();
|
|
|
|
_loggedOutEvent.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get wantKeepAlive => true;
|
|
|
|
}
|