memories_widget.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. PageController _pageController;
  195. bool _shouldDisableScroll = false;
  196. final GlobalKey shareButtonKey = GlobalKey();
  197. @override
  198. void initState() {
  199. super.initState();
  200. _index = widget.index;
  201. Future.delayed(Duration(seconds: 3), () {
  202. if (mounted) {
  203. setState(() {
  204. _opacity = 0;
  205. });
  206. }
  207. });
  208. MemoriesService.instance.markMemoryAsSeen(widget.memories[_index]);
  209. }
  210. @override
  211. Widget build(BuildContext context) {
  212. final file = widget.memories[_index].file;
  213. return Scaffold(
  214. appBar: AppBar(
  215. toolbarHeight: 84,
  216. automaticallyImplyLeading: false,
  217. title: Column(
  218. crossAxisAlignment: CrossAxisAlignment.start,
  219. children: [
  220. StepProgressIndicator(
  221. totalSteps: widget.memories.length,
  222. currentStep: _index + 1,
  223. size: 2,
  224. selectedColor: Colors.white, //same for both themes
  225. unselectedColor: Colors.white.withOpacity(0.4),
  226. ),
  227. SizedBox(
  228. height: 18,
  229. ),
  230. Row(
  231. children: [
  232. Padding(
  233. padding: const EdgeInsets.only(right: 16),
  234. child: InkWell(
  235. onTap: () {
  236. Navigator.pop(context);
  237. },
  238. child: Icon(
  239. Icons.close,
  240. color: Colors.white, //same for both themes
  241. ),
  242. ),
  243. ),
  244. Text(
  245. getFormattedDate(
  246. DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  247. ),
  248. style: Theme.of(context).textTheme.subtitle1.copyWith(
  249. fontSize: 14,
  250. color: Colors.white,
  251. ), //same for both themes
  252. ),
  253. ],
  254. ),
  255. ],
  256. ),
  257. flexibleSpace: Container(
  258. decoration: BoxDecoration(
  259. gradient: LinearGradient(
  260. begin: Alignment.topCenter,
  261. end: Alignment.bottomCenter,
  262. colors: [
  263. Colors.black.withOpacity(0.6),
  264. Colors.black.withOpacity(0.5),
  265. Colors.transparent,
  266. ],
  267. stops: const [0, 0.6, 1],
  268. ),
  269. ),
  270. ),
  271. backgroundColor: Color(0x00000000),
  272. elevation: 0,
  273. ),
  274. extendBodyBehindAppBar: true,
  275. body: Container(
  276. color: Colors.black,
  277. child: Stack(
  278. alignment: Alignment.bottomCenter,
  279. children: [
  280. _buildSwiper(),
  281. bottomGradient(),
  282. _buildTitleText(),
  283. _buildBottomIcons(),
  284. ],
  285. ),
  286. ),
  287. );
  288. }
  289. Hero _buildTitleText() {
  290. return Hero(
  291. tag: widget.title,
  292. child: Container(
  293. alignment: Alignment.bottomCenter,
  294. padding: EdgeInsets.fromLTRB(0, 0, 0, 28),
  295. child: AnimatedOpacity(
  296. opacity: _opacity,
  297. duration: Duration(milliseconds: 500),
  298. child: Text(
  299. widget.title,
  300. style: Theme.of(context)
  301. .textTheme
  302. .headline4
  303. .copyWith(color: Colors.white),
  304. ),
  305. ),
  306. ),
  307. );
  308. }
  309. Widget _buildBottomIcons() {
  310. final file = widget.memories[_index].file;
  311. return Container(
  312. alignment: Alignment.bottomRight,
  313. padding: EdgeInsets.fromLTRB(0, 0, 26, 20),
  314. child: IconButton(
  315. icon: Icon(
  316. Icons.adaptive.share,
  317. color: Colors.white, //same for both themes
  318. ),
  319. onPressed: () {
  320. share(context, [file]);
  321. },
  322. ),
  323. );
  324. }
  325. Widget bottomGradient() {
  326. return Container(
  327. height: 124,
  328. width: double.infinity,
  329. decoration: BoxDecoration(
  330. gradient: LinearGradient(
  331. begin: Alignment.bottomCenter,
  332. end: Alignment.topCenter,
  333. colors: [
  334. Colors.black.withOpacity(0.5), //same for both themes
  335. Colors.transparent,
  336. ],
  337. stops: const [0, 0.8],
  338. ),
  339. ),
  340. );
  341. }
  342. Widget _buildSwiper() {
  343. _pageController = PageController(initialPage: _index);
  344. return ExtentsPageView.extents(
  345. itemBuilder: (BuildContext context, int index) {
  346. if (index < widget.memories.length - 1) {
  347. final nextFile = widget.memories[index + 1].file;
  348. preloadThumbnail(nextFile);
  349. preloadFile(nextFile);
  350. }
  351. final file = widget.memories[index].file;
  352. return FileWidget(
  353. file,
  354. autoPlay: false,
  355. tagPrefix: "memories",
  356. shouldDisableScroll: (value) {
  357. setState(() {
  358. _shouldDisableScroll = value;
  359. });
  360. },
  361. backgroundDecoration: BoxDecoration(
  362. color: Colors.transparent,
  363. ),
  364. );
  365. },
  366. itemCount: widget.memories.length,
  367. controller: _pageController,
  368. extents: 1,
  369. onPageChanged: (index) async {
  370. await MemoriesService.instance.markMemoryAsSeen(widget.memories[index]);
  371. setState(() {
  372. _index = index;
  373. });
  374. },
  375. physics: _shouldDisableScroll
  376. ? NeverScrollableScrollPhysics()
  377. : PageScrollPhysics(),
  378. );
  379. }
  380. }