refactor: rename timerange & remove isvalid

This commit is contained in:
Yaros
2026-02-19 13:23:40 +01:00
parent 39d2e14d3a
commit b0f6d5cf38
5 changed files with 43 additions and 35 deletions
@@ -27,12 +27,15 @@ class DriftMapRepository extends DriftDatabaseRepository {
condition = condition & _db.remoteAssetEntity.isFavorite.equals(true); condition = condition & _db.remoteAssetEntity.isFavorite.equals(true);
} }
if (options.customTimeRange.isValid) { final from = options.customTimeRange.from;
if (options.customTimeRange.from != null) { final to = options.customTimeRange.to;
condition = condition & _db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(options.customTimeRange.from!);
if (from != null || to != null) {
if (from != null) {
condition = condition & _db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(from);
} }
if (options.customTimeRange.to != null) { if (to != null) {
condition = condition & _db.remoteAssetEntity.createdAt.isSmallerOrEqualValue(options.customTimeRange.to!); condition = condition & _db.remoteAssetEntity.createdAt.isSmallerOrEqualValue(to);
} }
} else if (options.relativeDays > 0) { } else if (options.relativeDays > 0) {
final fromDate = DateTime.now().subtract(Duration(days: options.relativeDays)); final fromDate = DateTime.now().subtract(Duration(days: options.relativeDays));
@@ -22,7 +22,7 @@ class TimelineMapOptions {
final bool includeArchived; final bool includeArchived;
final bool withPartners; final bool withPartners;
final int relativeDays; final int relativeDays;
final CustomTimeRange customTimeRange; final TimeRange customTimeRange;
const TimelineMapOptions({ const TimelineMapOptions({
required this.bounds, required this.bounds,
@@ -30,7 +30,7 @@ class TimelineMapOptions {
this.includeArchived = false, this.includeArchived = false,
this.withPartners = false, this.withPartners = false,
this.relativeDays = 0, this.relativeDays = 0,
this.customTimeRange = const CustomTimeRange(), this.customTimeRange = const TimeRange(),
}); });
} }
@@ -531,13 +531,16 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
query.where(_db.remoteAssetEntity.isFavorite.equals(true)); query.where(_db.remoteAssetEntity.isFavorite.equals(true));
} }
if (options.customTimeRange.isValid) { final from = options.customTimeRange.from;
final to = options.customTimeRange.to;
if (from != null || to != null) {
// Use custom from/to filters // Use custom from/to filters
if (options.customTimeRange.from != null) { if (from != null) {
query.where(_db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(options.customTimeRange.from!)); query.where(_db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(from));
} }
if (options.customTimeRange.to != null) { if (to != null) {
query.where(_db.remoteAssetEntity.createdAt.isSmallerOrEqualValue(options.customTimeRange.to!)); query.where(_db.remoteAssetEntity.createdAt.isSmallerOrEqualValue(to));
} }
} else if (options.relativeDays > 0) { } else if (options.relativeDays > 0) {
// Use relative days // Use relative days
@@ -582,13 +585,16 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
query.where(_db.remoteAssetEntity.isFavorite.equals(true)); query.where(_db.remoteAssetEntity.isFavorite.equals(true));
} }
if (options.customTimeRange.isValid) { final from = options.customTimeRange.from;
final to = options.customTimeRange.to;
if (from != null || to != null) {
// Use custom from/to filters // Use custom from/to filters
if (options.customTimeRange.from != null) { if (from != null) {
query.where(_db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(options.customTimeRange.from!)); query.where(_db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(from));
} }
if (options.customTimeRange.to != null) { if (to != null) {
query.where(_db.remoteAssetEntity.createdAt.isSmallerOrEqualValue(options.customTimeRange.to!)); query.where(_db.remoteAssetEntity.createdAt.isSmallerOrEqualValue(to));
} }
} else if (options.relativeDays > 0) { } else if (options.relativeDays > 0) {
// Use relative days // Use relative days
@@ -9,20 +9,18 @@ import 'package:immich_mobile/providers/map/map_state.provider.dart';
import 'package:immich_mobile/services/app_settings.service.dart'; import 'package:immich_mobile/services/app_settings.service.dart';
import 'package:maplibre_gl/maplibre_gl.dart'; import 'package:maplibre_gl/maplibre_gl.dart';
class CustomTimeRange { class TimeRange {
final DateTime? from; final DateTime? from;
final DateTime? to; final DateTime? to;
const CustomTimeRange({this.from, this.to}); const TimeRange({this.from, this.to});
bool get isValid => from != null || to != null; TimeRange copyWith({DateTime? from, DateTime? to}) {
return TimeRange(from: from ?? this.from, to: to ?? this.to);
CustomTimeRange copyWith({DateTime? from, DateTime? to}) {
return CustomTimeRange(from: from ?? this.from, to: to ?? this.to);
} }
CustomTimeRange clearFrom() => CustomTimeRange(to: to); TimeRange clearFrom() => TimeRange(to: to);
CustomTimeRange clearTo() => CustomTimeRange(from: from); TimeRange clearTo() => TimeRange(from: from);
} }
class MapState { class MapState {
@@ -32,7 +30,7 @@ class MapState {
final bool includeArchived; final bool includeArchived;
final bool withPartners; final bool withPartners;
final int relativeDays; final int relativeDays;
final CustomTimeRange customTimeRange; final TimeRange customTimeRange;
const MapState({ const MapState({
this.themeMode = ThemeMode.system, this.themeMode = ThemeMode.system,
@@ -41,7 +39,7 @@ class MapState {
this.includeArchived = false, this.includeArchived = false,
this.withPartners = false, this.withPartners = false,
this.relativeDays = 0, this.relativeDays = 0,
this.customTimeRange = const CustomTimeRange(), this.customTimeRange = const TimeRange(),
}); });
@override @override
@@ -59,7 +57,7 @@ class MapState {
bool? includeArchived, bool? includeArchived,
bool? withPartners, bool? withPartners,
int? relativeDays, int? relativeDays,
CustomTimeRange? customTimeRange, TimeRange? customTimeRange,
}) { }) {
return MapState( return MapState(
bounds: bounds ?? this.bounds, bounds: bounds ?? this.bounds,
@@ -125,7 +123,7 @@ class MapStateNotifier extends Notifier<MapState> {
EventStream.shared.emit(const MapMarkerReloadEvent()); EventStream.shared.emit(const MapMarkerReloadEvent());
} }
void setCustomTimeRange(CustomTimeRange range) { void setCustomTimeRange(TimeRange range) {
ref ref
.read(appSettingsServiceProvider) .read(appSettingsServiceProvider)
.setSetting(AppSettingsEnum.mapCustomFrom, range.from == null ? "" : range.from!.toIso8601String()); .setSetting(AppSettingsEnum.mapCustomFrom, range.from == null ? "" : range.from!.toIso8601String());
@@ -148,7 +146,7 @@ class MapStateNotifier extends Notifier<MapState> {
withPartners: appSettingsService.getSetting(AppSettingsEnum.mapwithPartners), withPartners: appSettingsService.getSetting(AppSettingsEnum.mapwithPartners),
relativeDays: appSettingsService.getSetting(AppSettingsEnum.mapRelativeDate), relativeDays: appSettingsService.getSetting(AppSettingsEnum.mapRelativeDate),
bounds: LatLngBounds(northeast: const LatLng(0, 0), southwest: const LatLng(0, 0)), bounds: LatLngBounds(northeast: const LatLng(0, 0), southwest: const LatLng(0, 0)),
customTimeRange: CustomTimeRange( customTimeRange: TimeRange(
from: customFrom.isNotEmpty ? DateTime.parse(customFrom) : null, from: customFrom.isNotEmpty ? DateTime.parse(customFrom) : null,
to: customTo.isNotEmpty ? DateTime.parse(customTo) : null, to: customTo.isNotEmpty ? DateTime.parse(customTo) : null,
), ),
@@ -21,7 +21,8 @@ class _DriftMapSettingsSheetState extends ConsumerState<DriftMapSettingsSheet> {
void initState() { void initState() {
super.initState(); super.initState();
final mapState = ref.read(mapStateProvider); final mapState = ref.read(mapStateProvider);
useCustomRange = mapState.customTimeRange.isValid; final timeRange = mapState.customTimeRange;
useCustomRange = timeRange.from != null || timeRange.to != null;
} }
@override @override
@@ -75,7 +76,7 @@ class _DriftMapSettingsSheetState extends ConsumerState<DriftMapSettingsSheet> {
onPressed: () => setState(() { onPressed: () => setState(() {
useCustomRange = false; useCustomRange = false;
ref.read(mapStateProvider.notifier).setRelativeTime(0); ref.read(mapStateProvider.notifier).setRelativeTime(0);
ref.read(mapStateProvider.notifier).setCustomTimeRange(const CustomTimeRange()); ref.read(mapStateProvider.notifier).setCustomTimeRange(const TimeRange());
}), }),
child: Text("remove_custom_date_range".t(context: context)), child: Text("remove_custom_date_range".t(context: context)),
), ),
@@ -91,7 +92,7 @@ class _DriftMapSettingsSheetState extends ConsumerState<DriftMapSettingsSheet> {
onPressed: () => setState(() { onPressed: () => setState(() {
useCustomRange = true; useCustomRange = true;
ref.read(mapStateProvider.notifier).setRelativeTime(0); ref.read(mapStateProvider.notifier).setRelativeTime(0);
ref.read(mapStateProvider.notifier).setCustomTimeRange(const CustomTimeRange()); ref.read(mapStateProvider.notifier).setCustomTimeRange(const TimeRange());
}), }),
child: Text("use_custom_date_range".t(context: context)), child: Text("use_custom_date_range".t(context: context)),
), ),
@@ -6,8 +6,8 @@ import 'package:intl/intl.dart';
class MapCustomTimeRange extends StatelessWidget { class MapCustomTimeRange extends StatelessWidget {
const MapCustomTimeRange({super.key, required this.customTimeRange, required this.onChanged}); const MapCustomTimeRange({super.key, required this.customTimeRange, required this.onChanged});
final CustomTimeRange customTimeRange; final TimeRange customTimeRange;
final Function(CustomTimeRange) onChanged; final Function(TimeRange) onChanged;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {