DB api to support filtering locations by time range

This commit is contained in:
Ming Ming 2024-07-15 01:19:03 +08:00
parent 4bb825a7dc
commit 739b23b8ba
5 changed files with 21 additions and 7 deletions

View file

@ -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:nc_photos/remote_storage_util.dart' as remote_storage_util;
import 'package:np_async/np_async.dart'; import 'package:np_async/np_async.dart';
import 'package:np_codegen/np_codegen.dart'; import 'package:np_codegen/np_codegen.dart';
import 'package:np_datetime/np_datetime.dart';
import 'package:np_db/np_db.dart'; import 'package:np_db/np_db.dart';
part 'data_source.g.dart'; part 'data_source.g.dart';
@ -17,10 +18,12 @@ class ImageLocationNpDbDataSource implements ImageLocationDataSource {
const ImageLocationNpDbDataSource(this.db); const ImageLocationNpDbDataSource(this.db);
@override @override
Future<List<ImageLatLng>> getLocations(Account account) async { Future<List<ImageLatLng>> getLocations(
_log.info("[getLocations]"); Account account, TimeRange timeRange) async {
_log.info("[getLocations] timeRange: $timeRange");
final results = await db.getImageLatLngWithFileIds( final results = await db.getImageLatLngWithFileIds(
account: account.toDb(), account: account.toDb(),
timeRange: timeRange,
includeRelativeRoots: account.roots includeRelativeRoots: account.roots
.map((e) => File(path: file_util.unstripPath(account, e)) .map((e) => File(path: file_util.unstripPath(account, e))
.strippedPathWithEmpty) .strippedPathWithEmpty)

View file

@ -2,6 +2,7 @@ import 'package:equatable/equatable.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart'; import 'package:nc_photos/account.dart';
import 'package:np_codegen/np_codegen.dart'; import 'package:np_codegen/np_codegen.dart';
import 'package:np_datetime/np_datetime.dart';
part 'repo.g.dart'; part 'repo.g.dart';
@ -28,7 +29,7 @@ abstract class ImageLocationRepo {
/// Query all locations with the corresponding file ids /// Query all locations with the corresponding file ids
/// ///
/// Returned data are sorted by the file date time in descending order /// Returned data are sorted by the file date time in descending order
Future<List<ImageLatLng>> getLocations(Account account); Future<List<ImageLatLng>> getLocations(Account account, TimeRange timeRange);
} }
@npLog @npLog
@ -36,8 +37,9 @@ class BasicImageLocationRepo implements ImageLocationRepo {
const BasicImageLocationRepo(this.dataSrc); const BasicImageLocationRepo(this.dataSrc);
@override @override
Future<List<ImageLatLng>> getLocations(Account account) => Future<List<ImageLatLng>> getLocations(
dataSrc.getLocations(account); Account account, TimeRange timeRange) =>
dataSrc.getLocations(account, timeRange);
final ImageLocationDataSource dataSrc; final ImageLocationDataSource dataSrc;
} }
@ -46,5 +48,5 @@ abstract class ImageLocationDataSource {
/// Query all locations with the corresponding file ids /// Query all locations with the corresponding file ids
/// ///
/// Returned data are sorted by the file date time in descending order /// Returned data are sorted by the file date time in descending order
Future<List<ImageLatLng>> getLocations(Account account); Future<List<ImageLatLng>> getLocations(Account account, TimeRange timeRange);
} }

View file

@ -435,6 +435,7 @@ abstract class NpDb {
/// Return the latitude, longitude and the file id of all files /// Return the latitude, longitude and the file id of all files
Future<List<DbImageLatLng>> getImageLatLngWithFileIds({ Future<List<DbImageLatLng>> getImageLatLngWithFileIds({
required DbAccount account, required DbAccount account,
TimeRange? timeRange,
List<String>? includeRelativeRoots, List<String>? includeRelativeRoots,
List<String>? excludeRelativeRoots, List<String>? excludeRelativeRoots,
List<String>? mimes, List<String>? mimes,

View file

@ -31,12 +31,13 @@ class ImageLatLng {
extension SqliteDbImageLocationExtension on SqliteDb { extension SqliteDbImageLocationExtension on SqliteDb {
Future<List<ImageLatLng>> queryImageLatLngWithFileIds({ Future<List<ImageLatLng>> queryImageLatLngWithFileIds({
required ByAccount account, required ByAccount account,
TimeRange? timeRange,
List<String>? includeRelativeRoots, List<String>? includeRelativeRoots,
List<String>? includeRelativeDirs, List<String>? includeRelativeDirs,
List<String>? excludeRelativeRoots, List<String>? excludeRelativeRoots,
List<String>? mimes, List<String>? mimes,
}) async { }) async {
_log.info("[queryImageLatLngWithFileIds]"); _log.info("[queryImageLatLngWithFileIds] timeRange: $timeRange");
final query = _queryFiles().let((q) { final query = _queryFiles().let((q) {
q q
..setQueryMode( ..setQueryMode(
@ -73,6 +74,11 @@ extension SqliteDbImageLocationExtension on SqliteDb {
} else { } else {
query.where(files.isCollection.isNotValue(true)); query.where(files.isCollection.isNotValue(true));
} }
if (timeRange != null) {
accountFiles.bestDateTime
.isBetweenTimeRange(timeRange)
?.let((e) => query.where(e));
}
query query
..where(imageLocations.latitude.isNotNull() & ..where(imageLocations.latitude.isNotNull() &
imageLocations.longitude.isNotNull()) imageLocations.longitude.isNotNull())

View file

@ -578,6 +578,7 @@ class NpDbSqlite implements NpDb {
@override @override
Future<List<DbImageLatLng>> getImageLatLngWithFileIds({ Future<List<DbImageLatLng>> getImageLatLngWithFileIds({
required DbAccount account, required DbAccount account,
TimeRange? timeRange,
List<String>? includeRelativeRoots, List<String>? includeRelativeRoots,
List<String>? excludeRelativeRoots, List<String>? excludeRelativeRoots,
List<String>? mimes, List<String>? mimes,
@ -585,6 +586,7 @@ class NpDbSqlite implements NpDb {
final sqlObjs = await _db.use((db) async { final sqlObjs = await _db.use((db) async {
return await db.queryImageLatLngWithFileIds( return await db.queryImageLatLngWithFileIds(
account: ByAccount.db(account), account: ByAccount.db(account),
timeRange: timeRange,
includeRelativeRoots: includeRelativeRoots, includeRelativeRoots: includeRelativeRoots,
excludeRelativeRoots: excludeRelativeRoots, excludeRelativeRoots: excludeRelativeRoots,
mimes: mimes, mimes: mimes,