ente/lib/services/memories_service.dart

92 lines
3.2 KiB
Dart
Raw Normal View History

2020-07-21 22:01:44 +00:00
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/db/memories_db.dart';
2020-07-21 08:06:15 +00:00
import 'package:photos/models/filters/important_items_filter.dart';
import 'package:photos/models/memory.dart';
2020-07-21 22:01:44 +00:00
class MemoriesService extends ChangeNotifier {
final _logger = Logger("MemoryService");
final _memoriesDB = MemoriesDB.instance;
final _filesDB = FilesDB.instance;
static final microSecondsInADay = 86400000000;
static final daysInAYear = 365;
2020-07-21 20:30:15 +00:00
static final yearsBefore = 30;
static final daysBefore = 7;
static final daysAfter = 1;
2021-02-14 09:42:02 +00:00
List<Memory> _cachedMemories;
2020-07-21 12:19:55 +00:00
MemoriesService._privateConstructor();
2020-07-21 12:19:55 +00:00
static final MemoriesService instance = MemoriesService._privateConstructor();
2020-07-20 12:58:20 +00:00
Future<void> init() async {
2021-02-14 09:42:02 +00:00
addListener(() {
_cachedMemories = null;
});
await _memoriesDB.clearMemoriesSeenBeforeTime(
DateTime.now().microsecondsSinceEpoch - (7 * microSecondsInADay));
}
2021-03-17 21:11:31 +00:00
void clearCache() {
_cachedMemories = null;
}
Future<List<Memory>> getMemories() async {
2021-02-14 09:42:02 +00:00
if (_cachedMemories != null) {
2021-02-05 17:01:55 +00:00
return _cachedMemories;
}
final presentTime = DateTime.now();
final present = presentTime.subtract(Duration(
hours: presentTime.hour,
minutes: presentTime.minute,
seconds: presentTime.second));
2021-04-27 16:00:29 +00:00
final List<List<int>> durations = [];
2020-07-21 20:30:15 +00:00
for (var yearAgo = 1; yearAgo <= yearsBefore; yearAgo++) {
2020-07-21 08:06:15 +00:00
final date = _getDate(present, yearAgo);
final startCreationTime =
2020-07-21 20:30:15 +00:00
date.subtract(Duration(days: daysBefore)).microsecondsSinceEpoch;
final endCreationTime =
2020-07-21 20:30:15 +00:00
date.add(Duration(days: daysAfter)).microsecondsSinceEpoch;
2021-04-27 16:00:29 +00:00
durations.add([startCreationTime, endCreationTime]);
}
2021-04-27 16:00:29 +00:00
final files = await _filesDB.getFilesCreatedWithinDurations(durations);
final seenTimes = await _memoriesDB.getSeenTimes();
2021-04-27 16:00:29 +00:00
final List<Memory> memories = [];
final filter = ImportantItemsFilter();
for (final file in files) {
2020-07-21 08:06:15 +00:00
if (filter.shouldInclude(file)) {
2020-08-09 22:34:59 +00:00
final seenTime = seenTimes[file.generatedID] ?? -1;
memories.add(Memory(file, seenTime));
2020-07-21 08:06:15 +00:00
}
}
2020-07-20 15:09:30 +00:00
_logger.info("Number of memories: " + memories.length.toString());
2021-04-27 16:00:29 +00:00
final endTime = DateTime.now();
final duration = Duration(
microseconds: endTime.microsecondsSinceEpoch -
presentTime.microsecondsSinceEpoch);
_logger.info("Time taken: " + duration.inMilliseconds.toString() + "ms");
2021-02-14 09:42:02 +00:00
_cachedMemories = memories;
return _cachedMemories;
}
2020-07-21 08:06:15 +00:00
DateTime _getDate(DateTime present, int yearAgo) {
final year = (present.year - yearAgo).toString();
final month = present.month > 9
? present.month.toString()
: "0" + present.month.toString();
final day =
present.day > 9 ? present.day.toString() : "0" + present.day.toString();
final date = DateTime.parse(year + "-" + month + "-" + day);
return date;
}
Future markMemoryAsSeen(Memory memory) async {
_logger.info("Marking memory " + memory.file.title + " as seen");
2020-07-21 22:01:44 +00:00
await _memoriesDB.markMemoryAsSeen(
memory, DateTime.now().microsecondsSinceEpoch);
2020-07-21 22:01:44 +00:00
notifyListeners();
}
}