memories_widget.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import 'package:flutter/material.dart';
  2. import 'package:photos/models/memory.dart';
  3. import 'package:photos/services/memories_service.dart';
  4. import 'package:photos/ui/extents_page_view.dart';
  5. import 'package:photos/ui/file_widget.dart';
  6. import 'package:photos/ui/thumbnail_widget.dart';
  7. import 'package:photos/utils/date_time_util.dart';
  8. import 'package:photos/utils/file_util.dart';
  9. import 'package:photos/utils/navigation_util.dart';
  10. import 'package:photos/utils/share_util.dart';
  11. import 'package:step_progress_indicator/step_progress_indicator.dart';
  12. class MemoriesWidget extends StatelessWidget {
  13. const MemoriesWidget({Key key}) : super(key: key);
  14. @override
  15. Widget build(BuildContext context) {
  16. return FutureBuilder<List<Memory>>(
  17. future: MemoriesService.instance.getMemories(),
  18. builder: (context, snapshot) {
  19. if (snapshot.hasError || !snapshot.hasData || snapshot.data.isEmpty) {
  20. return const SizedBox.shrink();
  21. } else {
  22. return Column(
  23. crossAxisAlignment: CrossAxisAlignment.start,
  24. children: [
  25. _buildMemories(snapshot.data),
  26. Divider(),
  27. ],
  28. );
  29. }
  30. },
  31. );
  32. }
  33. Widget _buildMemories(List<Memory> memories) {
  34. final collatedMemories = _collateMemories(memories);
  35. final List<Widget> memoryWidgets = [];
  36. for (final memories in collatedMemories) {
  37. memoryWidgets.add(MemoryWidget(memories: memories));
  38. }
  39. return SingleChildScrollView(
  40. scrollDirection: Axis.horizontal,
  41. child: Row(children: memoryWidgets),
  42. );
  43. }
  44. List<List<Memory>> _collateMemories(List<Memory> memories) {
  45. final List<Memory> yearlyMemories = [];
  46. final List<List<Memory>> collatedMemories = [];
  47. for (int index = 0; index < memories.length; index++) {
  48. if (index > 0 &&
  49. !_areMemoriesFromSameYear(memories[index - 1], memories[index])) {
  50. final List<Memory> collatedYearlyMemories = [];
  51. collatedYearlyMemories.addAll(yearlyMemories);
  52. collatedMemories.add(collatedYearlyMemories);
  53. yearlyMemories.clear();
  54. }
  55. yearlyMemories.add(memories[index]);
  56. }
  57. if (yearlyMemories.isNotEmpty) {
  58. collatedMemories.add(yearlyMemories);
  59. }
  60. return collatedMemories.reversed.toList();
  61. }
  62. bool _areMemoriesFromSameYear(Memory first, Memory second) {
  63. var firstDate =
  64. DateTime.fromMicrosecondsSinceEpoch(first.file.creationTime);
  65. var secondDate =
  66. DateTime.fromMicrosecondsSinceEpoch(second.file.creationTime);
  67. return firstDate.year == secondDate.year;
  68. }
  69. }
  70. class MemoryWidget extends StatefulWidget {
  71. const MemoryWidget({
  72. Key key,
  73. @required this.memories,
  74. }) : super(key: key);
  75. final List<Memory> memories;
  76. @override
  77. State<MemoryWidget> createState() => _MemoryWidgetState();
  78. }
  79. class _MemoryWidgetState extends State<MemoryWidget> {
  80. @override
  81. Widget build(BuildContext context) {
  82. final index = _getNextMemoryIndex();
  83. final title = _getTitle(widget.memories[index]);
  84. return GestureDetector(
  85. onTap: () async {
  86. await routeToPage(
  87. context,
  88. FullScreenMemory(title, widget.memories, index),
  89. );
  90. setState(() {});
  91. },
  92. child: SizedBox(
  93. width: 92,
  94. height: 100,
  95. child: Padding(
  96. padding: const EdgeInsets.all(8.0),
  97. child: Column(
  98. children: [
  99. _buildMemoryItem(context, index),
  100. Padding(padding: EdgeInsets.all(4)),
  101. Hero(
  102. tag: title,
  103. child: Material(
  104. type: MaterialType.transparency,
  105. child: Text(
  106. title,
  107. style: Theme.of(context)
  108. .textTheme
  109. .subtitle1
  110. .copyWith(fontSize: 12),
  111. textAlign: TextAlign.center,
  112. ),
  113. ),
  114. ),
  115. ],
  116. ),
  117. ),
  118. ),
  119. );
  120. }
  121. Container _buildMemoryItem(BuildContext context, int index) {
  122. final memory = widget.memories[index];
  123. final isSeen = memory.isSeen();
  124. return Container(
  125. decoration: BoxDecoration(
  126. border: isSeen
  127. ? Border()
  128. : Border.all(
  129. color: Theme.of(context).buttonColor,
  130. width: isSeen ? 0 : 2,
  131. ),
  132. borderRadius: BorderRadius.circular(40),
  133. ),
  134. child: ClipOval(
  135. child: SizedBox(
  136. width: isSeen ? 60 : 56,
  137. height: isSeen ? 60 : 56,
  138. child: Hero(
  139. tag: "memories" + memory.file.tag(),
  140. child: ThumbnailWidget(
  141. memory.file,
  142. shouldShowSyncStatus: false,
  143. key: Key("memories" + memory.file.tag()),
  144. ),
  145. ),
  146. ),
  147. ),
  148. );
  149. }
  150. // Returns either the first unseen memory or the memory that succeeds the
  151. // last seen memory
  152. int _getNextMemoryIndex() {
  153. int lastSeenIndex = 0;
  154. int lastSeenTimestamp = 0;
  155. for (var index = 0; index < widget.memories.length; index++) {
  156. final memory = widget.memories[index];
  157. if (!memory.isSeen()) {
  158. return index;
  159. } else {
  160. if (memory.seenTime() > lastSeenTimestamp) {
  161. lastSeenIndex = index;
  162. lastSeenTimestamp = memory.seenTime();
  163. }
  164. }
  165. }
  166. if (lastSeenIndex == widget.memories.length - 1) {
  167. return 0;
  168. }
  169. return lastSeenIndex + 1;
  170. }
  171. String _getTitle(Memory memory) {
  172. final present = DateTime.now();
  173. final then = DateTime.fromMicrosecondsSinceEpoch(memory.file.creationTime);
  174. final diffInYears = present.year - then.year;
  175. if (diffInYears == 1) {
  176. return "1 year ago";
  177. } else {
  178. return diffInYears.toString() + " years ago";
  179. }
  180. }
  181. }
  182. class FullScreenMemory extends StatefulWidget {
  183. final String title;
  184. final List<Memory> memories;
  185. final int index;
  186. FullScreenMemory(this.title, this.memories, this.index, {Key key})
  187. : super(key: key);
  188. @override
  189. _FullScreenMemoryState createState() => _FullScreenMemoryState();
  190. }
  191. class _FullScreenMemoryState extends State<FullScreenMemory> {
  192. int _index = 0;
  193. double _opacity = 1;
  194. // shows memory counter as index+1/totalFiles for large number of memories
  195. // when the top step indicator isn't visible.
  196. bool _showCounter = false;
  197. PageController _pageController;
  198. bool _shouldDisableScroll = false;
  199. final GlobalKey shareButtonKey = GlobalKey();
  200. @override
  201. void initState() {
  202. super.initState();
  203. _index = widget.index;
  204. Future.delayed(Duration(seconds: 3), () {
  205. if (mounted) {
  206. setState(() {
  207. _opacity = 0;
  208. _showCounter = widget.memories.length > 60;
  209. });
  210. }
  211. });
  212. MemoriesService.instance.markMemoryAsSeen(widget.memories[_index]);
  213. }
  214. @override
  215. Widget build(BuildContext context) {
  216. final file = widget.memories[_index].file;
  217. return Scaffold(
  218. appBar: AppBar(
  219. toolbarHeight: 84,
  220. automaticallyImplyLeading: false,
  221. title: Column(
  222. crossAxisAlignment: CrossAxisAlignment.start,
  223. children: [
  224. StepProgressIndicator(
  225. totalSteps: widget.memories.length,
  226. currentStep: _index + 1,
  227. size: 2,
  228. selectedColor: Colors.white, //same for both themes
  229. unselectedColor: Colors.white.withOpacity(0.4),
  230. ),
  231. SizedBox(
  232. height: 18,
  233. ),
  234. Row(
  235. children: [
  236. Padding(
  237. padding: const EdgeInsets.only(right: 16),
  238. child: InkWell(
  239. onTap: () {
  240. Navigator.pop(context);
  241. },
  242. child: Icon(
  243. Icons.close,
  244. color: Colors.white, //same for both themes
  245. ),
  246. ),
  247. ),
  248. Text(
  249. getFormattedDate(
  250. DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  251. ),
  252. style: Theme.of(context).textTheme.subtitle1.copyWith(
  253. fontSize: 14,
  254. color: Colors.white,
  255. ), //same for both themes
  256. ),
  257. ],
  258. ),
  259. ],
  260. ),
  261. flexibleSpace: Container(
  262. decoration: BoxDecoration(
  263. gradient: LinearGradient(
  264. begin: Alignment.topCenter,
  265. end: Alignment.bottomCenter,
  266. colors: [
  267. Colors.black.withOpacity(0.6),
  268. Colors.black.withOpacity(0.5),
  269. Colors.transparent,
  270. ],
  271. stops: const [0, 0.6, 1],
  272. ),
  273. ),
  274. ),
  275. backgroundColor: Color(0x00000000),
  276. elevation: 0,
  277. ),
  278. extendBodyBehindAppBar: true,
  279. body: Container(
  280. color: Colors.black,
  281. child: Stack(
  282. alignment: Alignment.bottomCenter,
  283. children: [
  284. _buildSwiper(),
  285. bottomGradient(),
  286. _buildInfoText(),
  287. _buildBottomIcons(),
  288. ],
  289. ),
  290. ),
  291. );
  292. }
  293. Hero _buildInfoText() {
  294. return Hero(
  295. tag: widget.title,
  296. child: Container(
  297. alignment: Alignment.bottomCenter,
  298. padding: EdgeInsets.fromLTRB(0, 0, 0, 28),
  299. child: _showCounter
  300. ? Text(
  301. '${_index + 1}/${widget.memories.length}',
  302. style: Theme.of(context)
  303. .textTheme
  304. .bodyText1
  305. .copyWith(color: Colors.white.withOpacity(0.4)),
  306. )
  307. : AnimatedOpacity(
  308. opacity: _opacity,
  309. duration: Duration(milliseconds: 500),
  310. child: Text(
  311. widget.title,
  312. style: Theme.of(context)
  313. .textTheme
  314. .headline4
  315. .copyWith(color: Colors.white),
  316. ),
  317. ),
  318. ),
  319. );
  320. }
  321. Widget _buildBottomIcons() {
  322. final file = widget.memories[_index].file;
  323. return Container(
  324. alignment: Alignment.bottomRight,
  325. padding: EdgeInsets.fromLTRB(0, 0, 26, 20),
  326. child: IconButton(
  327. icon: Icon(
  328. Icons.adaptive.share,
  329. color: Colors.white, //same for both themes
  330. ),
  331. onPressed: () {
  332. share(context, [file]);
  333. },
  334. ),
  335. );
  336. }
  337. Widget bottomGradient() {
  338. return Container(
  339. height: 124,
  340. width: double.infinity,
  341. decoration: BoxDecoration(
  342. gradient: LinearGradient(
  343. begin: Alignment.bottomCenter,
  344. end: Alignment.topCenter,
  345. colors: [
  346. Colors.black.withOpacity(0.5), //same for both themes
  347. Colors.transparent,
  348. ],
  349. stops: const [0, 0.8],
  350. ),
  351. ),
  352. );
  353. }
  354. Widget _buildSwiper() {
  355. _pageController = PageController(initialPage: _index);
  356. return ExtentsPageView.extents(
  357. itemBuilder: (BuildContext context, int index) {
  358. if (index < widget.memories.length - 1) {
  359. final nextFile = widget.memories[index + 1].file;
  360. preloadThumbnail(nextFile);
  361. preloadFile(nextFile);
  362. }
  363. final file = widget.memories[index].file;
  364. return FileWidget(
  365. file,
  366. autoPlay: false,
  367. tagPrefix: "memories",
  368. shouldDisableScroll: (value) {
  369. setState(() {
  370. _shouldDisableScroll = value;
  371. });
  372. },
  373. backgroundDecoration: BoxDecoration(
  374. color: Colors.transparent,
  375. ),
  376. );
  377. },
  378. itemCount: widget.memories.length,
  379. controller: _pageController,
  380. extents: 1,
  381. onPageChanged: (index) async {
  382. await MemoriesService.instance.markMemoryAsSeen(widget.memories[index]);
  383. setState(() {
  384. _index = index;
  385. });
  386. },
  387. physics: _shouldDisableScroll
  388. ? NeverScrollableScrollPhysics()
  389. : PageScrollPhysics(),
  390. );
  391. }
  392. }