shared_collections_gallery.dart 16 KB

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