ente/lib/ui/collections_gallery_widget.dart

544 lines
18 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2020-10-26 11:18:00 +00:00
import 'package:fluttertoast/fluttertoast.dart';
2020-10-28 15:25:32 +00:00
import 'package:logging/logging.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/core/event_bus.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/events/backup_folders_updated_event.dart';
2020-10-28 12:03:28 +00:00
import 'package:photos/events/collection_updated_event.dart';
import 'package:photos/events/local_photos_updated_event.dart';
2020-10-26 11:18:00 +00:00
import 'package:photos/events/tab_changed_event.dart';
2021-03-17 21:07:17 +00:00
import 'package:photos/events/user_logged_out_event.dart';
import 'package:photos/models/collection_items.dart';
import 'package:photos/models/device_folder.dart';
import 'package:photos/services/collections_service.dart';
2021-10-12 20:01:51 +00:00
import 'package:photos/ui/archive_page.dart';
import 'package:photos/ui/collection_page.dart';
2020-12-12 01:11:12 +00:00
import 'package:photos/ui/common_elements.dart';
import 'package:photos/ui/device_folder_page.dart';
import 'package:photos/ui/loading_widget.dart';
import 'package:photos/ui/thumbnail_widget.dart';
2021-10-12 20:01:51 +00:00
import 'package:photos/ui/trash_page.dart';
import 'package:photos/utils/local_settings.dart';
import 'package:photos/utils/navigation_util.dart';
2020-10-26 11:18:00 +00:00
import 'package:photos/utils/toast_util.dart';
2021-09-17 04:44:10 +00:00
class CollectionsGalleryWidget extends StatefulWidget {
const CollectionsGalleryWidget({Key key}) : super(key: key);
@override
_CollectionsGalleryWidgetState createState() =>
_CollectionsGalleryWidgetState();
}
2020-11-10 11:36:51 +00:00
class _CollectionsGalleryWidgetState extends State<CollectionsGalleryWidget>
with AutomaticKeepAliveClientMixin {
2020-10-28 15:25:32 +00:00
final _logger = Logger("CollectionsGallery");
2020-10-28 12:03:28 +00:00
StreamSubscription<LocalPhotosUpdatedEvent> _localFilesSubscription;
StreamSubscription<CollectionUpdatedEvent> _collectionUpdatesSubscription;
StreamSubscription<BackupFoldersUpdatedEvent> _backupFoldersUpdatedEvent;
2021-03-17 21:07:17 +00:00
StreamSubscription<UserLoggedOutEvent> _loggedOutEvent;
AlbumSortKey sortKey;
@override
void initState() {
2020-10-28 12:03:28 +00:00
_localFilesSubscription =
Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
2021-04-19 17:08:12 +00:00
_logger.info("Files updated");
2020-10-28 12:03:28 +00:00
setState(() {});
});
_collectionUpdatesSubscription =
Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
setState(() {});
});
2021-03-17 21:07:17 +00:00
_loggedOutEvent = Bus.instance.on<UserLoggedOutEvent>().listen((event) {
setState(() {});
});
_backupFoldersUpdatedEvent =
Bus.instance.on<BackupFoldersUpdatedEvent>().listen((event) {
setState(() {});
});
sortKey = LocalSettings.instance.albumSortKey();
super.initState();
}
@override
Widget build(BuildContext context) {
2020-11-16 08:00:31 +00:00
super.build(context);
2021-04-19 17:08:12 +00:00
_logger.info("Building ");
return FutureBuilder<CollectionItems>(
future: _getCollections(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return _getCollectionsGalleryWidget(snapshot.data);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
} else {
return loadWidget;
}
},
);
}
Future<CollectionItems> _getCollections() async {
2020-10-30 22:30:54 +00:00
final filesDB = FilesDB.instance;
final collectionsService = CollectionsService.instance;
final userID = Configuration.instance.getUserID();
2021-04-19 17:08:12 +00:00
final List<DeviceFolder> folders = [];
final latestLocalFiles = await filesDB.getLatestLocalFiles();
for (final file in latestLocalFiles) {
folders.add(DeviceFolder(file.deviceFolder, file.deviceFolder, file));
}
folders.sort((first, second) =>
second.thumbnail.creationTime.compareTo(first.thumbnail.creationTime));
final List<CollectionWithThumbnail> collectionsWithThumbnail = [];
final latestCollectionFiles =
await collectionsService.getLatestCollectionFiles();
for (final file in latestCollectionFiles) {
final c = collectionsService.getCollectionByID(file.collectionID);
if (c.owner.id == userID) {
collectionsWithThumbnail.add(CollectionWithThumbnail(c, file));
2020-10-28 15:25:32 +00:00
}
2020-10-30 22:30:54 +00:00
}
2020-11-30 08:57:23 +00:00
collectionsWithThumbnail.sort((first, second) {
if (sortKey == AlbumSortKey.albumName) {
// alphabetical ASC order
return first.collection.name.compareTo(second.collection.name);
} else if (sortKey == AlbumSortKey.recentPhoto) {
return second.thumbnail.creationTime
.compareTo(first.thumbnail.creationTime);
} else {
return second.collection.updationTime
.compareTo(first.collection.updationTime);
}
2020-11-30 08:57:23 +00:00
});
2020-10-28 15:25:32 +00:00
return CollectionItems(folders, collectionsWithThumbnail);
}
2020-12-12 01:11:12 +00:00
Widget _getCollectionsGalleryWidget(CollectionItems items) {
return SingleChildScrollView(
child: Container(
margin: const EdgeInsets.only(bottom: 50),
child: Column(
children: [
Padding(padding: EdgeInsets.all(6)),
2021-04-06 00:07:53 +00:00
SectionTitle("on device"),
Padding(padding: EdgeInsets.all(8)),
2021-03-12 08:47:57 +00:00
items.folders.isEmpty
? Padding(
padding: const EdgeInsets.all(22),
child: nothingToSeeHere,
)
2021-08-05 19:35:09 +00:00
: SizedBox(
height: 170,
2021-03-12 08:47:57 +00:00
child: Align(
alignment: Alignment.centerLeft,
child: items.folders.isEmpty
? nothingToSeeHere
: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
physics:
ScrollPhysics(), // to disable GridView's scrolling
itemBuilder: (context, index) {
2021-04-27 15:29:34 +00:00
return DeviceFolderIcon(items.folders[index]);
2021-03-12 08:47:57 +00:00
},
itemCount: items.folders.length,
),
),
),
Padding(padding: EdgeInsets.all(4)),
Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
SectionTitle("on ente"),
_sortMenu(),
],
),
2021-04-06 00:07:53 +00:00
Padding(padding: EdgeInsets.all(12)),
Configuration.instance.hasConfiguredAccount()
? GridView.builder(
shrinkWrap: true,
2021-04-06 00:07:53 +00:00
padding: EdgeInsets.fromLTRB(0, 0, 12, 0),
physics: ScrollPhysics(), // to disable GridView's scrolling
itemBuilder: (context, index) {
return _buildCollection(
context, items.collections, index);
},
itemCount:
items.collections.length + 1, // To include the + button
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 12,
),
)
: nothingToSeeHere,
2021-09-17 04:44:10 +00:00
Divider(),
2021-09-23 07:30:37 +00:00
Padding(padding: EdgeInsets.all(8)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
OutlinedButton(
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
),
padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
side: BorderSide(
width: 2,
color: Colors.white12,
2021-09-23 07:30:37 +00:00
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: const [
Icon(
Icons.archive_outlined,
color: Colors.white,
),
Padding(padding: EdgeInsets.all(6)),
Text(
"archive",
style: TextStyle(
color: Colors.white,
),
),
],
2021-10-12 20:01:51 +00:00
),
onPressed: () async {
routeToPage(
context,
ArchivePage(),
);
}),
Padding(padding: EdgeInsets.fromLTRB(18,0,18,0)),
OutlinedButton(
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
),
padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
side: BorderSide(
width: 2,
color: Colors.white12,
2021-10-12 20:01:51 +00:00
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: const [
Icon(
Icons.delete_outline_sharp,
color: Colors.white,
),
Padding(padding: EdgeInsets.all(6)),
Text(
"trash",
style: TextStyle(
color: Colors.white,
),
),
],
),
onPressed: () async {
routeToPage(
context,
TrashPage(),
);
}),
],
),
Padding(padding: EdgeInsets.fromLTRB(12, 12, 12, 72)),
],
),
2020-12-12 01:11:12 +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:
text = "album name";
break;
2021-09-08 20:19:25 +00:00
case AlbumSortKey.lastUpdated:
text = "last updated";
break;
2021-09-08 20:19:25 +00:00
case AlbumSortKey.recentPhoto:
text = "recent photo";
break;
2021-09-08 20:19:25 +00:00
}
return Text(
text,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 13,
color: Colors.white.withOpacity(0.6),
),
);
2021-09-08 20:19:25 +00:00
}
return Padding(
padding: const EdgeInsets.only(right: 24),
child: PopupMenuButton(
offset: Offset(10, 40),
initialValue: sortKey?.index ?? 0,
child: Align(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
sortOptionText(sortKey),
Padding(padding: EdgeInsets.only(left: 5.0)),
Icon(
Icons.sort,
color: Theme.of(context).buttonColor,
size: 20,
),
],
),
),
2021-09-11 07:01:47 +00:00
onSelected: (int index) async {
2021-09-11 07:12:53 +00:00
sortKey = AlbumSortKey.values[index];
2021-09-11 07:01:47 +00:00
await LocalSettings.instance.setAlbumSortKey(sortKey);
2021-09-11 07:12:53 +00:00
setState(() {});
},
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
Widget _buildCollection(BuildContext context,
List<CollectionWithThumbnail> collections, int index) {
if (index < collections.length) {
final c = collections[index];
2021-04-27 15:35:42 +00:00
return CollectionItem(c);
2021-04-27 15:29:34 +00:00
} else {
return Container(
padding: EdgeInsets.fromLTRB(28, 0, 28, 58),
child: OutlinedButton(
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
side: BorderSide(
width: 1,
2021-05-12 15:59:07 +00:00
color: Theme.of(context).buttonColor.withOpacity(0.4),
2021-04-27 15:29:34 +00:00
),
),
child: Icon(
Icons.add,
2021-05-12 15:59:07 +00:00
color: Theme.of(context).buttonColor.withOpacity(0.7),
2021-04-27 15:29:34 +00:00
),
onPressed: () async {
await showToast(
"long press to select photos and click + to create an album",
toastLength: Toast.LENGTH_LONG);
Bus.instance.fire(
TabChangedEvent(0, TabChangedEventSource.collections_page));
},
),
);
}
}
@override
void dispose() {
_localFilesSubscription.cancel();
_collectionUpdatesSubscription.cancel();
_loggedOutEvent.cancel();
_backupFoldersUpdatedEvent.cancel();
super.dispose();
}
@override
bool get wantKeepAlive => true;
}
class DeviceFolderIcon extends StatelessWidget {
const DeviceFolderIcon(
this.folder, {
Key key,
}) : super(key: key);
static final kUnsyncedIconOverlay = Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.transparent,
Colors.black.withOpacity(0.6),
],
2021-08-05 19:35:09 +00:00
stops: const [0.7, 1],
),
),
child: Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.only(right: 8, bottom: 8),
child: Icon(
Icons.cloud_off_outlined,
size: 18,
color: Colors.white.withOpacity(0.9),
),
),
),
);
2021-04-27 15:29:34 +00:00
final DeviceFolder folder;
@override
Widget build(BuildContext context) {
final isBackedUp =
Configuration.instance.getPathsToBackUp().contains(folder.path);
return GestureDetector(
2020-12-12 00:31:06 +00:00
child: Container(
height: 140,
width: 142,
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
ClipRRect(
2020-12-12 01:11:12 +00:00
borderRadius: BorderRadius.circular(18.0),
2021-08-05 19:35:09 +00:00
child: SizedBox(
child: Hero(
tag: "device_folder:" + folder.path + folder.thumbnail.tag(),
child: Stack(
children: [
ThumbnailWidget(
folder.thumbnail,
shouldShowSyncStatus: false,
2021-05-08 18:05:51 +00:00
key: Key("device_folder:" +
folder.path +
folder.thumbnail.tag()),
),
isBackedUp ? Container() : kUnsyncedIconOverlay,
],
),
),
height: 120,
width: 120,
),
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: Text(
folder.name,
style: TextStyle(
2020-12-12 00:31:06 +00:00
fontSize: 12,
),
overflow: TextOverflow.ellipsis,
),
),
],
),
),
onTap: () {
routeToPage(context, DeviceFolderPage(folder));
},
);
}
2021-04-27 15:29:34 +00:00
}
2021-04-27 15:35:42 +00:00
class CollectionItem extends StatelessWidget {
CollectionItem(
this.c, {
2021-04-27 15:29:34 +00:00
Key key,
}) : super(key: Key(c.collection.id.toString()));
final CollectionWithThumbnail c;
@override
Widget build(BuildContext context) {
return GestureDetector(
child: Column(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(18.0),
2021-08-05 19:35:09 +00:00
child: SizedBox(
2021-04-27 15:29:34 +00:00
child: Hero(
tag: "collection" + c.thumbnail.tag(),
child: ThumbnailWidget(
c.thumbnail,
2021-05-08 18:05:51 +00:00
key: Key("collection" + c.thumbnail.tag()),
2021-04-27 15:29:34 +00:00
)),
height: 140,
width: 140,
2020-12-12 01:11:12 +00:00
),
2021-04-27 15:29:34 +00:00
),
Padding(padding: EdgeInsets.all(4)),
Expanded(
child: Text(
c.collection.name,
style: TextStyle(
fontSize: 16,
2020-12-12 01:11:12 +00:00
),
2021-04-27 15:29:34 +00:00
overflow: TextOverflow.ellipsis,
),
2020-12-12 00:31:06 +00:00
),
2021-04-27 15:29:34 +00:00
],
),
onTap: () {
2021-05-13 18:05:32 +00:00
routeToPage(context, CollectionPage(c));
2021-04-27 15:29:34 +00:00
},
);
}
}
class SectionTitle extends StatelessWidget {
final String title;
final Alignment alignment;
2021-06-16 15:11:30 +00:00
final double opacity;
const SectionTitle(
this.title, {
2021-08-05 19:35:09 +00:00
this.opacity = 0.8,
Key key,
this.alignment = Alignment.centerLeft,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
2021-08-05 19:35:09 +00:00
margin: EdgeInsets.fromLTRB(24, 12, 0, 0),
child: Column(
children: [
Align(
alignment: alignment,
child: Text(
title,
style: TextStyle(
fontWeight: FontWeight.bold,
2021-08-05 19:35:09 +00:00
color: Theme.of(context).buttonColor.withOpacity(opacity),
2020-12-12 00:31:06 +00:00
fontSize: 20,
letterSpacing: 1,
),
),
),
2021-08-05 19:35:09 +00:00
],
),
);
}
}