From 739b23b8ba0b03d0ba8d6e63feedfc43513ef3c9 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Mon, 15 Jul 2024 01:19:03 +0800 Subject: [PATCH] DB api to support filtering locations by time range --- app/lib/entity/image_location/data_source.dart | 7 +++++-- app/lib/entity/image_location/repo.dart | 10 ++++++---- np_db/lib/src/api.dart | 1 + .../lib/src/database/image_location_extension.dart | 8 +++++++- np_db_sqlite/lib/src/sqlite_api.dart | 2 ++ 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/lib/entity/image_location/data_source.dart b/app/lib/entity/image_location/data_source.dart index 3b8b4919..53cc3d8e 100644 --- a/app/lib/entity/image_location/data_source.dart +++ b/app/lib/entity/image_location/data_source.dart @@ -8,6 +8,7 @@ import 'package:nc_photos/entity/image_location/repo.dart'; import 'package:nc_photos/remote_storage_util.dart' as remote_storage_util; import 'package:np_async/np_async.dart'; import 'package:np_codegen/np_codegen.dart'; +import 'package:np_datetime/np_datetime.dart'; import 'package:np_db/np_db.dart'; part 'data_source.g.dart'; @@ -17,10 +18,12 @@ class ImageLocationNpDbDataSource implements ImageLocationDataSource { const ImageLocationNpDbDataSource(this.db); @override - Future> getLocations(Account account) async { - _log.info("[getLocations]"); + Future> getLocations( + Account account, TimeRange timeRange) async { + _log.info("[getLocations] timeRange: $timeRange"); final results = await db.getImageLatLngWithFileIds( account: account.toDb(), + timeRange: timeRange, includeRelativeRoots: account.roots .map((e) => File(path: file_util.unstripPath(account, e)) .strippedPathWithEmpty) diff --git a/app/lib/entity/image_location/repo.dart b/app/lib/entity/image_location/repo.dart index a97f3f59..cca0b0e9 100644 --- a/app/lib/entity/image_location/repo.dart +++ b/app/lib/entity/image_location/repo.dart @@ -2,6 +2,7 @@ import 'package:equatable/equatable.dart'; import 'package:logging/logging.dart'; import 'package:nc_photos/account.dart'; import 'package:np_codegen/np_codegen.dart'; +import 'package:np_datetime/np_datetime.dart'; part 'repo.g.dart'; @@ -28,7 +29,7 @@ abstract class ImageLocationRepo { /// Query all locations with the corresponding file ids /// /// Returned data are sorted by the file date time in descending order - Future> getLocations(Account account); + Future> getLocations(Account account, TimeRange timeRange); } @npLog @@ -36,8 +37,9 @@ class BasicImageLocationRepo implements ImageLocationRepo { const BasicImageLocationRepo(this.dataSrc); @override - Future> getLocations(Account account) => - dataSrc.getLocations(account); + Future> getLocations( + Account account, TimeRange timeRange) => + dataSrc.getLocations(account, timeRange); final ImageLocationDataSource dataSrc; } @@ -46,5 +48,5 @@ abstract class ImageLocationDataSource { /// Query all locations with the corresponding file ids /// /// Returned data are sorted by the file date time in descending order - Future> getLocations(Account account); + Future> getLocations(Account account, TimeRange timeRange); } diff --git a/np_db/lib/src/api.dart b/np_db/lib/src/api.dart index a184f250..1c988b24 100644 --- a/np_db/lib/src/api.dart +++ b/np_db/lib/src/api.dart @@ -435,6 +435,7 @@ abstract class NpDb { /// Return the latitude, longitude and the file id of all files Future> getImageLatLngWithFileIds({ required DbAccount account, + TimeRange? timeRange, List? includeRelativeRoots, List? excludeRelativeRoots, List? mimes, diff --git a/np_db_sqlite/lib/src/database/image_location_extension.dart b/np_db_sqlite/lib/src/database/image_location_extension.dart index 53cb9a5e..901dfd33 100644 --- a/np_db_sqlite/lib/src/database/image_location_extension.dart +++ b/np_db_sqlite/lib/src/database/image_location_extension.dart @@ -31,12 +31,13 @@ class ImageLatLng { extension SqliteDbImageLocationExtension on SqliteDb { Future> queryImageLatLngWithFileIds({ required ByAccount account, + TimeRange? timeRange, List? includeRelativeRoots, List? includeRelativeDirs, List? excludeRelativeRoots, List? mimes, }) async { - _log.info("[queryImageLatLngWithFileIds]"); + _log.info("[queryImageLatLngWithFileIds] timeRange: $timeRange"); final query = _queryFiles().let((q) { q ..setQueryMode( @@ -73,6 +74,11 @@ extension SqliteDbImageLocationExtension on SqliteDb { } else { query.where(files.isCollection.isNotValue(true)); } + if (timeRange != null) { + accountFiles.bestDateTime + .isBetweenTimeRange(timeRange) + ?.let((e) => query.where(e)); + } query ..where(imageLocations.latitude.isNotNull() & imageLocations.longitude.isNotNull()) diff --git a/np_db_sqlite/lib/src/sqlite_api.dart b/np_db_sqlite/lib/src/sqlite_api.dart index b0c90919..bff7c0aa 100644 --- a/np_db_sqlite/lib/src/sqlite_api.dart +++ b/np_db_sqlite/lib/src/sqlite_api.dart @@ -578,6 +578,7 @@ class NpDbSqlite implements NpDb { @override Future> getImageLatLngWithFileIds({ required DbAccount account, + TimeRange? timeRange, List? includeRelativeRoots, List? excludeRelativeRoots, List? mimes, @@ -585,6 +586,7 @@ class NpDbSqlite implements NpDb { final sqlObjs = await _db.use((db) async { return await db.queryImageLatLngWithFileIds( account: ByAccount.db(account), + timeRange: timeRange, includeRelativeRoots: includeRelativeRoots, excludeRelativeRoots: excludeRelativeRoots, mimes: mimes,