2020-07-20 11:55:17 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:path/path.dart';
|
2021-07-21 20:34:53 +00:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2020-07-20 11:55:17 +00:00
|
|
|
import 'package:photos/models/memory.dart';
|
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
|
|
|
|
class MemoriesDB {
|
|
|
|
static final _databaseName = "ente.memories.db";
|
|
|
|
static final _databaseVersion = 1;
|
|
|
|
|
|
|
|
static final table = 'memories';
|
|
|
|
|
2020-07-21 21:06:28 +00:00
|
|
|
static final columnFileID = 'file_id';
|
2020-07-20 11:55:17 +00:00
|
|
|
static final columnSeenTime = 'seen_time';
|
|
|
|
|
|
|
|
MemoriesDB._privateConstructor();
|
|
|
|
static final MemoriesDB instance = MemoriesDB._privateConstructor();
|
|
|
|
|
2021-07-21 20:34:53 +00:00
|
|
|
static Future<Database> _dbFuture;
|
2020-07-20 11:55:17 +00:00
|
|
|
Future<Database> get database async {
|
2021-07-21 20:34:53 +00:00
|
|
|
_dbFuture ??= _initDatabase();
|
|
|
|
return _dbFuture;
|
2020-07-20 11:55:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 20:34:53 +00:00
|
|
|
Future<Database> _initDatabase() async {
|
2020-07-20 11:55:17 +00:00
|
|
|
Directory documentsDirectory = await getApplicationDocumentsDirectory();
|
|
|
|
String path = join(documentsDirectory.path, _databaseName);
|
|
|
|
return await openDatabase(
|
|
|
|
path,
|
|
|
|
version: _databaseVersion,
|
|
|
|
onCreate: _onCreate,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future _onCreate(Database db, int version) async {
|
|
|
|
await db.execute('''
|
|
|
|
CREATE TABLE $table (
|
2020-07-21 21:06:28 +00:00
|
|
|
$columnFileID INTEGER PRIMARY KEY NOT NULL,
|
2020-07-20 11:55:17 +00:00
|
|
|
$columnSeenTime TEXT NOT NULL
|
|
|
|
)
|
|
|
|
''');
|
|
|
|
}
|
|
|
|
|
2021-03-17 21:07:17 +00:00
|
|
|
Future<void> clearTable() async {
|
|
|
|
final db = await instance.database;
|
|
|
|
await db.delete(table);
|
|
|
|
}
|
|
|
|
|
2020-07-20 11:55:17 +00:00
|
|
|
Future<int> clearMemoriesSeenBeforeTime(int timestamp) async {
|
|
|
|
final db = await instance.database;
|
|
|
|
return db.delete(
|
|
|
|
table,
|
2020-07-20 13:26:21 +00:00
|
|
|
where: '$columnSeenTime < ?',
|
2020-07-20 11:55:17 +00:00
|
|
|
whereArgs: [timestamp],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> markMemoryAsSeen(Memory memory, int timestamp) async {
|
|
|
|
final db = await instance.database;
|
|
|
|
return await db.insert(table, _getRowForSeenMemory(memory, timestamp),
|
|
|
|
conflictAlgorithm: ConflictAlgorithm.replace);
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:38:13 +00:00
|
|
|
Future<Map<int, int>> getSeenTimes() async {
|
2020-07-20 11:55:17 +00:00
|
|
|
final db = await instance.database;
|
2020-07-29 15:38:13 +00:00
|
|
|
return _convertToSeenTimes(await db.query(table));
|
2020-07-20 11:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> _getRowForSeenMemory(Memory memory, int timestamp) {
|
2021-07-22 18:41:58 +00:00
|
|
|
var row = <String, dynamic>{};
|
2020-08-09 22:34:59 +00:00
|
|
|
row[columnFileID] = memory.file.generatedID;
|
2020-07-20 11:55:17 +00:00
|
|
|
row[columnSeenTime] = timestamp;
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:38:13 +00:00
|
|
|
Map<int, int> _convertToSeenTimes(List<Map<String, dynamic>> rows) {
|
2021-07-22 18:41:58 +00:00
|
|
|
final seenTimes = <int, int>{};
|
2020-07-20 11:55:17 +00:00
|
|
|
for (final row in rows) {
|
2020-07-29 15:38:13 +00:00
|
|
|
seenTimes[row[columnFileID]] = int.parse(row[columnSeenTime]);
|
2020-07-20 11:55:17 +00:00
|
|
|
}
|
2020-07-29 15:38:13 +00:00
|
|
|
return seenTimes;
|
2020-07-20 11:55:17 +00:00
|
|
|
}
|
|
|
|
}
|