Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

feat: adjust wrapped heatmap layout

laurenspriem497d86f95416
diff --git a/mobile/apps/photos/lib/services/wrapped/builders/stats_candidate_builder.dart b/mobile/apps/photos/lib/services/wrapped/builders/stats_candidate_builder.dart
index 5d25078987..edf35849aa 100644
--- a/mobile/apps/photos/lib/services/wrapped/builders/stats_candidate_builder.dart
+++ b/mobile/apps/photos/lib/services/wrapped/builders/stats_candidate_builder.dart
@@ -256,9 +256,9 @@ class StatsCandidateBuilder extends WrappedCandidateBuilder {
return const <WrappedCard>[];
}
- final List<Map<String, Object?>> quarterBlocks =
- _QuarterHeatmapData.fromSnapshot(snapshot);
- if (quarterBlocks.isEmpty) {
+ final _HeatmapGridData? heatmapData =
+ _HeatmapGridData.fromSnapshot(snapshot);
+ if (heatmapData == null || heatmapData.grid.isEmpty) {
return const <WrappedCard>[];
}
@@ -313,7 +313,8 @@ class StatsCandidateBuilder extends WrappedCandidateBuilder {
]);
final Map<String, Object?> meta = <String, Object?>{
"weekdayLabels": snapshot.heatmapWeekdayLabels,
- "quarters": quarterBlocks,
+ "grid": heatmapData.grid,
+ "weekLabels": heatmapData.weekLabels,
"maxCount": snapshot.heatmapMaxCount,
"detailChips": detailChips,
};
@@ -772,10 +773,18 @@ class _DayAggregate {
final List<int> uploadedFileIDs = <int>[];
}
-class _QuarterHeatmapData {
- static List<Map<String, Object?>> fromSnapshot(_StatsSnapshot snapshot) {
+class _HeatmapGridData {
+ const _HeatmapGridData({
+ required this.grid,
+ required this.weekLabels,
+ });
+
+ final List<List<int>> grid;
+ final List<String> weekLabels;
+
+ static _HeatmapGridData? fromSnapshot(_StatsSnapshot snapshot) {
if (snapshot.heatmapRows.isEmpty) {
- return const <Map<String, Object?>>[];
+ return null;
}
final DateFormat monthFormat = DateFormat("MMM");
final DateTime lastVisibleDay = _atLocalMidnight(
@@ -783,25 +792,22 @@ class _QuarterHeatmapData {
? snapshot.periodEndDay
: snapshot.heatmapEnd,
);
- final List<List<List<int>>> quarterWeeks =
- List<List<List<int>>>.generate(4, (_) => <List<int>>[]);
- final List<List<DateTime>> quarterWeekDates =
- List<List<DateTime>>.generate(4, (_) => <DateTime>[]);
-
- for (int index = 0;
- index < snapshot.heatmapRows.length &&
- index < snapshot.heatmapWeekStartDates.length;
- index += 1) {
+ final DateTime yearStart = DateTime(snapshot.year, 1, 1);
+ final int cappedLength = math.min(
+ snapshot.heatmapRows.length,
+ snapshot.heatmapWeekStartDates.length,
+ );
+ final List<List<int>> sanitizedRows = <List<int>>[];
+ final List<DateTime?> rowLastValidDays = <DateTime?>[];
+ for (int index = 0; index < cappedLength; index += 1) {
final DateTime weekStart =
_atLocalMidnight(snapshot.heatmapWeekStartDates[index]);
- final int quarterIndex = ((weekStart.month - 1) ~/ 3).clamp(0, 3);
final List<int> counts = snapshot.heatmapRows[index];
final List<int> sanitized = List<int>.filled(7, 0);
- final DateTime yearStart = DateTime(snapshot.year, 1, 1);
+ DateTime? lastValidDay;
for (int dayOffset = 0; dayOffset < 7; dayOffset += 1) {
- final DateTime day = _atLocalMidnight(
- weekStart.add(Duration(days: dayOffset)),
- );
+ final DateTime day =
+ _atLocalMidnight(weekStart.add(Duration(days: dayOffset)));
if (day.isAfter(lastVisibleDay)) {
sanitized[dayOffset] = kWrappedHeatmapFutureValue;
} else if (day.isBefore(yearStart)) {
@@ -809,88 +815,52 @@ class _QuarterHeatmapData {
} else {
sanitized[dayOffset] =
dayOffset < counts.length ? counts[dayOffset] : 0;
+ lastValidDay = day;
}
}
- quarterWeeks[quarterIndex].add(sanitized);
- quarterWeekDates[quarterIndex].add(weekStart);
+ sanitizedRows.add(List<int>.unmodifiable(sanitized));
+ rowLastValidDays.add(lastValidDay);
}
-
- final List<Map<String, Object?>> blocks = <Map<String, Object?>>[];
- for (int quarter = 0; quarter < quarterWeeks.length; quarter += 1) {
- final List<List<int>> weeks = quarterWeeks[quarter];
- if (weeks.isEmpty) {
- continue;
+ if (sanitizedRows.isEmpty) {
+ return null;
+ }
+ int lastValidIndex = sanitizedRows.length - 1;
+ while (lastValidIndex >= 0) {
+ final List<int> row = sanitizedRows[lastValidIndex];
+ final bool hasCurrentYearValue = row.any((int value) => value >= 0);
+ if (hasCurrentYearValue) {
+ break;
}
- final List<List<int>> grid = _transposeWeeks(weeks);
- final List<String> columnLabels =
- _buildColumnLabels(quarterWeekDates[quarter], monthFormat);
- blocks.add(<String, Object?>{
- "label": _quarterLabel(quarter),
- "grid": grid
- .map((List<int> row) => List<int>.unmodifiable(row))
- .toList(growable: false),
- "columnLabels": columnLabels,
- });
+ lastValidIndex -= 1;
}
- return blocks;
- }
-
- static List<List<int>> _transposeWeeks(List<List<int>> weeks) {
- if (weeks.isEmpty) {
- return List<List<int>>.generate(
- 7,
- (_) => const <int>[],
- growable: false,
- );
+ if (lastValidIndex < 0) {
+ return null;
}
- final int columnCount = weeks.length;
- final int rowCount = weeks.first.length;
- return List<List<int>>.generate(
- rowCount,
- (int dayIndex) => List<int>.generate(
- columnCount,
- (int columnIndex) => weeks[columnIndex][dayIndex],
- growable: false,
- ),
+ final List<List<int>> trimmedRows =
+ sanitizedRows.sublist(0, lastValidIndex + 1);
+ final List<DateTime?> trimmedLastValidDays =
+ rowLastValidDays.sublist(0, lastValidIndex + 1);
+ int? lastLabeledMonth;
+ final List<String> weekLabels = List<String>.generate(
+ trimmedRows.length,
+ (int index) {
+ final DateTime? labelDay = trimmedLastValidDays[index];
+ if (labelDay == null) {
+ return "";
+ }
+ final int month = labelDay.month;
+ if (lastLabeledMonth == null || month != lastLabeledMonth) {
+ lastLabeledMonth = month;
+ return monthFormat.format(labelDay);
+ }
+ return "";
+ },
growable: false,
);
- }
-
- static List<String> _buildColumnLabels(
- List<DateTime> weekStarts,
- DateFormat monthFormat,
- ) {
- final List<String> labels = <String>[];
- int? lastMonth;
- for (final DateTime start in weekStarts) {
- final int month = start.month;
- final bool isNewMonth = labels.isEmpty || month != lastMonth;
- final bool isQuarterStart = start.day <= 7;
- if (isNewMonth && isQuarterStart) {
- final String monthLabel = monthFormat.format(start);
- final String singleChar =
- monthLabel.isEmpty ? "" : monthLabel.substring(0, 1);
- labels.add(singleChar.toUpperCase());
- lastMonth = month;
- } else {
- labels.add("");
- }
- }
- return List<String>.unmodifiable(labels);
- }
-
- static String _quarterLabel(int index) {
- switch (index) {
- case 0:
- return "Jan – Mar";
- case 1:
- return "Apr – Jun";
- case 2:
- return "Jul – Sep";
- case 3:
- default:
- return "Oct – Dec";
- }
+ return _HeatmapGridData(
+ grid: List<List<int>>.unmodifiable(trimmedRows),
+ weekLabels: List<String>.unmodifiable(weekLabels),
+ );
}
}
diff --git a/mobile/apps/photos/lib/ui/wrapped/cards/stats_card_content.dart b/mobile/apps/photos/lib/ui/wrapped/cards/stats_card_content.dart
index 343783138d..d27f22bdb1 100644
--- a/mobile/apps/photos/lib/ui/wrapped/cards/stats_card_content.dart
+++ b/mobile/apps/photos/lib/ui/wrapped/cards/stats_card_content.dart
@@ -994,20 +994,13 @@ class _HeatmapCardContent extends StatelessWidget {
@override
Widget build(BuildContext context) {
final List<String> chips = _stringListFromMeta(card.meta, "detailChips");
- final List<dynamic> rawQuarters =
- card.meta["quarters"] as List<dynamic>? ?? const <dynamic>[];
- final List<_QuarterBlock> quarters = rawQuarters
- .map(
- (dynamic entry) => _QuarterBlock.fromJson(
- (entry as Map).cast<String, Object?>(),
- ),
- )
- .where((_QuarterBlock block) => block.grid.isNotEmpty)
- .toList(growable: false);
+ final List<List<int>> grid = _parseHeatmapGrid(card.meta["grid"]);
final List<String> weekdayLabels =
(card.meta["weekdayLabels"] as List<dynamic>? ?? const <dynamic>[])
.whereType<String>()
.toList(growable: false);
+ final List<String> weekLabels =
+ _stringListFromMeta(card.meta, "weekLabels");
final int maxCount = (card.meta["maxCount"] as num?)?.toInt() ?? 0;
return Column(
@@ -1023,83 +1016,23 @@ class _HeatmapCardContent extends StatelessWidget {
textTheme.bodyMuted,
padding: const EdgeInsets.only(top: 12),
),
- const SizedBox(height: 18),
- if (quarters.isEmpty)
+ const SizedBox(height: 16),
+ if (grid.isEmpty)
_MediaPlaceholder(
height: 180,
colorScheme: colorScheme,
)
else
- LayoutBuilder(
- builder: (BuildContext context, BoxConstraints constraints) {
- final double maxWidth = constraints.maxWidth;
- final int columns =
- quarters.length <= 1 ? 1 : (maxWidth >= 280 ? 2 : 1);
- final double spacing = columns > 1 ? 18 : 0;
- final double itemWidth = columns > 1
- ? (maxWidth - ((columns - 1) * spacing)) / columns
- : maxWidth;
- final int maxColumnCount = quarters
- .map(
- (_QuarterBlock block) =>
- block.grid.isNotEmpty ? block.grid.first.length : 0,
- )
- .fold(0, math.max);
- const double labelColumnWidth = 20;
- const double cellSpacing = 1.5;
- const double fallbackCellSize = 6;
- const double maxCellWidth = 18;
- const double maxCellHeight = 12;
-
- double computeCellWidth(double availableWidth) {
- if (maxColumnCount <= 0) {
- return fallbackCellSize;
- }
- final double usable = math.max(
- 0,
- availableWidth -
- cellSpacing * math.max(0, maxColumnCount - 1),
- );
- final double raw = usable / math.max(1, maxColumnCount);
- return raw.clamp(fallbackCellSize, maxCellWidth);
- }
-
- final double cellWidthWithLabels =
- computeCellWidth(itemWidth - labelColumnWidth);
- final double cellWidthWithoutLabels = computeCellWidth(itemWidth);
- final double cellWidth =
- math.min(cellWidthWithLabels, cellWidthWithoutLabels);
- final double cellHeight = math.min(
- maxCellHeight,
- cellWidth.clamp(fallbackCellSize, maxCellHeight),
- );
- return Wrap(
- spacing: spacing,
- runSpacing: 18,
- children: <Widget>[
- for (final (int index, _QuarterBlock block)
- in quarters.indexed)
- SizedBox(
- width: itemWidth,
- child: _QuarterHeatmap(
- block: block,
- weekdayLabels: weekdayLabels,
- maxCount: maxCount,
- colorScheme: colorScheme,
- textTheme: textTheme,
- showDayLabels: columns == 1 || index % columns == 0,
- cellWidth: cellWidth,
- cellHeight: cellHeight,
- cellSpacing: cellSpacing,
- labelColumnWidth: labelColumnWidth,
- ),
- ),
- ],
- );
- },
+ _YearHeatmap(
+ grid: grid,
+ weekLabels: weekLabels,
+ weekdayLabels: weekdayLabels,
+ maxCount: maxCount,
+ colorScheme: colorScheme,
+ textTheme: textTheme,
),
if (chips.isNotEmpty) ...[
- const SizedBox(height: 20),
+ const SizedBox(height: 16),
_DetailChips(
chips: chips,
colorScheme: colorScheme,
@@ -1112,158 +1045,170 @@ class _HeatmapCardContent extends StatelessWidget {
}
}
-class _QuarterBlock {
- _QuarterBlock({
- required this.label,
- required this.grid,
- required this.columnLabels,
- });
-
- final String label;
- final List<List<int>> grid;
- final List<String> columnLabels;
-
- static _QuarterBlock fromJson(Map<String, Object?> json) {
- final List<List<int>> grid =
- (json["grid"] as List<dynamic>? ?? const <dynamic>[])
- .map(
- (dynamic row) => (row as List<dynamic>)
- .map((dynamic value) => (value as num).toInt())
- .toList(growable: false),
- )
- .toList(growable: false);
- final List<String> columnLabels =
- (json["columnLabels"] as List<dynamic>? ?? const <dynamic>[])
- .whereType<String>()
- .toList(growable: false);
- return _QuarterBlock(
- label: json["label"] as String? ?? "",
- grid: grid,
- columnLabels: columnLabels,
- );
+List<List<int>> _parseHeatmapGrid(Object? raw) {
+ if (raw is! List) {
+ return const <List<int>>[];
+ }
+ final List<List<int>> rows = <List<int>>[];
+ for (final dynamic entry in raw) {
+ if (entry is! List) {
+ continue;
+ }
+ final List<int> parsedRow = <int>[];
+ for (final dynamic value in entry) {
+ if (value is num) {
+ parsedRow.add(value.toInt());
+ }
+ }
+ rows.add(List<int>.unmodifiable(parsedRow));
}
+ return List<List<int>>.unmodifiable(rows);
}
-class _QuarterHeatmap extends StatelessWidget {
- const _QuarterHeatmap({
- required this.block,
+class _YearHeatmap extends StatelessWidget {
+ const _YearHeatmap({
+ required this.grid,
+ required this.weekLabels,
required this.weekdayLabels,
required this.maxCount,
required this.colorScheme,
required this.textTheme,
- required this.showDayLabels,
- required this.cellWidth,
- required this.cellHeight,
- required this.cellSpacing,
- required this.labelColumnWidth,
});
- final _QuarterBlock block;
+ final List<List<int>> grid;
+ final List<String> weekLabels;
final List<String> weekdayLabels;
final int maxCount;
final EnteColorScheme colorScheme;
final EnteTextTheme textTheme;
- final bool showDayLabels;
- final double cellWidth;
- final double cellHeight;
- final double cellSpacing;
- final double labelColumnWidth;
@override
Widget build(BuildContext context) {
- final int columnCount = block.grid.isEmpty ? 0 : block.grid.first.length;
+ final int columnCount = grid.isNotEmpty ? grid.first.length : 0;
if (columnCount == 0) {
return _MediaPlaceholder(
- height: 120,
+ height: 180,
colorScheme: colorScheme,
);
}
- final TextStyle axisStyle =
- textTheme.tinyMuted.copyWith(fontSize: 8.5, height: 1.15);
- final List<String> headerLabels = List<String>.generate(
- columnCount,
- (int index) =>
- index < block.columnLabels.length ? block.columnLabels[index] : "",
- );
-
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- block.label,
- style: textTheme.smallBold,
- ),
- const SizedBox(height: 6),
- Row(
- children: [
- SizedBox(width: labelColumnWidth),
- for (int columnIndex = 0;
- columnIndex < headerLabels.length;
- columnIndex += 1) ...[
- SizedBox(
- width: cellWidth,
- child: Text(
- headerLabels[columnIndex],
- style: axisStyle,
- textAlign: TextAlign.center,
- softWrap: false,
- overflow: TextOverflow.fade,
- ),
- ),
- SizedBox(
- width: columnIndex == headerLabels.length - 1 ? 0 : cellSpacing,
- ),
- ],
- ],
- ),
- const SizedBox(height: 4),
- Column(
- children: List<Widget>.generate(weekdayLabels.length, (int dayIndex) {
- final List<int> row = block.grid.length > dayIndex
- ? block.grid[dayIndex]
- : const <int>[];
- final String dayLabel =
- dayIndex < weekdayLabels.length ? weekdayLabels[dayIndex] : "";
- return Padding(
- padding: EdgeInsets.only(
- bottom: dayIndex == weekdayLabels.length - 1 ? 0 : cellSpacing,
- ),
- child: Row(
- children: [
- SizedBox(
- width: labelColumnWidth,
- child: Visibility(
- visible: showDayLabels,
- maintainSize: true,
- maintainAnimation: true,
- maintainState: true,
- child: Text(
- dayLabel,
- style: axisStyle,
+ return LayoutBuilder(
+ builder: (BuildContext context, BoxConstraints constraints) {
+ const double labelColumnWidth = 22;
+ const double horizontalSpacing = 0.8;
+ const double verticalSpacing = 0.6;
+ const double minCellSize = 4;
+ const double maxCellWidth = 11;
+ const double maxCellHeight = 7.0;
+ const double heightCompression = 0.55;
+
+ final double usableWidth = math.max(
+ 0,
+ constraints.maxWidth -
+ labelColumnWidth -
+ horizontalSpacing * math.max(0, columnCount - 1),
+ );
+ final double rawCellWidth =
+ columnCount > 0 ? usableWidth / columnCount : minCellSize;
+ final double cellWidth =
+ rawCellWidth.clamp(minCellSize, maxCellWidth.toDouble()).toDouble();
+ final double compressedHeight = cellWidth * heightCompression;
+ final double cellHeight =
+ compressedHeight.clamp(minCellSize, maxCellHeight).toDouble();
+ final double gridWidth = labelColumnWidth +
+ (cellWidth * columnCount) +
+ horizontalSpacing * math.max(0, columnCount - 1);
+ final TextStyle axisStyle =
+ textTheme.tinyMuted.copyWith(fontSize: 7.5, height: 1.05);
+
+ return Align(
+ alignment: Alignment.topCenter,
+ child: SizedBox(
+ width: gridWidth,
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Row(
+ children: [
+ const SizedBox(width: labelColumnWidth),
+ for (int columnIndex = 0;
+ columnIndex < columnCount;
+ columnIndex += 1) ...[
+ SizedBox(
+ width: cellWidth,
+ child: Text(
+ columnIndex < weekdayLabels.length
+ ? weekdayLabels[columnIndex]
+ : "",
+ style: axisStyle,
+ textAlign: TextAlign.center,
+ softWrap: false,
+ overflow: TextOverflow.fade,
+ ),
),
- ),
- ),
- for (int columnIndex = 0;
- columnIndex < columnCount;
- columnIndex += 1) ...[
- _HeatmapCell(
- value: columnIndex < row.length ? row[columnIndex] : -1,
- width: cellWidth,
- height: cellHeight,
- colorScheme: colorScheme,
- maxCount: maxCount,
- ),
- SizedBox(
- width: columnIndex == columnCount - 1 ? 0 : cellSpacing,
- ),
+ SizedBox(
+ width: columnIndex == columnCount - 1
+ ? 0
+ : horizontalSpacing,
+ ),
+ ],
],
- ],
- ),
- );
- }),
- ),
- ],
+ ),
+ const SizedBox(height: 4),
+ Column(
+ children: List<Widget>.generate(grid.length, (int rowIndex) {
+ final List<int> row = grid[rowIndex];
+ final String label = rowIndex < weekLabels.length
+ ? weekLabels[rowIndex]
+ : "";
+ return Padding(
+ padding: EdgeInsets.only(
+ bottom:
+ rowIndex == grid.length - 1 ? 0 : verticalSpacing,
+ ),
+ child: Row(
+ children: [
+ SizedBox(
+ width: labelColumnWidth,
+ child: Visibility(
+ visible: label.isNotEmpty,
+ maintainSize: true,
+ maintainAnimation: true,
+ maintainState: true,
+ child: Text(
+ label,
+ style: axisStyle,
+ ),
+ ),
+ ),
+ for (int columnIndex = 0;
+ columnIndex < columnCount;
+ columnIndex += 1) ...[
+ _HeatmapCell(
+ value: columnIndex < row.length
+ ? row[columnIndex]
+ : -1,
+ width: cellWidth,
+ height: cellHeight,
+ colorScheme: colorScheme,
+ maxCount: maxCount,
+ ),
+ SizedBox(
+ width: columnIndex == columnCount - 1
+ ? 0
+ : horizontalSpacing,
+ ),
+ ],
+ ],
+ ),
+ );
+ }),
+ ),
+ ],
+ ),
+ ),
+ );
+ },
);
}
}