quid_faciam_hodie/lib/managers/calendar_manager.dart
2022-08-16 16:53:42 +02:00

76 lines
2.0 KiB
Dart

import 'package:share_location/foreign_types/memory.dart';
import 'package:share_location/helpers/iterate_months.dart';
class CalendarManager {
final Map<DateTime, Set<String>> _values;
CalendarManager({
required final List<Memory> memories,
}) : _values = mapFromMemoriesList(memories);
static DateTime createDateKey(final DateTime date) =>
DateTime(date.year, date.month, date.day);
static Map<DateTime, Set<String>> mapFromMemoriesList(
final List<Memory> memories) {
final map = <DateTime, Set<String>>{};
for (final memory in memories) {
final key = createDateKey(memory.creationDate);
if (map.containsKey(key)) {
map[key]!.add(memory.id);
} else {
map[key] = {
memory.id,
};
}
}
return map;
}
static Map<DateTime, Map<DateTime, int>> fillEmptyMonths(
Map<DateTime, Map<DateTime, int>> monthMapping) {
final earliestDate =
monthMapping.keys.reduce((a, b) => a.isBefore(b) ? a : b);
final latestDate = monthMapping.keys.reduce((a, b) => a.isAfter(b) ? a : b);
final filledMonthMapping = <DateTime, Map<DateTime, int>>{};
for (final date in iterateMonths(earliestDate, latestDate)) {
filledMonthMapping[date] = monthMapping[date] ?? {};
}
return filledMonthMapping;
}
Map<DateTime, Map<DateTime, int>> getMonthDayAmountMapping() {
final map = <DateTime, Map<DateTime, int>>{};
for (final entry in _values.entries) {
final date = entry.key;
final monthDate = DateTime(date.year, date.month, 1);
final memoryIDs = entry.value;
if (map.containsKey(monthDate)) {
map[monthDate]![date] = memoryIDs.length;
} else {
map[monthDate] = {
date: memoryIDs.length,
};
}
}
return map;
}
Map<DateTime, Map<DateTime, int>> getMappingForList() {
final monthMapping = fillEmptyMonths(getMonthDayAmountMapping());
return Map.fromEntries(
monthMapping.entries.toList()..sort((a, b) => b.key.compareTo(a.key)),
);
}
}