shared_collections_gallery.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // @dart=2.9
  2. import 'dart:async';
  3. import 'dart:math';
  4. import 'package:flutter/material.dart';
  5. import 'package:fluttertoast/fluttertoast.dart';
  6. import 'package:logging/logging.dart';
  7. import 'package:photos/core/configuration.dart';
  8. import 'package:photos/core/event_bus.dart';
  9. import 'package:photos/db/files_db.dart';
  10. import 'package:photos/ente_theme_data.dart';
  11. import 'package:photos/events/collection_updated_event.dart';
  12. import 'package:photos/events/local_photos_updated_event.dart';
  13. import 'package:photos/events/tab_changed_event.dart';
  14. import 'package:photos/events/user_logged_out_event.dart';
  15. import 'package:photos/models/collection_items.dart';
  16. import 'package:photos/models/gallery_type.dart';
  17. import 'package:photos/services/collections_service.dart';
  18. import 'package:photos/ui/collections/section_title.dart';
  19. import 'package:photos/ui/common/gradient_button.dart';
  20. import 'package:photos/ui/common/loading_widget.dart';
  21. import 'package:photos/ui/viewer/file/thumbnail_widget.dart';
  22. import 'package:photos/ui/viewer/gallery/collection_page.dart';
  23. import 'package:photos/utils/navigation_util.dart';
  24. import 'package:photos/utils/share_util.dart';
  25. import 'package:photos/utils/toast_util.dart';
  26. class SharedCollectionGallery extends StatefulWidget {
  27. const SharedCollectionGallery({Key key}) : super(key: key);
  28. @override
  29. State<SharedCollectionGallery> createState() =>
  30. _SharedCollectionGalleryState();
  31. }
  32. class _SharedCollectionGalleryState extends State<SharedCollectionGallery>
  33. with AutomaticKeepAliveClientMixin {
  34. final Logger _logger = Logger("SharedCollectionGallery");
  35. StreamSubscription<LocalPhotosUpdatedEvent> _localFilesSubscription;
  36. StreamSubscription<CollectionUpdatedEvent> _collectionUpdatesSubscription;
  37. StreamSubscription<UserLoggedOutEvent> _loggedOutEvent;
  38. @override
  39. void initState() {
  40. _localFilesSubscription =
  41. Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
  42. _logger.info("Files updated");
  43. setState(() {});
  44. });
  45. _collectionUpdatesSubscription =
  46. Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
  47. setState(() {});
  48. });
  49. _loggedOutEvent = Bus.instance.on<UserLoggedOutEvent>().listen((event) {
  50. setState(() {});
  51. });
  52. super.initState();
  53. }
  54. @override
  55. Widget build(BuildContext context) {
  56. super.build(context);
  57. return FutureBuilder<SharedCollections>(
  58. future:
  59. Future.value(CollectionsService.instance.getLatestCollectionFiles())
  60. .then((files) async {
  61. final List<CollectionWithThumbnail> outgoing = [];
  62. final List<CollectionWithThumbnail> incoming = [];
  63. for (final file in files) {
  64. final c =
  65. CollectionsService.instance.getCollectionByID(file.collectionID);
  66. if (c.owner.id == Configuration.instance.getUserID()) {
  67. if (c.sharees.isNotEmpty || c.publicURLs.isNotEmpty) {
  68. outgoing.add(
  69. CollectionWithThumbnail(
  70. c,
  71. file,
  72. ),
  73. );
  74. }
  75. } else {
  76. incoming.add(
  77. CollectionWithThumbnail(
  78. c,
  79. file,
  80. ),
  81. );
  82. }
  83. }
  84. outgoing.sort((first, second) {
  85. return second.collection.updationTime
  86. .compareTo(first.collection.updationTime);
  87. });
  88. incoming.sort((first, second) {
  89. return second.collection.updationTime
  90. .compareTo(first.collection.updationTime);
  91. });
  92. return SharedCollections(outgoing, incoming);
  93. }),
  94. builder: (context, snapshot) {
  95. if (snapshot.hasData) {
  96. return _getSharedCollectionsGallery(snapshot.data);
  97. } else if (snapshot.hasError) {
  98. _logger.shout(snapshot.error);
  99. return Center(child: Text(snapshot.error.toString()));
  100. } else {
  101. return const EnteLoadingWidget();
  102. }
  103. },
  104. );
  105. }
  106. Widget _getSharedCollectionsGallery(SharedCollections collections) {
  107. const double horizontalPaddingOfGridRow = 16;
  108. const double crossAxisSpacingOfGrid = 9;
  109. final Size size = MediaQuery.of(context).size;
  110. final int albumsCountInOneRow = max(size.width ~/ 220.0, 2);
  111. final double totalWhiteSpaceOfRow = (horizontalPaddingOfGridRow * 2) +
  112. (albumsCountInOneRow - 1) * crossAxisSpacingOfGrid;
  113. final double sideOfThumbnail = (size.width / albumsCountInOneRow) -
  114. (totalWhiteSpaceOfRow / albumsCountInOneRow);
  115. return SingleChildScrollView(
  116. child: Container(
  117. margin: const EdgeInsets.only(bottom: 50),
  118. child: Column(
  119. children: [
  120. const SizedBox(height: 12),
  121. const SectionTitle(title: "Shared with me"),
  122. const SizedBox(height: 12),
  123. collections.incoming.isNotEmpty
  124. ? Padding(
  125. padding: const EdgeInsets.symmetric(horizontal: 16),
  126. child: GridView.builder(
  127. shrinkWrap: true,
  128. physics: const NeverScrollableScrollPhysics(),
  129. itemBuilder: (context, index) {
  130. return IncomingCollectionItem(
  131. collections.incoming[index],
  132. );
  133. },
  134. itemCount: collections.incoming.length,
  135. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  136. crossAxisCount: albumsCountInOneRow,
  137. mainAxisSpacing: 12,
  138. crossAxisSpacing: crossAxisSpacingOfGrid,
  139. childAspectRatio:
  140. sideOfThumbnail / (sideOfThumbnail + 24),
  141. ), //24 is height of album title
  142. ),
  143. )
  144. : _getIncomingCollectionEmptyState(),
  145. const SectionTitle(title: "Shared by me"),
  146. const SizedBox(height: 12),
  147. collections.outgoing.isNotEmpty
  148. ? ListView.builder(
  149. shrinkWrap: true,
  150. padding: const EdgeInsets.only(bottom: 12),
  151. physics: const NeverScrollableScrollPhysics(),
  152. itemBuilder: (context, index) {
  153. return OutgoingCollectionItem(
  154. collections.outgoing[index],
  155. );
  156. },
  157. itemCount: collections.outgoing.length,
  158. )
  159. : _getOutgoingCollectionEmptyState(),
  160. const SizedBox(height: 32),
  161. ],
  162. ),
  163. ),
  164. );
  165. }
  166. Widget _getIncomingCollectionEmptyState() {
  167. return SizedBox(
  168. height: 220,
  169. child: Column(
  170. mainAxisAlignment: MainAxisAlignment.center,
  171. children: [
  172. Text(
  173. "Ask your loved ones to share",
  174. style: Theme.of(context).textTheme.caption,
  175. ),
  176. const Padding(padding: EdgeInsets.only(top: 14)),
  177. SizedBox(
  178. width: 200,
  179. height: 50,
  180. child: GradientButton(
  181. onTap: () async {
  182. shareText("Check out https://ente.io");
  183. },
  184. iconData: Icons.outgoing_mail,
  185. text: "Invite",
  186. ),
  187. ),
  188. const SizedBox(height: 60),
  189. ],
  190. ),
  191. );
  192. }
  193. Widget _getOutgoingCollectionEmptyState() {
  194. return SizedBox(
  195. height: 200,
  196. child: Column(
  197. mainAxisAlignment: MainAxisAlignment.center,
  198. children: [
  199. Text(
  200. "Share your first album",
  201. style: Theme.of(context).textTheme.caption,
  202. ),
  203. const Padding(padding: EdgeInsets.only(top: 14)),
  204. SizedBox(
  205. width: 200,
  206. height: 50,
  207. child: GradientButton(
  208. onTap: () async {
  209. await showToast(
  210. context,
  211. "Open an album and tap the share button on the top right to share.",
  212. toastLength: Toast.LENGTH_LONG,
  213. );
  214. Bus.instance.fire(
  215. TabChangedEvent(1, TabChangedEventSource.collectionsPage),
  216. );
  217. },
  218. iconData: Icons.person_add,
  219. text: "Share",
  220. ),
  221. ),
  222. const SizedBox(height: 60),
  223. ],
  224. ),
  225. );
  226. }
  227. @override
  228. void dispose() {
  229. _localFilesSubscription.cancel();
  230. _collectionUpdatesSubscription.cancel();
  231. _loggedOutEvent.cancel();
  232. super.dispose();
  233. }
  234. @override
  235. bool get wantKeepAlive => true;
  236. }
  237. class OutgoingCollectionItem extends StatelessWidget {
  238. final CollectionWithThumbnail c;
  239. const OutgoingCollectionItem(
  240. this.c, {
  241. Key key,
  242. }) : super(key: key);
  243. @override
  244. Widget build(BuildContext context) {
  245. final sharees = <String>[];
  246. for (int index = 0; index < c.collection.sharees.length; index++) {
  247. final sharee = c.collection.sharees[index];
  248. final name =
  249. (sharee.name?.isNotEmpty ?? false) ? sharee.name : sharee.email;
  250. if (index < 2) {
  251. sharees.add(name);
  252. } else {
  253. final remaining = c.collection.sharees.length - index;
  254. if (remaining == 1) {
  255. // If it's the last sharee
  256. sharees.add(name);
  257. } else {
  258. sharees.add(
  259. "and " +
  260. remaining.toString() +
  261. " other" +
  262. (remaining > 1 ? "s" : ""),
  263. );
  264. }
  265. break;
  266. }
  267. }
  268. return GestureDetector(
  269. behavior: HitTestBehavior.opaque,
  270. child: Container(
  271. margin: const EdgeInsets.fromLTRB(16, 12, 16, 12),
  272. child: Row(
  273. children: <Widget>[
  274. ClipRRect(
  275. borderRadius: BorderRadius.circular(3),
  276. child: SizedBox(
  277. height: 60,
  278. width: 60,
  279. child: Hero(
  280. tag: "outgoing_collection" + c.thumbnail.tag,
  281. child: ThumbnailWidget(
  282. c.thumbnail,
  283. key: Key("outgoing_collection" + c.thumbnail.tag),
  284. ),
  285. ),
  286. ),
  287. ),
  288. const Padding(padding: EdgeInsets.all(8)),
  289. Expanded(
  290. child: Column(
  291. crossAxisAlignment: CrossAxisAlignment.start,
  292. children: [
  293. Row(
  294. children: [
  295. Text(
  296. c.collection.name,
  297. style: const TextStyle(
  298. fontSize: 16,
  299. ),
  300. ),
  301. const Padding(padding: EdgeInsets.all(2)),
  302. c.collection.publicURLs.isEmpty
  303. ? Container()
  304. : const Icon(Icons.link),
  305. ],
  306. ),
  307. sharees.isEmpty
  308. ? Container()
  309. : Padding(
  310. padding: const EdgeInsets.fromLTRB(0, 4, 0, 0),
  311. child: Text(
  312. "Shared with " + sharees.join(", "),
  313. style: TextStyle(
  314. fontSize: 14,
  315. color: Theme.of(context).primaryColorLight,
  316. ),
  317. textAlign: TextAlign.left,
  318. overflow: TextOverflow.ellipsis,
  319. ),
  320. ),
  321. ],
  322. ),
  323. ),
  324. ],
  325. ),
  326. ),
  327. onTap: () {
  328. final page = CollectionPage(
  329. c,
  330. appBarType: GalleryType.ownedCollection,
  331. tagPrefix: "outgoing_collection",
  332. );
  333. routeToPage(context, page);
  334. },
  335. );
  336. }
  337. }
  338. class IncomingCollectionItem extends StatelessWidget {
  339. final CollectionWithThumbnail c;
  340. const IncomingCollectionItem(
  341. this.c, {
  342. Key key,
  343. }) : super(key: key);
  344. @override
  345. Widget build(BuildContext context) {
  346. const double horizontalPaddingOfGridRow = 16;
  347. const double crossAxisSpacingOfGrid = 9;
  348. final TextStyle albumTitleTextStyle =
  349. Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 14);
  350. final Size size = MediaQuery.of(context).size;
  351. final int albumsCountInOneRow = max(size.width ~/ 220.0, 2);
  352. final double totalWhiteSpaceOfRow = (horizontalPaddingOfGridRow * 2) +
  353. (albumsCountInOneRow - 1) * crossAxisSpacingOfGrid;
  354. final double sideOfThumbnail = (size.width / albumsCountInOneRow) -
  355. (totalWhiteSpaceOfRow / albumsCountInOneRow);
  356. return GestureDetector(
  357. child: Column(
  358. crossAxisAlignment: CrossAxisAlignment.start,
  359. children: <Widget>[
  360. ClipRRect(
  361. borderRadius: BorderRadius.circular(4),
  362. child: SizedBox(
  363. height: sideOfThumbnail,
  364. width: sideOfThumbnail,
  365. child: Stack(
  366. children: [
  367. Hero(
  368. tag: "shared_collection" + c.thumbnail.tag,
  369. child: ThumbnailWidget(
  370. c.thumbnail,
  371. key: Key("shared_collection" + c.thumbnail.tag),
  372. ),
  373. ),
  374. Align(
  375. alignment: Alignment.bottomRight,
  376. child: Container(
  377. padding: const EdgeInsets.all(8),
  378. margin: const EdgeInsets.fromLTRB(0, 0, 4, 0),
  379. decoration: BoxDecoration(
  380. shape: BoxShape.circle,
  381. color: Theme.of(context)
  382. .colorScheme
  383. .defaultBackgroundColor,
  384. ),
  385. child: Text(
  386. c.collection.owner.name == null ||
  387. c.collection.owner.name.isEmpty
  388. ? c.collection.owner.email.substring(0, 1)
  389. : c.collection.owner.name.substring(0, 1),
  390. textAlign: TextAlign.center,
  391. ),
  392. ),
  393. ),
  394. ],
  395. ),
  396. ),
  397. ),
  398. const SizedBox(height: 4),
  399. Row(
  400. children: [
  401. Container(
  402. constraints: BoxConstraints(maxWidth: sideOfThumbnail - 40),
  403. child: Text(
  404. c.collection.name,
  405. style: albumTitleTextStyle,
  406. overflow: TextOverflow.ellipsis,
  407. ),
  408. ),
  409. FutureBuilder<int>(
  410. future: FilesDB.instance.collectionFileCount(c.collection.id),
  411. builder: (context, snapshot) {
  412. if (snapshot.hasData && snapshot.data > 0) {
  413. return RichText(
  414. text: TextSpan(
  415. style: albumTitleTextStyle.copyWith(
  416. color: albumTitleTextStyle.color.withOpacity(0.5),
  417. ),
  418. children: [
  419. const TextSpan(text: " \u2022 "),
  420. TextSpan(text: snapshot.data.toString()),
  421. ],
  422. ),
  423. );
  424. } else {
  425. return Container();
  426. }
  427. },
  428. ),
  429. ],
  430. ),
  431. ],
  432. ),
  433. onTap: () {
  434. routeToPage(
  435. context,
  436. CollectionPage(
  437. c,
  438. appBarType: GalleryType.sharedCollection,
  439. tagPrefix: "shared_collection",
  440. ),
  441. );
  442. },
  443. );
  444. }
  445. }