Allow TimeRange to be open

This commit is contained in:
Ming Ming 2024-04-10 00:40:14 +08:00
parent 25ef1a94f9
commit 6281e6daff
4 changed files with 48 additions and 20 deletions

View file

@ -5,9 +5,9 @@ enum TimeRangeBound {
class TimeRange {
const TimeRange({
required this.from,
this.from,
this.fromBound = TimeRangeBound.inclusive,
required this.to,
this.to,
this.toBound = TimeRangeBound.exclusive,
});
@ -18,9 +18,9 @@ class TimeRange {
"${toBound == TimeRangeBound.inclusive ? "]" : ")"}";
}
final DateTime from;
final DateTime? from;
final TimeRangeBound fromBound;
final DateTime to;
final DateTime? to;
final TimeRangeBound toBound;
}
@ -30,22 +30,26 @@ extension TimeRangeExtension on TimeRange {
/// The comparison is independent of whether the time is in UTC or in the
/// local time zone
bool isIn(DateTime a) {
if (a.isBefore(from)) {
if (from != null) {
if (a.isBefore(from!)) {
return false;
}
if (fromBound == TimeRangeBound.exclusive) {
if (a.isAtSameMomentAs(from)) {
if (a.isAtSameMomentAs(from!)) {
return false;
}
}
if (a.isAfter(to)) {
}
if (to != null) {
if (a.isAfter(to!)) {
return false;
}
if (toBound == TimeRangeBound.exclusive) {
if (a.isAtSameMomentAs(to)) {
if (a.isAtSameMomentAs(to!)) {
return false;
}
}
}
return true;
}
}

View file

@ -143,12 +143,10 @@ extension SqliteDbFileExtension on SqliteDb {
}
return q.build();
});
final dateTime = accountFiles.bestDateTime.unixepoch;
query
..where(dateTime.isBetweenValues(
range.from.millisecondsSinceEpoch ~/ 1000,
(range.to.millisecondsSinceEpoch ~/ 1000) - 1))
..orderBy([OrderingTerm.desc(dateTime)]);
accountFiles.bestDateTime
.isBetweenTimeRange(range)
?.let((e) => query.where(e));
query.orderBy([OrderingTerm.desc(accountFiles.bestDateTime.unixepoch)]);
return _mapCompleteFile(query);
}

View file

@ -18,6 +18,7 @@ import 'package:np_db_sqlite/src/files_query_builder.dart';
import 'package:np_db_sqlite/src/isolate_util.dart';
import 'package:np_db_sqlite/src/k.dart' as k;
import 'package:np_db_sqlite/src/table.dart';
import 'package:np_db_sqlite/src/util.dart';
import 'package:np_geocoder/np_geocoder.dart';
import 'package:np_platform_lock/np_platform_lock.dart';
import 'package:np_platform_util/np_platform_util.dart';

View file

@ -2,6 +2,8 @@ import 'dart:io' as io;
import 'package:drift/drift.dart';
import 'package:flutter/foundation.dart';
import 'package:np_common/object_util.dart';
import 'package:np_datetime/np_datetime.dart';
import 'package:np_db_sqlite/src/database.dart';
import 'package:np_db_sqlite/src/native/util.dart'
if (dart.library.html) 'package:np_db_sqlite/src/web/util.dart' as impl;
@ -27,3 +29,26 @@ Future<void> applyWorkaroundToOpenSqlite3OnOldAndroidVersions() =>
/// means only internal directories are allowed
Future<io.File> exportSqliteDb(SqliteDb db, io.Directory dir) =>
impl.exportSqliteDb(db, dir);
extension DateTimeColumnExtension on Column<DateTime> {
Expression<bool>? isBetweenTimeRange(TimeRange range) {
final epoch = unixepoch;
// convert ranges to inclusive
final from = range.fromBound == TimeRangeBound.inclusive
? range.from?.millisecondsSinceEpoch.let((e) => e ~/ 1000)
: range.from?.millisecondsSinceEpoch.let((e) => e ~/ 1000 + 1);
final to = range.toBound == TimeRangeBound.inclusive
? range.to?.millisecondsSinceEpoch.let((e) => e ~/ 1000)
: range.to?.millisecondsSinceEpoch.let((e) => e ~/ 1000 - 1);
if (from != null && to != null) {
return epoch.isBetweenValues(from, to);
} else if (from != null) {
return epoch.isBiggerOrEqualValue(from);
} else if (to != null) {
return epoch.isSmallerOrEqualValue(to);
} else {
// both null
return null;
}
}
}