ente/lib/ui/collections_gallery_widget.dart

710 lines
26 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:math';
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';
2022-05-05 06:46:31 +00:00
import 'package:photos/db/trash_db.dart';
2022-04-28 13:08:27 +00:00
import 'package:photos/ente_theme_data.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';
2022-05-05 06:46:31 +00:00
import 'package:photos/models/magic_metadata.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;
2022-05-05 06:46:31 +00:00
String _loadReason = "init";
@override
void initState() {
2020-10-28 12:03:28 +00:00
_localFilesSubscription =
Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
2022-05-05 06:46:31 +00:00
_loadReason = (LocalPhotosUpdatedEvent).toString();
2020-10-28 12:03:28 +00:00
setState(() {});
});
_collectionUpdatesSubscription =
Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
2022-05-05 06:46:31 +00:00
_loadReason = (CollectionUpdatedEvent).toString();
setState(() {});
});
2021-03-17 21:07:17 +00:00
_loggedOutEvent = Bus.instance.on<UserLoggedOutEvent>().listen((event) {
2022-05-05 06:46:31 +00:00
_loadReason = (UserLoggedOutEvent).toString();
2021-03-17 21:07:17 +00:00
setState(() {});
});
_backupFoldersUpdatedEvent =
Bus.instance.on<BackupFoldersUpdatedEvent>().listen((event) {
2022-05-05 06:46:31 +00:00
_loadReason = (BackupFoldersUpdatedEvent).toString();
setState(() {});
});
sortKey = LocalSettings.instance.albumSortKey();
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");
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
}
2022-04-28 13:08:27 +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.newestPhoto) {
return second.thumbnail.creationTime
.compareTo(first.thumbnail.creationTime);
} else {
return second.collection.updationTime
.compareTo(first.collection.updationTime);
}
},
);
2020-10-28 15:25:32 +00:00
return CollectionItems(folders, collectionsWithThumbnail);
}
2020-12-12 01:11:12 +00:00
Widget _getCollectionsGalleryWidget(CollectionItems items) {
const double horizontalPaddingOfGridRow = 16;
const double crossAxisSpacingOfGrid = 5;
2022-05-04 03:13:28 +00:00
final TextStyle trashAndHiddenTextStyle = Theme.of(context)
.textTheme
.subtitle1
.copyWith(
color:
Theme.of(context).textTheme.subtitle1.color.withOpacity(0.5));
Size size = MediaQuery.of(context).size;
2022-04-28 14:47:05 +00:00
int albumsCountInOneRow = max(size.width ~/ 220.0, 2);
final double sideOfThumbnail = (size.width / 2) -
horizontalPaddingOfGridRow -
((crossAxisSpacingOfGrid / 2) * (albumsCountInOneRow - 1));
2020-12-12 01:11:12 +00:00
return SingleChildScrollView(
child: Container(
margin: const EdgeInsets.only(bottom: 50),
child: Column(
children: [
Padding(padding: EdgeInsets.all(6)),
2022-04-28 13:08:27 +00:00
SectionTitle("On device"),
2021-04-06 00:07:53 +00:00
Padding(padding: EdgeInsets.all(8)),
2021-03-12 08:47:57 +00:00
items.folders.isEmpty
? Padding(
padding: const EdgeInsets.all(22),
2022-04-28 13:08:27 +00:00
child: nothingToSeeHere(
textColor:
Theme.of(context).colorScheme.defaultTextColor),
2021-03-12 08:47:57 +00:00
)
2022-04-28 13:08:27 +00:00
: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: SizedBox(
height: 170,
child: Align(
alignment: Alignment.centerLeft,
child: items.folders.isEmpty
? nothingToSeeHere(
textColor: Theme.of(context)
.colorScheme
.defaultTextColor)
: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
padding: EdgeInsets.fromLTRB(6, 0, 6, 0),
2022-05-05 06:46:31 +00:00
physics: ScrollPhysics(),
// to disable GridView's scrolling
2022-04-28 13:08:27 +00:00
itemBuilder: (context, index) {
return DeviceFolderIcon(items.folders[index]);
},
itemCount: items.folders.length,
),
),
2021-03-12 08:47:57 +00:00
),
),
2022-04-30 12:18:26 +00:00
const Padding(padding: EdgeInsets.all(4)),
const Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
2022-04-28 13:08:27 +00:00
SectionTitle("On ente"),
_sortMenu(),
],
),
2021-04-06 00:07:53 +00:00
Padding(padding: EdgeInsets.all(12)),
Configuration.instance.hasConfiguredAccount()
2022-04-28 13:08:27 +00:00
? Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: GridView.builder(
shrinkWrap: true,
2022-05-05 06:46:31 +00:00
physics: ScrollPhysics(),
// to disable GridView's scrolling
2022-04-28 13:08:27 +00:00
itemBuilder: (context, index) {
return _buildCollection(
context, items.collections, index);
},
2022-05-05 06:46:31 +00:00
itemCount: items.collections.length + 1,
// To include the + button
2022-04-28 13:08:27 +00:00
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
2022-04-28 14:47:05 +00:00
crossAxisCount: albumsCountInOneRow,
2022-04-28 13:08:27 +00:00
mainAxisSpacing: 12,
crossAxisSpacing: crossAxisSpacingOfGrid,
childAspectRatio: sideOfThumbnail /
(sideOfThumbnail +
24)), //24 is height of album title
),
)
2022-04-28 13:08:27 +00:00
: nothingToSeeHere(
textColor: Theme.of(context).colorScheme.defaultTextColor),
2022-04-30 12:18:26 +00:00
const SizedBox(height: 20),
const Divider(),
const Padding(padding: EdgeInsets.all(8)),
2022-04-28 13:08:27 +00:00
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
children: [
OutlinedButton(
style: OutlinedButton.styleFrom(
backgroundColor: Theme.of(context).backgroundColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
padding: EdgeInsets.all(0),
side: BorderSide(
width: 0.5,
color:
Theme.of(context).iconTheme.color.withOpacity(0.24),
),
),
2022-05-05 06:46:31 +00:00
child: SizedBox(
2022-04-28 13:08:27 +00:00
height: 48,
width: double.infinity,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Icon(
Icons.delete,
color: Theme.of(context).iconTheme.color,
),
Padding(padding: EdgeInsets.all(6)),
2022-05-05 06:46:31 +00:00
FutureBuilder<int>(
future: TrashDB.instance.count(),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data > 0) {
return RichText(
text: TextSpan(
style: trashAndHiddenTextStyle,
children: [
TextSpan(
text: "Trash",
style: Theme.of(context)
.textTheme
.subtitle1),
TextSpan(text: " \u2022 "),
TextSpan(
text: snapshot.data.toString()),
//need to query in db and bring this value
]));
} else {
return RichText(
text: TextSpan(
style: trashAndHiddenTextStyle,
children: [
TextSpan(
text: "Trash",
style: Theme.of(context)
.textTheme
.subtitle1),
//need to query in db and bring this value
]));
}
},
),
2022-04-28 13:08:27 +00:00
],
),
Icon(
Icons.chevron_right,
color: Theme.of(context).iconTheme.color,
),
],
),
2022-04-28 13:08:27 +00:00
),
2021-10-12 20:01:51 +00:00
),
2022-04-28 13:08:27 +00:00
onPressed: () async {
routeToPage(
context,
TrashPage(),
);
},
),
2022-04-28 13:08:27 +00:00
SizedBox(height: 12),
OutlinedButton(
style: OutlinedButton.styleFrom(
backgroundColor: Theme.of(context).backgroundColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
padding: EdgeInsets.all(0),
side: BorderSide(
width: 0.5,
color:
Theme.of(context).iconTheme.color.withOpacity(0.24),
),
),
2022-04-28 13:08:27 +00:00
child: Container(
height: 48,
width: double.infinity,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Icon(
Icons.visibility_off,
color: Theme.of(context).iconTheme.color,
),
Padding(padding: EdgeInsets.all(6)),
2022-05-05 06:46:31 +00:00
FutureBuilder<int>(
future: FilesDB.instance
.fileCountWithVisibility(
2022-05-17 12:54:56 +00:00
kVisibilityArchive,
Configuration.instance.getUserID()),
2022-05-05 06:46:31 +00:00
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data > 0) {
return RichText(
text: TextSpan(
style: trashAndHiddenTextStyle,
children: [
TextSpan(
text: "Hidden",
style: Theme.of(context)
.textTheme
.subtitle1),
TextSpan(text: " \u2022 "),
TextSpan(
text: snapshot.data.toString()),
//need to query in db and bring this value
]));
} else {
return RichText(
text: TextSpan(
style: trashAndHiddenTextStyle,
children: [
TextSpan(
text: "Hidden",
style: Theme.of(context)
.textTheme
.subtitle1),
//need to query in db and bring this value
]));
}
},
),
2022-04-28 13:08:27 +00:00
],
),
Icon(
Icons.chevron_right,
color: Theme.of(context).iconTheme.color,
),
],
),
2022-04-28 13:08:27 +00:00
),
),
2022-04-28 13:08:27 +00:00
onPressed: () async {
routeToPage(
context,
ArchivePage(),
);
},
),
2022-04-28 13:08:27 +00:00
],
),
),
2022-04-28 13:08:27 +00:00
Padding(padding: EdgeInsets.fromLTRB(12, 12, 12, 36)),
],
),
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:
2022-04-28 13:08:27 +00:00
text = "Name";
break;
2022-04-28 13:08:27 +00:00
case AlbumSortKey.newestPhoto:
text = "Newest";
break;
2022-04-28 13:08:27 +00:00
case AlbumSortKey.lastUpdated:
text = "Last updated";
2021-09-08 20:19:25 +00:00
}
2022-04-28 13:08:27 +00:00
return Text(text,
style: Theme.of(context).textTheme.subtitle1.copyWith(
fontSize: 14,
color: Theme.of(context).iconTheme.color.withOpacity(0.7)));
2021-09-08 20:19:25 +00:00
}
return Padding(
padding: const EdgeInsets.only(right: 24),
child: PopupMenuButton(
2022-04-28 13:08:27 +00:00
offset: Offset(10, 50),
initialValue: sortKey?.index ?? 0,
child: Align(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
2022-04-28 13:08:27 +00:00
Padding(
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-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 {
2022-04-28 13:08:27 +00:00
return InkWell(
child: Container(
margin: EdgeInsets.fromLTRB(30, 30, 30, 54),
decoration: BoxDecoration(
color: Theme.of(context).backgroundColor,
boxShadow: [
BoxShadow(
blurRadius: 2,
spreadRadius: 0,
offset: Offset(0, 0),
color: Theme.of(context).iconTheme.color.withOpacity(0.3))
],
borderRadius: BorderRadius.circular(4),
2021-04-27 15:29:34 +00:00
),
child: Icon(
Icons.add,
2022-04-28 13:08:27 +00:00
color: Theme.of(context).iconTheme.color.withOpacity(0.25),
2021-04-27 15:29:34 +00:00
),
),
2022-04-28 13:08:27 +00:00
onTap: () 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));
},
2021-04-27 15:29:34 +00:00
);
}
}
@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(
2022-04-28 13:08:27 +00:00
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 2),
child: Container(
height: 140,
width: 120,
// padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(4),
child: SizedBox(
child: Hero(
tag:
"device_folder:" + folder.path + folder.thumbnail.tag(),
child: Stack(
children: [
ThumbnailWidget(
folder.thumbnail,
shouldShowSyncStatus: false,
key: Key("device_folder:" +
folder.path +
folder.thumbnail.tag()),
),
isBackedUp ? Container() : kUnsyncedIconOverlay,
],
),
),
2022-04-28 13:08:27 +00:00
height: 120,
width: 120,
),
),
2022-04-28 13:08:27 +00:00
Padding(
padding: const EdgeInsets.only(top: 10),
child: Text(
folder.name,
style: Theme.of(context)
.textTheme
.subtitle1
.copyWith(fontSize: 12, fontFamily: "Inter"),
overflow: TextOverflow.ellipsis,
),
),
2022-04-28 13:08:27 +00:00
],
),
),
),
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) {
2022-04-28 13:08:27 +00:00
TextStyle albumTitleTextStyle = Theme.of(context)
.textTheme
.subtitle1
.copyWith(fontSize: 14, fontFamily: "Inter");
final double sideOfThumbnail =
(MediaQuery.of(context).size.width / 2) - 18.5;
2021-04-27 15:29:34 +00:00
return GestureDetector(
child: Column(
2022-04-28 13:08:27 +00:00
crossAxisAlignment: CrossAxisAlignment.start,
2021-04-27 15:29:34 +00:00
children: <Widget>[
ClipRRect(
2022-04-28 13:08:27 +00:00
borderRadius: BorderRadius.circular(4),
2021-08-05 19:35:09 +00:00
child: SizedBox(
2022-04-28 13:08:27 +00:00
child: Hero(
tag: "collection" + c.thumbnail.tag(),
child: ThumbnailWidget(
c.thumbnail,
shouldShowArchiveStatus: c.collection.isArchived(),
key: Key(
"collection" + c.thumbnail.tag(),
),
)),
height: sideOfThumbnail,
width: sideOfThumbnail),
2021-04-27 15:29:34 +00:00
),
2022-04-28 13:08:27 +00:00
SizedBox(height: 4),
Row(
children: [
Container(
constraints: BoxConstraints(maxWidth: sideOfThumbnail - 40),
child: Text(
c.collection.name,
style: albumTitleTextStyle,
overflow: TextOverflow.ellipsis,
),
2020-12-12 01:11:12 +00:00
),
2022-05-05 06:46:31 +00:00
FutureBuilder<int>(
future: FilesDB.instance.collectionFileCount(c.collection.id),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data > 0) {
return RichText(
text: TextSpan(
style: albumTitleTextStyle.copyWith(
color:
albumTitleTextStyle.color.withOpacity(0.5)),
children: [
TextSpan(text: " \u2022 "),
TextSpan(text: snapshot.data.toString()),
//need to query in db and bring this value
]));
} else {
return Container();
}
},
2022-04-28 13:08:27 +00:00
),
],
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,
2022-04-28 13:08:27 +00:00
style:
Theme.of(context).textTheme.headline6.copyWith(fontSize: 22),
),
),
2021-08-05 19:35:09 +00:00
],
),
);
}
}