Skip to content

High performance self-hosted photo and video management solution.

fix(mobile): handle transient loading states for map timelines (#29735)

Adam Gastineaudc813e281675
diff --git a/mobile/lib/domain/models/map.model.dart b/mobile/lib/domain/models/map.model.dart
index 52f6fe8afd..9da424534c 100644
--- a/mobile/lib/domain/models/map.model.dart
+++ b/mobile/lib/domain/models/map.model.dart
@@ -1,4 +1,5 @@
import 'package:freezed_annotation/freezed_annotation.dart';
+import 'package:immich_mobile/domain/models/time_range.model.dart';
import 'package:maplibre_gl/maplibre_gl.dart';
part 'map.model.freezed.dart';
@@ -7,3 +8,17 @@ part 'map.model.freezed.dart';
abstract class Marker with _$Marker {
const factory Marker({required LatLng location, required String assetId}) = _Marker;
}
+
+@Freezed(fromJson: false, toJson: false)
+abstract class TimelineMapOptions with _$TimelineMapOptions {
+ const factory TimelineMapOptions({
+ required LatLngBounds bounds,
+
+ @Default(false) bool onlyFavorites,
+ @Default(false) bool includeArchived,
+ @Default(false) bool withPartners,
+ @Default(0) int relativeDays,
+
+ @Default(TimeRange()) TimeRange timeRange,
+ }) = _TimelineMapOptions;
+}
diff --git a/mobile/lib/domain/models/time_range.model.dart b/mobile/lib/domain/models/time_range.model.dart
index b0b990d6db..67ddfdc2d6 100644
--- a/mobile/lib/domain/models/time_range.model.dart
+++ b/mobile/lib/domain/models/time_range.model.dart
@@ -3,15 +3,16 @@ import 'package:immich_mobile/utils/option.dart';
part 'time_range.model.freezed.dart';
-@Freezed(copyWith: false)
+// copyWith is hand-written so that fields can be cleared via optionals
+@Freezed(copyWith: false, fromJson: false, toJson: false)
abstract class TimeRange with _$TimeRange {
const TimeRange._();
const factory TimeRange({DateTime? from, DateTime? to}) = _TimeRange;
- // Patching is custom, which prevents using Freezed `copyWith`
- TimeRange copyWith({Option<DateTime>? from, Option<DateTime>? to}) =>
- TimeRange(from: from.patch(this.from), to: to.patch(this.to));
+ TimeRange copyWith({Option<DateTime>? from, Option<DateTime>? to}) {
+ return TimeRange(from: from.patch(this.from), to: to.patch(this.to));
+ }
TimeRange clearFrom() => TimeRange(to: to);
TimeRange clearTo() => TimeRange(from: from);
diff --git a/mobile/lib/domain/services/map.service.dart b/mobile/lib/domain/services/map.service.dart
index 5e0a3bd857..3a784e3924 100644
--- a/mobile/lib/domain/services/map.service.dart
+++ b/mobile/lib/domain/services/map.service.dart
@@ -1,6 +1,5 @@
import 'package:immich_mobile/domain/models/map.model.dart';
import 'package:immich_mobile/infrastructure/repositories/map.repository.dart';
-import 'package:immich_mobile/infrastructure/repositories/timeline.repository.dart';
import 'package:maplibre_gl/maplibre_gl.dart';
typedef MapMarkerSource = Future<List<Marker>> Function(LatLngBounds? bounds);
diff --git a/mobile/lib/domain/services/timeline.service.dart b/mobile/lib/domain/services/timeline.service.dart
index 84895b6056..6bd088b74a 100644
--- a/mobile/lib/domain/services/timeline.service.dart
+++ b/mobile/lib/domain/services/timeline.service.dart
@@ -5,6 +5,7 @@ import 'package:collection/collection.dart';
import 'package:immich_mobile/constants/constants.dart';
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
import 'package:immich_mobile/domain/models/events.model.dart';
+import 'package:immich_mobile/domain/models/map.model.dart';
import 'package:immich_mobile/domain/models/timeline.model.dart';
import 'package:immich_mobile/domain/utils/event_stream.dart';
import 'package:immich_mobile/infrastructure/repositories/settings.repository.dart';
@@ -85,8 +86,12 @@ class TimelineFactory {
TimelineService fromAssetsWithBuckets(List<BaseAsset> assets, TimelineOrigin type) =>
TimelineService(_timelineRepository.fromAssetsWithBuckets(assets, type));
- TimelineService map(List<String> userIds, TimelineMapOptions options) =>
- TimelineService(_timelineRepository.map(userIds, options, groupBy));
+ /// Creates a TimelineService for serving geographical map queries, such assets within bounded locations
+ TimelineService geographicMap(
+ List<String> userIds,
+ TimelineMapOptions Function() currentOptions,
+ Stream<TimelineMapOptions> optionsStream,
+ ) => TimelineService(_timelineRepository.geographicMap(userIds, currentOptions, optionsStream, groupBy));
}
class TimelineService {
diff --git a/mobile/lib/infrastructure/repositories/map.repository.dart b/mobile/lib/infrastructure/repositories/map.repository.dart
index 7a6e0e8dad..e046b9a0b8 100644
--- a/mobile/lib/infrastructure/repositories/map.repository.dart
+++ b/mobile/lib/infrastructure/repositories/map.repository.dart
@@ -6,7 +6,6 @@ import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
import 'package:immich_mobile/domain/models/map.model.dart';
import 'package:immich_mobile/domain/services/map.service.dart';
import 'package:immich_mobile/infrastructure/repositories/map.repository.drift.dart';
-import 'package:immich_mobile/infrastructure/repositories/timeline.repository.dart';
import 'package:maplibre_gl/maplibre_gl.dart';
@DriftAccessor()
diff --git a/mobile/lib/infrastructure/repositories/timeline.repository.dart b/mobile/lib/infrastructure/repositories/timeline.repository.dart
index 4b7c5cf90c..729de23cd8 100644
--- a/mobile/lib/infrastructure/repositories/timeline.repository.dart
+++ b/mobile/lib/infrastructure/repositories/timeline.repository.dart
@@ -8,32 +8,13 @@ import 'package:immich_mobile/data/db/main/table/remote/asset.dart';
import 'package:immich_mobile/data/db/main/table/remote/asset.drift.dart';
import 'package:immich_mobile/domain/models/album/album.model.dart';
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
-import 'package:immich_mobile/domain/models/time_range.model.dart';
+import 'package:immich_mobile/domain/models/map.model.dart';
import 'package:immich_mobile/domain/models/timeline.model.dart';
import 'package:immich_mobile/domain/services/timeline.service.dart';
import 'package:immich_mobile/infrastructure/repositories/map.repository.dart';
import 'package:immich_mobile/infrastructure/repositories/timeline.repository.drift.dart';
-import 'package:maplibre_gl/maplibre_gl.dart';
import 'package:stream_transform/stream_transform.dart';
-class TimelineMapOptions {
- final LatLngBounds bounds;
- final bool onlyFavorites;
- final bool includeArchived;
- final bool withPartners;
- final int relativeDays;
- final TimeRange timeRange;
-
- const TimelineMapOptions({
- required this.bounds,
- this.onlyFavorites = false,
- this.includeArchived = false,
- this.withPartners = false,
- this.relativeDays = 0,
- this.timeRange = const TimeRange(),
- });
-}
-
@DriftAccessor()
class TimelineRepository extends DatabaseAccessor<Drift> with $TimelineRepositoryMixin {
TimelineRepository(super.attachedDatabase);
@@ -524,9 +505,21 @@ class TimelineRepository extends DatabaseAccessor<Drift> with $TimelineRepositor
return query.map((row) => row.toDto()).get();
}
- TimelineQuery map(List<String> userIds, TimelineMapOptions options, GroupAssetsBy groupBy) => (
- bucketSource: () => _watchMapBucket(userIds, options, groupBy: groupBy),
- assetSource: (offset, count) => _getMapBucketAssets(userIds, options, offset: offset, count: count),
+ /// Creates a geographic map query that can dynamically filter on changing [TimelineMapOptions]
+ /// (most notably the active map bounds)
+ TimelineQuery geographicMap(
+ List<String> userIds,
+ TimelineMapOptions Function() currentOptions,
+ Stream<TimelineMapOptions> optionsStream,
+ GroupAssetsBy groupBy,
+ ) => (
+ bucketSource: () => Stream.value(currentOptions())
+ .followedBy(optionsStream)
+ .switchMap(
+ // Any error would kill the stream for all options; make sure the stream stays alive
+ (options) => _watchMapBucket(userIds, options, groupBy: groupBy).handleError((_) {}),
+ ),
+ assetSource: (offset, count) => _getMapBucketAssets(userIds, currentOptions(), offset: offset, count: count),
origin: TimelineOrigin.map,
);
diff --git a/mobile/lib/presentation/widgets/bottom_sheet/map_bottom_sheet.widget.dart b/mobile/lib/presentation/widgets/bottom_sheet/map_bottom_sheet.widget.dart
index 95bd50f7fb..f81992b0f1 100644
--- a/mobile/lib/presentation/widgets/bottom_sheet/map_bottom_sheet.widget.dart
+++ b/mobile/lib/presentation/widgets/bottom_sheet/map_bottom_sheet.widget.dart
@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/domain/models/events.model.dart';
+import 'package:immich_mobile/domain/models/map.model.dart';
import 'package:immich_mobile/domain/utils/event_stream.dart';
import 'package:immich_mobile/extensions/build_context_extensions.dart';
import 'package:immich_mobile/generated/translations.g.dart';
@@ -40,7 +41,6 @@ class _ScopedMapTimeline extends StatelessWidget {
@override
Widget build(BuildContext context) {
- // TODO: this causes the timeline to switch to flicker to "loading" state and back. This is both janky and inefficient.
return ProviderScope(
overrides: [
timelineServiceProvider.overrideWith((ref) {
@@ -49,14 +49,24 @@ class _ScopedMapTimeline extends StatelessWidget {
throw Exception('User must be logged in to access archive');
}
- final users = ref.watch(mapStateProvider).withPartners
- ? ref.watch(timelineUsersProvider).valueOrNull ?? [user.id]
- : [user.id];
+ final withPartners = ref.watch(mapStateProvider.select((s) => s.withPartners));
+ final users = withPartners ? ref.watch(timelineUsersProvider).valueOrNull ?? [user.id] : [user.id];
+
+ final optionsController = StreamController<TimelineMapOptions>.broadcast();
+ ref.onDispose(optionsController.close);
+
+ var currentOptions = ref.read(mapStateProvider).toOptions();
+
+ ref.listen(mapStateProvider.select((state) => state.toOptions()), (_, newOptions) {
+ currentOptions = newOptions;
+ optionsController.add(newOptions);
+ });
final timelineService = ref
.watch(timelineFactoryProvider)
- .map(users, ref.watch(mapStateProvider).toOptions());
+ .geographicMap(users, () => currentOptions, optionsController.stream);
ref.onDispose(timelineService.dispose);
+
return timelineService;
}),
],
diff --git a/mobile/lib/presentation/widgets/map/map.state.dart b/mobile/lib/presentation/widgets/map/map.state.dart
index bc6436fc34..3370fb736c 100644
--- a/mobile/lib/presentation/widgets/map/map.state.dart
+++ b/mobile/lib/presentation/widgets/map/map.state.dart
@@ -4,9 +4,9 @@ import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/domain/models/events.model.dart';
+import 'package:immich_mobile/domain/models/map.model.dart';
import 'package:immich_mobile/domain/models/time_range.model.dart';
import 'package:immich_mobile/domain/utils/event_stream.dart';
-import 'package:immich_mobile/infrastructure/repositories/timeline.repository.dart';
import 'package:immich_mobile/providers/infrastructure/map.provider.dart';
import 'package:immich_mobile/providers/infrastructure/settings.provider.dart';
import 'package:immich_mobile/providers/map/map_state.provider.dart';