ente/lib/ui/shared_collections_gallery.dart

497 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';
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';
import 'package:photos/models/galleryType.dart';
2021-02-08 09:31:02 +00:00
import 'package:photos/services/collections_service.dart';
2020-11-02 14:38:59 +00:00
import 'package:photos/ui/collection_page.dart';
import 'package:photos/ui/collections_gallery_widget.dart';
2022-05-03 03:48:37 +00:00
import 'package:photos/ui/common/gradientButton.dart';
import 'package:photos/ui/loading_widget.dart';
import 'package:photos/ui/thumbnail_widget.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
_SharedCollectionGalleryState 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() {
_localFilesSubscription =
Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
_logger.info("Files updated");
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(() {});
});
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:
Future.value(CollectionsService.instance.getLatestCollectionFiles())
.then((files) async {
final List<CollectionWithThumbnail> outgoing = [];
final List<CollectionWithThumbnail> incoming = [];
for (final file in files) {
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) {
return second.collection.updationTime
.compareTo(first.collection.updationTime);
2020-11-02 14:38:59 +00:00
});
incoming.sort((first, second) {
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 loadWidget;
}
},
);
}
2020-11-02 14:38:59 +00:00
Widget _getSharedCollectionsGallery(SharedCollections collections) {
const double horizontalPaddingOfGridRow = 16;
const double crossAxisSpacingOfGrid = 9;
Size size = MediaQuery.of(context).size;
2022-05-03 03:48:37 +00:00
int albumsCountInOneRow = max(size.width ~/ 220.0, 2);
2022-06-12 12:44:05 +00:00
double totalWhiteSpaceOfRow = (horizontalPaddingOfGridRow * 2) +
(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-06-19 09:19:35 +00:00
SectionTitle("Shared with me"),
const SizedBox(height: 12),
2022-06-19 09:19:25 +00:00
!collections.incoming.isNotEmpty
? Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: GridView.builder(
shrinkWrap: true,
physics: 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,
childAspectRatio:
sideOfThumbnail / (sideOfThumbnail + 24),
), //24 is height of album title
),
)
: _getIncomingCollectionEmptyState(),
2022-06-19 09:19:35 +00:00
SectionTitle("Shared by me"),
const SizedBox(height: 12),
2022-06-19 09:19:25 +00:00
!collections.outgoing.isNotEmpty
2022-06-11 09:21:39 +00:00
? ListView.builder(
shrinkWrap: true,
padding: EdgeInsets.only(bottom: 12),
physics: NeverScrollableScrollPhysics(),
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(
"No one is sharing with you :(",
style: Theme.of(context).textTheme.caption,
),
Padding(padding: EdgeInsets.only(top: 14)),
SizedBox(
width: 200,
height: 50,
2022-05-03 03:48:37 +00:00
child: GradientButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
Icons.outgoing_mail,
2022-05-03 03:48:37 +00:00
color: Colors.white,
),
2022-05-03 03:48:37 +00:00
Padding(padding: EdgeInsets.all(2)),
Text(
2022-05-03 03:48:37 +00:00
"Invite",
style: gradientButtonTextTheme().copyWith(fontSize: 16),
),
],
),
2022-05-03 03:48:37 +00:00
linearGradientColors: const [
Color(0xFF2CD267),
Color(0xFF1DB954),
],
onTap: () async {
shareText("Check out https://ente.io");
},
),
),
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(
"Share your first album!",
style: Theme.of(context).textTheme.caption,
),
Padding(padding: EdgeInsets.only(top: 14)),
SizedBox(
width: 200,
height: 50,
2022-05-03 03:48:37 +00:00
child: GradientButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.person_add,
2022-05-03 03:48:37 +00:00
color: Colors.white,
),
2022-05-03 03:48:37 +00:00
Padding(padding: EdgeInsets.all(2)),
Text(
2022-05-03 03:48:37 +00:00
"Share",
style: gradientButtonTextTheme().copyWith(fontSize: 16),
),
],
),
2022-05-03 03:48:37 +00:00
linearGradientColors: const [
Color(0xFF2CD267),
Color(0xFF1DB954),
],
onTap: () async {
2022-06-11 08:23:52 +00:00
await showToast(
context,
2022-06-18 15:14:23 +00:00
"Open an album and tap the share button on the top right to share.",
2022-06-11 08:23:52 +00:00
toastLength: Toast.LENGTH_LONG,
);
Bus.instance.fire(
2022-06-11 08:23:52 +00:00
TabChangedEvent(1, TabChangedEventSource.collections_page),
);
},
),
),
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];
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(
"and " +
remaining.toString() +
" other" +
(remaining > 1 ? "s" : ""),
);
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-06-10 12:19:37 +00:00
margin: 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(
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
height: 60,
width: 60,
),
),
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,
style: TextStyle(
fontSize: 16,
),
),
2022-02-07 19:11:28 +00:00
Padding(padding: EdgeInsets.all(2)),
c.collection.publicURLs.isEmpty
? Container()
: Icon(Icons.link),
],
),
sharees.isEmpty
? Container()
: Padding(
padding: EdgeInsets.fromLTRB(0, 4, 0, 0),
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,
appBarType: GalleryType.owned_collection,
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;
TextStyle albumTitleTextStyle =
Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 14);
2022-06-12 12:44:05 +00:00
Size size = MediaQuery.of(context).size;
int albumsCountInOneRow = max(size.width ~/ 220.0, 2);
double totalWhiteSpaceOfRow = (horizontalPaddingOfGridRow * 2) +
(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(
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(
child: Text(
2020-11-09 12:44:00 +00:00
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,
),
padding: EdgeInsets.all(8),
margin: EdgeInsets.fromLTRB(0, 0, 4, 0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Theme.of(context)
.colorScheme
.defaultBackgroundColor,
),
),
),
],
),
height: sideOfThumbnail,
width: sideOfThumbnail,
),
),
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: [
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,
appBarType: GalleryType.shared_collection,
tagPrefix: "shared_collection",
),
);
},
);
}
}