shared_collections_gallery.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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/galleryType.dart';
  16. import 'package:photos/services/collections_service.dart';
  17. import 'package:photos/ui/collection_page.dart';
  18. import 'package:photos/ui/collections_gallery_widget.dart';
  19. import 'package:photos/ui/common/gradientButton.dart';
  20. import 'package:photos/ui/loading_widget.dart';
  21. import 'package:photos/ui/thumbnail_widget.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. _SharedCollectionGalleryState 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 loadWidget;
  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. SectionTitle("Incoming"),
  121. const SizedBox(height: 24),
  122. collections.incoming.isNotEmpty
  123. ? Padding(
  124. padding: const EdgeInsets.symmetric(horizontal: 16),
  125. child: GridView.builder(
  126. shrinkWrap: true,
  127. physics: 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 SizedBox(height: 32),
  145. SectionTitle("Outgoing"),
  146. const SizedBox(height: 12),
  147. collections.outgoing.isNotEmpty
  148. ? ListView.builder(
  149. shrinkWrap: true,
  150. padding: EdgeInsets.only(bottom: 12),
  151. physics: 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 Container(
  168. padding: EdgeInsets.only(top: 10),
  169. child: Column(
  170. children: [
  171. Text(
  172. "No one is sharing with you",
  173. style: TextStyle(
  174. color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
  175. ),
  176. ),
  177. Container(
  178. padding: EdgeInsets.symmetric(vertical: 12, horizontal: 72),
  179. child: GradientButton(
  180. child: Row(
  181. mainAxisAlignment: MainAxisAlignment.center,
  182. crossAxisAlignment: CrossAxisAlignment.center,
  183. children: [
  184. Icon(
  185. Icons.outgoing_mail,
  186. color: Colors.white,
  187. ),
  188. Padding(padding: EdgeInsets.all(2)),
  189. Text(
  190. "Invite",
  191. style: gradientButtonTextTheme(),
  192. ),
  193. ],
  194. ),
  195. linearGradientColors: const [
  196. Color(0xFF2CD267),
  197. Color(0xFF1DB954),
  198. ],
  199. onTap: () async {
  200. shareText("Check out https://ente.io");
  201. },
  202. ),
  203. ),
  204. ],
  205. ),
  206. );
  207. }
  208. Widget _getOutgoingCollectionEmptyState() {
  209. return Container(
  210. padding: EdgeInsets.only(top: 10),
  211. child: Column(
  212. children: [
  213. Text(
  214. "You aren't sharing anything",
  215. style: TextStyle(
  216. color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
  217. ),
  218. ),
  219. Container(
  220. padding: EdgeInsets.symmetric(vertical: 12, horizontal: 72),
  221. child: GradientButton(
  222. child: Row(
  223. mainAxisAlignment: MainAxisAlignment.center,
  224. crossAxisAlignment: CrossAxisAlignment.center,
  225. mainAxisSize: MainAxisSize.min,
  226. children: [
  227. Icon(
  228. Icons.person_add,
  229. color: Colors.white,
  230. ),
  231. Padding(padding: EdgeInsets.all(2)),
  232. Text(
  233. "Share",
  234. style: gradientButtonTextTheme(),
  235. ),
  236. ],
  237. ),
  238. linearGradientColors: const [
  239. Color(0xFF2CD267),
  240. Color(0xFF1DB954),
  241. ],
  242. onTap: () async {
  243. await showToast(
  244. context,
  245. "Select an album on ente to share",
  246. toastLength: Toast.LENGTH_LONG,
  247. );
  248. Bus.instance.fire(
  249. TabChangedEvent(1, TabChangedEventSource.collections_page),
  250. );
  251. },
  252. ),
  253. ),
  254. ],
  255. ),
  256. );
  257. }
  258. @override
  259. void dispose() {
  260. _localFilesSubscription.cancel();
  261. _collectionUpdatesSubscription.cancel();
  262. _loggedOutEvent.cancel();
  263. super.dispose();
  264. }
  265. @override
  266. bool get wantKeepAlive => true;
  267. }
  268. class OutgoingCollectionItem extends StatelessWidget {
  269. final CollectionWithThumbnail c;
  270. const OutgoingCollectionItem(
  271. this.c, {
  272. Key key,
  273. }) : super(key: key);
  274. @override
  275. Widget build(BuildContext context) {
  276. final sharees = <String>[];
  277. for (int index = 0; index < c.collection.sharees.length; index++) {
  278. final sharee = c.collection.sharees[index];
  279. final name =
  280. (sharee.name?.isNotEmpty ?? false) ? sharee.name : sharee.email;
  281. if (index < 2) {
  282. sharees.add(name);
  283. } else {
  284. final remaining = c.collection.sharees.length - index;
  285. if (remaining == 1) {
  286. // If it's the last sharee
  287. sharees.add(name);
  288. } else {
  289. sharees.add(
  290. "and " +
  291. remaining.toString() +
  292. " other" +
  293. (remaining > 1 ? "s" : ""),
  294. );
  295. }
  296. break;
  297. }
  298. }
  299. return GestureDetector(
  300. behavior: HitTestBehavior.opaque,
  301. child: Container(
  302. margin: EdgeInsets.fromLTRB(16, 12, 16, 12),
  303. child: Row(
  304. children: <Widget>[
  305. ClipRRect(
  306. borderRadius: BorderRadius.circular(3),
  307. child: SizedBox(
  308. child: Hero(
  309. tag: "outgoing_collection" + c.thumbnail.tag(),
  310. child: ThumbnailWidget(
  311. c.thumbnail,
  312. key: Key("outgoing_collection" + c.thumbnail.tag()),
  313. ),
  314. ),
  315. height: 60,
  316. width: 60,
  317. ),
  318. ),
  319. Padding(padding: EdgeInsets.all(8)),
  320. Expanded(
  321. child: Column(
  322. crossAxisAlignment: CrossAxisAlignment.start,
  323. children: [
  324. Row(
  325. children: [
  326. Text(
  327. c.collection.name,
  328. style: TextStyle(
  329. fontSize: 16,
  330. ),
  331. ),
  332. Padding(padding: EdgeInsets.all(2)),
  333. c.collection.publicURLs.isEmpty
  334. ? Container()
  335. : Icon(Icons.link),
  336. ],
  337. ),
  338. sharees.isEmpty
  339. ? Container()
  340. : Padding(
  341. padding: EdgeInsets.fromLTRB(0, 4, 0, 0),
  342. child: Text(
  343. "shared with " + sharees.join(", "),
  344. style: TextStyle(
  345. fontSize: 14,
  346. color: Theme.of(context).primaryColorLight,
  347. ),
  348. textAlign: TextAlign.left,
  349. overflow: TextOverflow.ellipsis,
  350. ),
  351. ),
  352. ],
  353. ),
  354. ),
  355. ],
  356. ),
  357. ),
  358. onTap: () {
  359. final page = CollectionPage(
  360. c,
  361. appBarType: GalleryType.owned_collection,
  362. tagPrefix: "outgoing_collection",
  363. );
  364. routeToPage(context, page);
  365. },
  366. );
  367. }
  368. }
  369. class IncomingCollectionItem extends StatelessWidget {
  370. final CollectionWithThumbnail c;
  371. const IncomingCollectionItem(
  372. this.c, {
  373. Key key,
  374. }) : super(key: key);
  375. @override
  376. Widget build(BuildContext context) {
  377. const double horizontalPaddingOfGridRow = 16;
  378. const double crossAxisSpacingOfGrid = 9;
  379. TextStyle albumTitleTextStyle =
  380. Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 14);
  381. Size size = MediaQuery.of(context).size;
  382. int albumsCountInOneRow = max(size.width ~/ 220.0, 2);
  383. double totalWhiteSpaceOfRow = (horizontalPaddingOfGridRow * 2) +
  384. (albumsCountInOneRow - 1) * crossAxisSpacingOfGrid;
  385. final double sideOfThumbnail = (size.width / albumsCountInOneRow) -
  386. (totalWhiteSpaceOfRow / albumsCountInOneRow);
  387. return GestureDetector(
  388. child: Column(
  389. crossAxisAlignment: CrossAxisAlignment.start,
  390. children: <Widget>[
  391. ClipRRect(
  392. borderRadius: BorderRadius.circular(4),
  393. child: SizedBox(
  394. child: Stack(
  395. children: [
  396. Hero(
  397. tag: "shared_collection" + c.thumbnail.tag(),
  398. child: ThumbnailWidget(
  399. c.thumbnail,
  400. key: Key("shared_collection" + c.thumbnail.tag()),
  401. ),
  402. ),
  403. Align(
  404. alignment: Alignment.bottomRight,
  405. child: Container(
  406. child: Text(
  407. c.collection.owner.name == null ||
  408. c.collection.owner.name.isEmpty
  409. ? c.collection.owner.email.substring(0, 1)
  410. : c.collection.owner.name.substring(0, 1),
  411. textAlign: TextAlign.center,
  412. ),
  413. padding: EdgeInsets.all(8),
  414. margin: EdgeInsets.fromLTRB(0, 0, 4, 0),
  415. decoration: BoxDecoration(
  416. shape: BoxShape.circle,
  417. color: Theme.of(context).buttonColor,
  418. ),
  419. ),
  420. ),
  421. ],
  422. ),
  423. height: sideOfThumbnail,
  424. width: sideOfThumbnail,
  425. ),
  426. ),
  427. SizedBox(height: 4),
  428. Row(
  429. children: [
  430. Container(
  431. constraints: BoxConstraints(maxWidth: sideOfThumbnail - 40),
  432. child: Text(
  433. c.collection.name,
  434. style: albumTitleTextStyle,
  435. overflow: TextOverflow.ellipsis,
  436. ),
  437. ),
  438. FutureBuilder<int>(
  439. future: FilesDB.instance.collectionFileCount(c.collection.id),
  440. builder: (context, snapshot) {
  441. if (snapshot.hasData && snapshot.data > 0) {
  442. return RichText(
  443. text: TextSpan(
  444. style: albumTitleTextStyle.copyWith(
  445. color: albumTitleTextStyle.color.withOpacity(0.5),
  446. ),
  447. children: [
  448. TextSpan(text: " \u2022 "),
  449. TextSpan(text: snapshot.data.toString()),
  450. ],
  451. ),
  452. );
  453. } else {
  454. return Container();
  455. }
  456. },
  457. ),
  458. ],
  459. ),
  460. ],
  461. ),
  462. onTap: () {
  463. routeToPage(
  464. context,
  465. CollectionPage(
  466. c,
  467. appBarType: GalleryType.shared_collection,
  468. tagPrefix: "shared_collection",
  469. ),
  470. );
  471. },
  472. );
  473. }
  474. }