From 51372d7b26fd03980aee607586bfeffbc42b3a00 Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Fri, 2 Aug 2019 00:16:50 +0900 Subject: [PATCH] album: group by month --- lib/model/image_entry.dart | 4 ++-- lib/thumbnail_collection.dart | 26 ++++++++++++++++++++++++-- lib/utils/date_utils.dart | 2 ++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/model/image_entry.dart b/lib/model/image_entry.dart index c57ad1311..dc4bc823b 100644 --- a/lib/model/image_entry.dart +++ b/lib/model/image_entry.dart @@ -79,8 +79,8 @@ class ImageEntry { return null; } - DateTime getDayTaken() { + DateTime getMonthTaken() { final d = getBestDate(); - return d == null ? null : DateTime(d.year, d.month, d.day); + return d == null ? null : DateTime(d.year, d.month); } } diff --git a/lib/thumbnail_collection.dart b/lib/thumbnail_collection.dart index 27e0d9779..2628d0751 100644 --- a/lib/thumbnail_collection.dart +++ b/lib/thumbnail_collection.dart @@ -16,7 +16,7 @@ class ThumbnailCollection extends StatelessWidget { final ScrollController scrollController = ScrollController(); ThumbnailCollection({Key key, this.entries, this.done}) - : sections = groupBy(entries, (entry) => entry.getDayTaken()), + : sections = groupBy(entries, (entry) => entry.getMonthTaken()), super(key: key); @override @@ -85,7 +85,7 @@ class SectionSliver extends StatelessWidget { // debugPrint('$runtimeType build with sectionKey=$sectionKey'); final columnCount = 4; return SliverStickyHeader( - header: DaySectionHeader(date: sectionKey), + header: MonthSectionHeader(date: sectionKey), sliver: SliverGrid( delegate: SliverChildBuilderDelegate( (sliverContext, index) { @@ -146,6 +146,28 @@ class DaySectionHeader extends StatelessWidget { } } +class MonthSectionHeader extends StatelessWidget { + final String text; + + MonthSectionHeader({Key key, DateTime date}) + : text = formatDate(date), + super(key: key); + + static DateFormat m = DateFormat.MMMM(); + static DateFormat ym = DateFormat.yMMMM(); + + static formatDate(DateTime date) { + if (isThisMonth(date)) return 'This month'; + if (isThisYear(date)) return m.format(date); + return ym.format(date); + } + + @override + Widget build(BuildContext context) { + return SectionHeader(text: text); + } +} + class SectionHeader extends StatelessWidget { final String text; diff --git a/lib/utils/date_utils.dart b/lib/utils/date_utils.dart index 7bae16b04..71ec3849d 100644 --- a/lib/utils/date_utils.dart +++ b/lib/utils/date_utils.dart @@ -6,4 +6,6 @@ bool isAtSameDayAs(DateTime d1, DateTime d2) => isAtSameMonthAs(d1, d2) && d1.da bool isToday(DateTime d) => isAtSameDayAs(d, DateTime.now()); +bool isThisMonth(DateTime d) => isAtSameMonthAs(d, DateTime.now()); + bool isThisYear(DateTime d) => isAtSameYearAs(d, DateTime.now());