shared_collections_gallery.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import 'dart:async';
  2. import 'dart:math';
  3. import 'package:flutter/material.dart';
  4. import 'package:fluttertoast/fluttertoast.dart';
  5. import 'package:logging/logging.dart';
  6. import 'package:photos/core/configuration.dart';
  7. import 'package:photos/core/event_bus.dart';
  8. import 'package:photos/db/files_db.dart';
  9. import 'package:photos/ente_theme_data.dart';
  10. import 'package:photos/events/collection_updated_event.dart';
  11. import 'package:photos/events/local_photos_updated_event.dart';
  12. import 'package:photos/events/tab_changed_event.dart';
  13. import 'package:photos/events/user_logged_out_event.dart';
  14. import 'package:photos/models/collection_items.dart';
  15. import 'package:photos/models/gallery_type.dart';
  16. import 'package:photos/services/collections_service.dart';
  17. import 'package:photos/ui/collections_gallery_widget.dart';
  18. import 'package:photos/ui/common/gradient_button.dart';
  19. import 'package:photos/ui/common/loading_widget.dart';
  20. import 'package:photos/ui/viewer/file/thumbnail_widget.dart';
  21. import 'package:photos/ui/viewer/gallery/collection_page.dart';
  22. import 'package:photos/utils/navigation_util.dart';
  23. import 'package:photos/utils/share_util.dart';
  24. import 'package:photos/utils/toast_util.dart';
  25. class SharedCollectionGallery extends StatefulWidget {
  26. const SharedCollectionGallery({Key key}) : super(key: key);
  27. @override
  28. State<SharedCollectionGallery> createState() =>
  29. _SharedCollectionGalleryState();
  30. }
  31. class _SharedCollectionGalleryState extends State<SharedCollectionGallery>
  32. with AutomaticKeepAliveClientMixin {
  33. final Logger _logger = Logger("SharedCollectionGallery");
  34. StreamSubscription<LocalPhotosUpdatedEvent> _localFilesSubscription;
  35. StreamSubscription<CollectionUpdatedEvent> _collectionUpdatesSubscription;
  36. StreamSubscription<UserLoggedOutEvent> _loggedOutEvent;
  37. @override
  38. void initState() {
  39. _localFilesSubscription =
  40. Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
  41. _logger.info("Files updated");
  42. setState(() {});
  43. });
  44. _collectionUpdatesSubscription =
  45. Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
  46. setState(() {});
  47. });
  48. _loggedOutEvent = Bus.instance.on<UserLoggedOutEvent>().listen((event) {
  49. setState(() {});
  50. });
  51. super.initState();
  52. }
  53. @override
  54. Widget build(BuildContext context) {
  55. super.build(context);
  56. return FutureBuilder<SharedCollections>(
  57. future:
  58. Future.value(CollectionsService.instance.getLatestCollectionFiles())
  59. .then((files) async {
  60. final List<CollectionWithThumbnail> outgoing = [];
  61. final List<CollectionWithThumbnail> incoming = [];
  62. for (final file in files) {
  63. final c =
  64. CollectionsService.instance.getCollectionByID(file.collectionID);
  65. if (c.owner.id == Configuration.instance.getUserID()) {
  66. if (c.sharees.isNotEmpty || c.publicURLs.isNotEmpty) {
  67. outgoing.add(
  68. CollectionWithThumbnail(
  69. c,
  70. file,
  71. ),
  72. );
  73. }
  74. } else {
  75. incoming.add(
  76. CollectionWithThumbnail(
  77. c,
  78. file,
  79. ),
  80. );
  81. }
  82. }
  83. outgoing.sort((first, second) {
  84. return second.collection.updationTime
  85. .compareTo(first.collection.updationTime);
  86. });
  87. incoming.sort((first, second) {
  88. return second.collection.updationTime
  89. .compareTo(first.collection.updationTime);
  90. });
  91. return SharedCollections(outgoing, incoming);
  92. }),
  93. builder: (context, snapshot) {
  94. if (snapshot.hasData) {
  95. return _getSharedCollectionsGallery(snapshot.data);
  96. } else if (snapshot.hasError) {
  97. _logger.shout(snapshot.error);
  98. return Center(child: Text(snapshot.error.toString()));
  99. } else {
  100. return const EnteLoadingWidget();
  101. }
  102. },
  103. );
  104. }
  105. Widget _getSharedCollectionsGallery(SharedCollections collections) {
  106. const double horizontalPaddingOfGridRow = 16;
  107. const double crossAxisSpacingOfGrid = 9;
  108. Size size = MediaQuery.of(context).size;
  109. int albumsCountInOneRow = max(size.width ~/ 220.0, 2);
  110. double totalWhiteSpaceOfRow = (horizontalPaddingOfGridRow * 2) +
  111. (albumsCountInOneRow - 1) * crossAxisSpacingOfGrid;
  112. final double sideOfThumbnail = (size.width / albumsCountInOneRow) -
  113. (totalWhiteSpaceOfRow / albumsCountInOneRow);
  114. return SingleChildScrollView(
  115. child: Container(
  116. margin: const EdgeInsets.only(bottom: 50),
  117. child: Column(
  118. children: [
  119. const SizedBox(height: 12),
  120. const SectionTitle("Shared with me"),
  121. const SizedBox(height: 12),
  122. collections.incoming.isNotEmpty
  123. ? Padding(
  124. padding: const EdgeInsets.symmetric(horizontal: 16),
  125. child: GridView.builder(
  126. shrinkWrap: true,
  127. physics: const NeverScrollableScrollPhysics(),
  128. itemBuilder: (context, index) {
  129. return IncomingCollectionItem(
  130. collections.incoming[index],
  131. );
  132. },
  133. itemCount: collections.incoming.length,
  134. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  135. crossAxisCount: albumsCountInOneRow,
  136. mainAxisSpacing: 12,
  137. crossAxisSpacing: crossAxisSpacingOfGrid,
  138. childAspectRatio:
  139. sideOfThumbnail / (sideOfThumbnail + 24),
  140. ), //24 is height of album title
  141. ),
  142. )
  143. : _getIncomingCollectionEmptyState(),
  144. const SectionTitle("Shared by me"),
  145. const SizedBox(height: 12),
  146. collections.outgoing.isNotEmpty
  147. ? ListView.builder(
  148. shrinkWrap: true,
  149. padding: const EdgeInsets.only(bottom: 12),
  150. physics: const NeverScrollableScrollPhysics(),
  151. itemBuilder: (context, index) {
  152. return OutgoingCollectionItem(
  153. collections.outgoing[index],
  154. );
  155. },
  156. itemCount: collections.outgoing.length,
  157. )
  158. : _getOutgoingCollectionEmptyState(),
  159. const SizedBox(height: 32),
  160. ],
  161. ),
  162. ),
  163. );
  164. }
  165. Widget _getIncomingCollectionEmptyState() {
  166. return SizedBox(
  167. height: 220,
  168. child: Column(
  169. mainAxisAlignment: MainAxisAlignment.center,
  170. children: [
  171. Text(
  172. "Ask your loved ones to share",
  173. style: Theme.of(context).textTheme.caption,
  174. ),
  175. const Padding(padding: EdgeInsets.only(top: 14)),
  176. SizedBox(
  177. width: 200,
  178. height: 50,
  179. child: GradientButton(
  180. onTap: () async {
  181. shareText("Check out https://ente.io");
  182. },
  183. iconData: Icons.outgoing_mail,
  184. paddingValue: 2,
  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. paddingValue: 2,
  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. TextStyle albumTitleTextStyle =
  350. Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 14);
  351. Size size = MediaQuery.of(context).size;
  352. int albumsCountInOneRow = max(size.width ~/ 220.0, 2);
  353. 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. }