ente/lib/ui/shared_collections_gallery.dart

494 lines
17 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';
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';
2023-04-07 04:57:54 +00:00
import "package:photos/generated/l10n.dart";
2023-01-26 05:38:45 +00:00
import 'package:photos/models/collection.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';
2022-12-17 07:40:19 +00:00
import 'package:photos/theme/colors.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/new_shared_collections_gallery.dart";
import 'package:photos/ui/sharing/user_avator_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");
late StreamSubscription<LocalPhotosUpdatedEvent> _localFilesSubscription;
late StreamSubscription<CollectionUpdatedEvent>
_collectionUpdatesSubscription;
late StreamSubscription<UserLoggedOutEvent> _loggedOutEvent;
@override
void initState() {
2022-07-03 09:45:00 +00:00
_localFilesSubscription =
Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
debugPrint("SetState Shared Collections on ${event.reason}");
setState(() {});
});
2022-07-03 09:45:00 +00:00
_collectionUpdatesSubscription =
Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
debugPrint("SetState Shared Collections on ${event.reason}");
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) {
2023-01-26 05:38:45 +00:00
if (file.collectionID == null) {
_logger.severe("collection id should not be null");
continue;
}
final Collection? c =
CollectionsService.instance.getCollectionByID(file.collectionID!);
if (c == null) {
_logger
.severe("shared collection is not cached ${file.collectionID}");
CollectionsService.instance
.fetchCollectionByID(file.collectionID!)
.ignore();
continue;
}
if (c.owner!.id == Configuration.instance.getUserID()) {
2023-01-25 11:02:08 +00:00
if (c.hasSharees || c.hasLink || c.isSharedFilesCollection()) {
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) {
if (second.collection.isSharedFilesCollection() ==
first.collection.isSharedFilesCollection()) {
return second.collection.updationTime
.compareTo(first.collection.updationTime);
} else {
if (first.collection.isSharedFilesCollection()) {
return 1;
}
return -1;
}
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) {
if ((snapshot.data?.incoming.length ?? 0) == 0 &&
(snapshot.data?.outgoing.length ?? 0) == 0) {
return const Center(child: EmptyStateWidget());
}
return _getSharedCollectionsGallery(snapshot.data!);
} else if (snapshot.hasError) {
2023-01-25 14:44:44 +00:00
_logger.severe(
"critical: failed to load share gallery",
snapshot.error,
snapshot.stackTrace,
);
2023-04-08 04:05:16 +00:00
return Center(child: Text(S.of(context).somethingWentWrong));
} 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),
2023-04-07 04:57:54 +00:00
SectionTitle(title: S.of(context).sharedWithMe),
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(),
2023-03-07 14:02:35 +00:00
const SizedBox(height: 16),
2023-04-07 04:57:54 +00:00
SectionTitle(title: S.of(context).sharedByMe),
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(
2023-04-07 04:57:54 +00:00
S.of(context).askYourLovedOnesToShare,
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 {
2023-04-07 04:57:54 +00:00
shareText(S.of(context).shareTextRecommendUsingEnte);
2022-07-03 09:45:00 +00:00
},
2022-07-04 04:45:32 +00:00
iconData: Icons.outgoing_mail,
2023-04-07 04:57:54 +00:00
text: S.of(context).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(
2023-04-07 04:57:54 +00:00
S.of(context).shareYourFirstAlbum,
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,
2023-04-07 04:57:54 +00:00
S.of(context).shareAlbumHint,
2022-07-03 09:45:00 +00:00
toastLength: Toast.LENGTH_LONG,
);
Bus.instance.fire(
TabChangedEvent(1, TabChangedEventSource.collectionsPage),
);
},
2022-07-04 04:45:32 +00:00
iconData: Icons.person_add,
2023-04-07 04:57:54 +00:00
text: S.of(context).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) {
2023-01-25 11:02:08 +00:00
final shareesName = <String>[];
if (c.collection.hasSharees) {
for (int index = 0; index < c.collection.sharees!.length; index++) {
final sharee = c.collection.sharees![index]!;
final String name =
(sharee.name?.isNotEmpty ?? false) ? sharee.name! : sharee.email;
if (index < 2) {
shareesName.add(name);
2020-12-07 17:32:35 +00:00
} else {
2023-01-25 11:02:08 +00:00
final remaining = c.collection.sharees!.length - index;
if (remaining == 1) {
// If it's the last sharee
shareesName.add(name);
} else {
shareesName.add(
"and " +
remaining.toString() +
" other" +
(remaining > 1 ? "s" : ""),
);
}
break;
2020-12-07 17:32:35 +00:00
}
}
}
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(
2022-11-21 12:07:24 +00:00
borderRadius: BorderRadius.circular(1),
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(
tag: "outgoing_collection" + c.thumbnail!.tag,
2022-06-11 08:23:52 +00:00
child: ThumbnailWidget(
c.thumbnail,
key: Key("outgoing_collection" + c.thumbnail!.tag),
2022-06-11 08:23:52 +00:00
),
),
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.displayName,
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)),
2023-01-25 11:02:08 +00:00
c.collection.hasLink
? (c.collection.publicURLs!.first!.isExpired
2022-12-17 07:40:19 +00:00
? const Icon(
Icons.link,
color: warning500,
)
2023-01-25 11:02:08 +00:00
: const Icon(Icons.link))
: Container(),
2022-02-07 19:11:28 +00:00
],
),
2023-01-25 11:02:08 +00:00
shareesName.isEmpty
2022-02-07 19:11:28 +00:00
? 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(
2023-04-07 04:57:54 +00:00
S.of(context).sharedWith(shareesName.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 =
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(
2022-11-21 12:07:24 +00:00
borderRadius: BorderRadius.circular(1),
child: SizedBox(
2022-07-03 09:45:00 +00:00
height: sideOfThumbnail,
width: sideOfThumbnail,
child: Stack(
children: [
Hero(
tag: "shared_collection" + c.thumbnail!.tag,
2022-06-11 08:23:52 +00:00
child: ThumbnailWidget(
c.thumbnail,
key: Key("shared_collection" + c.thumbnail!.tag),
2022-06-11 08:23:52 +00:00
),
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.only(right: 8.0, bottom: 8.0),
child: UserAvatarWidget(
c.collection.owner!,
2022-12-16 13:09:14 +00:00
thumbnailView: true,
),
),
),
],
),
),
),
2022-07-04 06:02:17 +00:00
const SizedBox(height: 4),
Row(
children: [
Container(
constraints: BoxConstraints(maxWidth: sideOfThumbnail - 40),
child: Text(
c.collection.displayName,
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),
2022-06-12 12:44:05 +00:00
),
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",
),
);
},
);
}
}