ente/lib/ui/shared_collections_gallery.dart

464 lines
16 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:logging/logging.dart';
2020-10-28 15:25:32 +00:00
import 'package:photos/core/configuration.dart';
import 'package:photos/core/event_bus.dart';
import 'package:photos/db/files_db.dart';
2022-05-03 03:48:37 +00:00
import 'package:photos/ente_theme_data.dart';
import 'package:photos/events/collection_updated_event.dart';
import 'package:photos/events/local_photos_updated_event.dart';
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';
2022-07-03 10:09:01 +00:00
import 'package:photos/models/gallery_type.dart';
2021-02-08 09:31:02 +00:00
import 'package:photos/services/collections_service.dart';
import 'package:photos/ui/collections/section_title.dart';
2022-07-03 10:09:01 +00:00
import 'package:photos/ui/common/gradient_button.dart';
import 'package:photos/ui/common/loading_widget.dart';
import 'package:photos/ui/viewer/file/thumbnail_widget.dart';
import 'package:photos/ui/viewer/gallery/collection_page.dart';
import 'package:photos/utils/navigation_util.dart';
import 'package:photos/utils/share_util.dart';
import 'package:photos/utils/toast_util.dart';
class SharedCollectionGallery extends StatefulWidget {
const SharedCollectionGallery({Key key}) : super(key: key);
@override
2022-07-03 09:45:00 +00:00
State<SharedCollectionGallery> createState() =>
_SharedCollectionGalleryState();
}
2020-11-10 11:36:51 +00:00
class _SharedCollectionGalleryState extends State<SharedCollectionGallery>
with AutomaticKeepAliveClientMixin {
final Logger _logger = Logger("SharedCollectionGallery");
StreamSubscription<LocalPhotosUpdatedEvent> _localFilesSubscription;
StreamSubscription<CollectionUpdatedEvent> _collectionUpdatesSubscription;
2021-03-17 21:07:17 +00:00
StreamSubscription<UserLoggedOutEvent> _loggedOutEvent;
@override
void initState() {
2022-07-03 09:45:00 +00:00
_localFilesSubscription =
Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
_logger.info("Files updated");
setState(() {});
});
2022-07-03 09:45:00 +00:00
_collectionUpdatesSubscription =
Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
setState(() {});
});
2021-03-17 21:07:17 +00:00
_loggedOutEvent = Bus.instance.on<UserLoggedOutEvent>().listen((event) {
setState(() {});
});
super.initState();
}
@override
Widget build(BuildContext context) {
2020-11-30 08:57:11 +00:00
super.build(context);
2020-11-02 14:38:59 +00:00
return FutureBuilder<SharedCollections>(
2021-05-08 18:05:51 +00:00
future:
2022-07-03 09:45:00 +00:00
Future.value(CollectionsService.instance.getLatestCollectionFiles())
.then((files) async {
final List<CollectionWithThumbnail> outgoing = [];
final List<CollectionWithThumbnail> incoming = [];
for (final file in files) {
2022-07-03 09:45:00 +00:00
final c =
CollectionsService.instance.getCollectionByID(file.collectionID);
if (c.owner.id == Configuration.instance.getUserID()) {
if (c.sharees.isNotEmpty || c.publicURLs.isNotEmpty) {
outgoing.add(
CollectionWithThumbnail(
c,
file,
),
);
2020-11-02 14:38:59 +00:00
}
} else {
incoming.add(
CollectionWithThumbnail(
c,
file,
),
);
}
}
2020-11-02 14:38:59 +00:00
outgoing.sort((first, second) {
2022-07-03 09:45:00 +00:00
return second.collection.updationTime
.compareTo(first.collection.updationTime);
2020-11-02 14:38:59 +00:00
});
incoming.sort((first, second) {
2022-07-03 09:45:00 +00:00
return second.collection.updationTime
.compareTo(first.collection.updationTime);
});
2020-11-02 14:38:59 +00:00
return SharedCollections(outgoing, incoming);
}),
builder: (context, snapshot) {
if (snapshot.hasData) {
2020-11-02 14:38:59 +00:00
return _getSharedCollectionsGallery(snapshot.data);
} else if (snapshot.hasError) {
_logger.shout(snapshot.error);
return Center(child: Text(snapshot.error.toString()));
} else {
return const EnteLoadingWidget();
}
},
);
}
2020-11-02 14:38:59 +00:00
Widget _getSharedCollectionsGallery(SharedCollections collections) {
const double horizontalPaddingOfGridRow = 16;
const double crossAxisSpacingOfGrid = 9;
2022-08-29 14:43:31 +00:00
final Size size = MediaQuery.of(context).size;
final int albumsCountInOneRow = max(size.width ~/ 220.0, 2);
final double totalWhiteSpaceOfRow = (horizontalPaddingOfGridRow * 2) +
2022-07-03 09:45:00 +00:00
(albumsCountInOneRow - 1) * crossAxisSpacingOfGrid;
final double sideOfThumbnail = (size.width / albumsCountInOneRow) -
(totalWhiteSpaceOfRow / albumsCountInOneRow);
2020-11-02 14:38:59 +00:00
return SingleChildScrollView(
child: Container(
margin: const EdgeInsets.only(bottom: 50),
child: Column(
children: [
const SizedBox(height: 12),
2022-07-04 06:02:17 +00:00
const SectionTitle("Shared with me"),
const SizedBox(height: 12),
2022-06-19 09:24:40 +00:00
collections.incoming.isNotEmpty
? Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: GridView.builder(
shrinkWrap: true,
2022-07-04 06:02:17 +00:00
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
return IncomingCollectionItem(
2022-06-11 09:11:08 +00:00
collections.incoming[index],
);
},
itemCount: collections.incoming.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: albumsCountInOneRow,
mainAxisSpacing: 12,
crossAxisSpacing: crossAxisSpacingOfGrid,
2022-07-03 09:45:00 +00:00
childAspectRatio:
sideOfThumbnail / (sideOfThumbnail + 24),
), //24 is height of album title
),
)
: _getIncomingCollectionEmptyState(),
2022-07-04 06:02:17 +00:00
const SectionTitle("Shared by me"),
const SizedBox(height: 12),
2022-06-19 09:24:40 +00:00
collections.outgoing.isNotEmpty
2022-06-11 09:21:39 +00:00
? ListView.builder(
shrinkWrap: true,
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.only(bottom: 12),
physics: const NeverScrollableScrollPhysics(),
2022-06-11 09:21:39 +00:00
itemBuilder: (context, index) {
return OutgoingCollectionItem(
collections.outgoing[index],
);
},
itemCount: collections.outgoing.length,
)
: _getOutgoingCollectionEmptyState(),
const SizedBox(height: 32),
],
),
2020-11-02 14:38:59 +00:00
),
);
}
Widget _getIncomingCollectionEmptyState() {
return SizedBox(
2022-06-19 09:19:25 +00:00
height: 220,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
2022-06-19 09:24:13 +00:00
"Ask your loved ones to share",
style: Theme.of(context).textTheme.caption,
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.only(top: 14)),
SizedBox(
width: 200,
height: 50,
2022-05-03 03:48:37 +00:00
child: GradientButton(
2022-07-03 09:45:00 +00:00
onTap: () async {
shareText("Check out https://ente.io");
},
2022-07-04 04:45:32 +00:00
iconData: Icons.outgoing_mail,
paddingValue: 2,
text: "Invite",
),
),
2022-06-19 09:19:25 +00:00
const SizedBox(height: 60),
],
),
);
}
Widget _getOutgoingCollectionEmptyState() {
return SizedBox(
2022-06-19 09:19:25 +00:00
height: 200,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
2022-06-19 09:24:13 +00:00
"Share your first album",
style: Theme.of(context).textTheme.caption,
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.only(top: 14)),
SizedBox(
width: 200,
height: 50,
2022-05-03 03:48:37 +00:00
child: GradientButton(
2022-07-03 09:45:00 +00:00
onTap: () async {
await showToast(
context,
"Open an album and tap the share button on the top right to share.",
toastLength: Toast.LENGTH_LONG,
);
Bus.instance.fire(
TabChangedEvent(1, TabChangedEventSource.collectionsPage),
);
},
2022-07-04 04:45:32 +00:00
iconData: Icons.person_add,
paddingValue: 2,
text: "Share",
),
),
2022-06-19 09:19:25 +00:00
const SizedBox(height: 60),
],
),
);
}
@override
void dispose() {
_localFilesSubscription.cancel();
_collectionUpdatesSubscription.cancel();
_loggedOutEvent.cancel();
super.dispose();
}
@override
bool get wantKeepAlive => true;
}
class OutgoingCollectionItem extends StatelessWidget {
final CollectionWithThumbnail c;
const OutgoingCollectionItem(
this.c, {
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
2021-09-05 09:27:07 +00:00
final sharees = <String>[];
2020-12-07 17:32:35 +00:00
for (int index = 0; index < c.collection.sharees.length; index++) {
2022-01-24 18:16:07 +00:00
final sharee = c.collection.sharees[index];
2022-07-03 09:45:00 +00:00
final name =
(sharee.name?.isNotEmpty ?? false) ? sharee.name : sharee.email;
2020-12-07 17:32:35 +00:00
if (index < 2) {
2022-01-24 18:16:07 +00:00
sharees.add(name);
2020-12-07 17:32:35 +00:00
} else {
final remaining = c.collection.sharees.length - index;
if (remaining == 1) {
// If it's the last sharee
2022-01-24 18:16:07 +00:00
sharees.add(name);
2020-12-07 17:32:35 +00:00
} else {
2022-06-11 08:23:52 +00:00
sharees.add(
2022-07-03 09:45:00 +00:00
"and " +
remaining.toString() +
" other" +
(remaining > 1 ? "s" : ""),
2022-06-11 08:23:52 +00:00
);
2020-12-07 17:32:35 +00:00
}
break;
}
}
2020-11-02 14:38:59 +00:00
return GestureDetector(
behavior: HitTestBehavior.opaque,
2020-11-02 14:38:59 +00:00
child: Container(
2022-07-04 06:02:17 +00:00
margin: const EdgeInsets.fromLTRB(16, 12, 16, 12),
2020-11-02 14:38:59 +00:00
child: Row(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(3),
2022-01-24 18:16:07 +00:00
child: SizedBox(
2022-07-03 09:45:00 +00:00
height: 60,
width: 60,
2020-11-02 14:38:59 +00:00
child: Hero(
2022-06-11 08:23:52 +00:00
tag: "outgoing_collection" + c.thumbnail.tag(),
child: ThumbnailWidget(
c.thumbnail,
key: Key("outgoing_collection" + c.thumbnail.tag()),
),
),
2020-11-02 14:38:59 +00:00
),
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(8)),
2022-02-07 19:11:28 +00:00
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
c.collection.name,
2022-07-04 06:02:17 +00:00
style: const TextStyle(
2022-02-07 19:11:28 +00:00
fontSize: 16,
),
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(2)),
2022-07-03 09:45:00 +00:00
c.collection.publicURLs.isEmpty
? Container()
2022-07-04 06:02:17 +00:00
: const Icon(Icons.link),
2022-02-07 19:11:28 +00:00
],
),
sharees.isEmpty
? Container()
: Padding(
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.fromLTRB(0, 4, 0, 0),
2022-02-07 19:11:28 +00:00
child: Text(
2022-06-14 06:41:21 +00:00
"Shared with " + sharees.join(", "),
2022-02-07 19:11:28 +00:00
style: TextStyle(
fontSize: 14,
color: Theme.of(context).primaryColorLight,
),
textAlign: TextAlign.left,
overflow: TextOverflow.ellipsis,
2022-01-24 18:18:33 +00:00
),
),
2022-02-07 19:11:28 +00:00
],
),
2020-11-02 14:38:59 +00:00
),
],
),
),
2020-11-02 14:38:59 +00:00
onTap: () {
final page = CollectionPage(
2021-05-13 18:05:32 +00:00
c,
2022-07-03 07:47:15 +00:00
appBarType: GalleryType.ownedCollection,
2020-11-02 14:38:59 +00:00
tagPrefix: "outgoing_collection",
);
routeToPage(context, page);
2020-11-02 14:38:59 +00:00
},
);
}
}
class IncomingCollectionItem extends StatelessWidget {
final CollectionWithThumbnail c;
const IncomingCollectionItem(
this.c, {
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
2022-06-12 12:44:05 +00:00
const double horizontalPaddingOfGridRow = 16;
const double crossAxisSpacingOfGrid = 9;
2022-08-29 14:43:31 +00:00
final TextStyle albumTitleTextStyle =
2022-07-03 09:45:00 +00:00
Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 14);
2022-08-29 14:43:31 +00:00
final Size size = MediaQuery.of(context).size;
final int albumsCountInOneRow = max(size.width ~/ 220.0, 2);
final double totalWhiteSpaceOfRow = (horizontalPaddingOfGridRow * 2) +
2022-07-03 09:45:00 +00:00
(albumsCountInOneRow - 1) * crossAxisSpacingOfGrid;
final double sideOfThumbnail = (size.width / albumsCountInOneRow) -
(totalWhiteSpaceOfRow / albumsCountInOneRow);
return GestureDetector(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(4),
child: SizedBox(
2022-07-03 09:45:00 +00:00
height: sideOfThumbnail,
width: sideOfThumbnail,
child: Stack(
children: [
Hero(
2022-06-11 08:23:52 +00:00
tag: "shared_collection" + c.thumbnail.tag(),
child: ThumbnailWidget(
c.thumbnail,
key: Key("shared_collection" + c.thumbnail.tag()),
),
),
Align(
alignment: Alignment.bottomRight,
child: Container(
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.all(8),
margin: const EdgeInsets.fromLTRB(0, 0, 4, 0),
decoration: BoxDecoration(
shape: BoxShape.circle,
2022-07-03 09:45:00 +00:00
color: Theme.of(context)
.colorScheme
.defaultBackgroundColor,
),
child: Text(
c.collection.owner.name == null ||
c.collection.owner.name.isEmpty
? c.collection.owner.email.substring(0, 1)
: c.collection.owner.name.substring(0, 1),
textAlign: TextAlign.center,
),
),
),
],
),
),
),
2022-07-04 06:02:17 +00:00
const SizedBox(height: 4),
Row(
children: [
Container(
constraints: BoxConstraints(maxWidth: sideOfThumbnail - 40),
child: Text(
c.collection.name,
style: albumTitleTextStyle,
overflow: TextOverflow.ellipsis,
2020-11-10 16:19:06 +00:00
),
),
FutureBuilder<int>(
future: FilesDB.instance.collectionFileCount(c.collection.id),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data > 0) {
return RichText(
2022-06-12 12:44:05 +00:00
text: TextSpan(
style: albumTitleTextStyle.copyWith(
color: albumTitleTextStyle.color.withOpacity(0.5),
),
children: [
2022-07-04 06:02:17 +00:00
const TextSpan(text: " \u2022 "),
TextSpan(text: snapshot.data.toString()),
2022-06-12 12:44:05 +00:00
],
),
);
} else {
return Container();
}
},
),
],
),
],
),
onTap: () {
routeToPage(
2022-06-11 08:23:52 +00:00
context,
CollectionPage(
c,
2022-07-03 07:47:15 +00:00
appBarType: GalleryType.sharedCollection,
2022-06-11 08:23:52 +00:00
tagPrefix: "shared_collection",
),
);
},
);
}
}