Skip to content

High performance self-hosted photo and video management solution.

fix(mobile): use timeline scroll velocity to add placeholders (#29443)

Adam Gastineau8fbddc8f3ae7
diff --git a/mobile/lib/presentation/widgets/timeline/fixed/segment.model.dart b/mobile/lib/presentation/widgets/timeline/fixed/segment.model.dart
index 32bbffa95e..13abd664f3 100644
--- a/mobile/lib/presentation/widgets/timeline/fixed/segment.model.dart
+++ b/mobile/lib/presentation/widgets/timeline/fixed/segment.model.dart
@@ -106,7 +106,7 @@ class _FixedSegmentRow extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
- final isScrubbing = ref.watch(timelineStateProvider.select((s) => s.isScrubbing));
+ final recommendDeferredLoading = ref.watch(timelineStateProvider.select((s) => s.recommendDeferredLoading));
final timelineService = ref.watch(timelineServiceProvider);
final isDynamicLayout = columnCount <= (context.isMobile ? 2 : 3);
@@ -119,7 +119,7 @@ class _FixedSegmentRow extends ConsumerWidget {
);
}
- if (isScrubbing) {
+ if (recommendDeferredLoading) {
return _buildPlaceholder(context);
}
diff --git a/mobile/lib/presentation/widgets/timeline/scrubber.widget.dart b/mobile/lib/presentation/widgets/timeline/scrubber.widget.dart
index 635e358755..8eca9a3ab2 100644
--- a/mobile/lib/presentation/widgets/timeline/scrubber.widget.dart
+++ b/mobile/lib/presentation/widgets/timeline/scrubber.widget.dart
@@ -12,7 +12,6 @@ import 'package:immich_mobile/presentation/widgets/timeline/constants.dart';
import 'package:immich_mobile/presentation/widgets/timeline/segment.model.dart';
import 'package:immich_mobile/presentation/widgets/timeline/timeline.state.dart';
import 'package:immich_mobile/providers/haptic_feedback.provider.dart';
-import 'package:immich_mobile/utils/debounce.dart';
import 'package:intl/intl.dart' hide TextDirection;
part 'scrubber.widget.freezed.dart';
@@ -91,8 +90,6 @@ class ScrubberState extends ConsumerState<Scrubber> with TickerProviderStateMixi
bool _isDragging = false;
List<_Segment> _segments = [];
int _monthCount = 0;
- DateTime? _currentScrubberDate;
- Debouncer? _scrubberDebouncer;
late AnimationController _thumbAnimationController;
Timer? _fadeOutTimer;
@@ -147,7 +144,6 @@ class ScrubberState extends ConsumerState<Scrubber> with TickerProviderStateMixi
_thumbAnimationController.dispose();
_labelAnimationController.dispose();
_fadeOutTimer?.cancel();
- _scrubberDebouncer?.dispose();
super.dispose();
}
@@ -191,24 +187,6 @@ class ScrubberState extends ConsumerState<Scrubber> with TickerProviderStateMixi
return false;
}
- void _onScrubberDateChanged(DateTime date) {
- if (_currentScrubberDate != date) {
- // Date changed, immediately set scrubbing to true
- _currentScrubberDate = date;
- ref.read(timelineStateProvider.notifier).setScrubbing(true);
-
- // Initialize debouncer if needed
- _scrubberDebouncer ??= Debouncer(interval: const Duration(milliseconds: 50));
-
- // Debounce setting scrubbing to false
- _scrubberDebouncer!.run(() {
- if (_currentScrubberDate == date) {
- ref.read(timelineStateProvider.notifier).setScrubbing(false);
- }
- });
- }
- }
-
void _onDragStart(DragStartDetails _) {
setState(() {
_isDragging = true;
@@ -239,11 +217,6 @@ class ScrubberState extends ConsumerState<Scrubber> with TickerProviderStateMixi
if (_lastLabel != label) {
ref.read(hapticFeedbackProvider.notifier).selectionClick();
_lastLabel = label;
-
- // Notify timeline state of the new scrubber date position
- if (_monthCount >= kMinMonthsToEnableScrubberSnap) {
- _onScrubberDateChanged(nearestMonthSegment.date);
- }
}
}
@@ -351,13 +324,6 @@ class ScrubberState extends ConsumerState<Scrubber> with TickerProviderStateMixi
_isDragging = false;
});
- ref.read(timelineStateProvider.notifier).setScrubbing(false);
-
- // Reset scrubber tracking when drag ends
- _currentScrubberDate = null;
- _scrubberDebouncer?.dispose();
- _scrubberDebouncer = null;
-
_resetThumbTimer();
}
diff --git a/mobile/lib/presentation/widgets/timeline/timeline.state.dart b/mobile/lib/presentation/widgets/timeline/timeline.state.dart
index adad06b987..6727464a77 100644
--- a/mobile/lib/presentation/widgets/timeline/timeline.state.dart
+++ b/mobile/lib/presentation/widgets/timeline/timeline.state.dart
@@ -25,26 +25,44 @@ abstract class TimelineArgs with _$TimelineArgs {
}) = _TimelineArgs;
}
-@freezed
-abstract class TimelineState with _$TimelineState {
- const TimelineState._();
+class TimelineState {
+ final bool isScrolling;
- const factory TimelineState({@Default(false) bool isScrubbing, @Default(false) bool isScrolling}) = _TimelineState;
+ /// Indicates whether the timeline is scrolling beyond some configured "high" speed,
+ /// such as when programmatically scrolling to the top or a really fast user fling
+ final bool recommendDeferredLoading;
- bool get isInteracting => isScrubbing || isScrolling;
-}
+ const TimelineState({this.isScrolling = false, this.recommendDeferredLoading = false});
-class TimelineStateNotifier extends Notifier<TimelineState> {
- void setScrubbing(bool isScrubbing) {
- state = state.copyWith(isScrubbing: isScrubbing);
+ bool get isInteracting => isScrolling || recommendDeferredLoading;
+
+ @override
+ bool operator ==(covariant TimelineState other) {
+ return isScrolling == other.isScrolling && recommendDeferredLoading == other.recommendDeferredLoading;
}
+ @override
+ int get hashCode => isScrolling.hashCode ^ recommendDeferredLoading.hashCode;
+
+ TimelineState copyWith({bool? isScrolling, bool? recommendDeferredLoading}) {
+ return TimelineState(
+ isScrolling: isScrolling ?? this.isScrolling,
+ recommendDeferredLoading: recommendDeferredLoading ?? this.recommendDeferredLoading,
+ );
+ }
+}
+
+class TimelineStateNotifier extends Notifier<TimelineState> {
void setScrolling(bool isScrolling) {
state = state.copyWith(isScrolling: isScrolling);
}
+ void setRecommendDeferredLoading(bool recommendDeferredLoading) {
+ state = state.copyWith(recommendDeferredLoading: recommendDeferredLoading);
+ }
+
@override
- TimelineState build() => const TimelineState(isScrubbing: false, isScrolling: false);
+ TimelineState build() => const TimelineState(isScrolling: false, recommendDeferredLoading: false);
}
// This provider watches the buckets from the timeline service & args and serves the segments.
diff --git a/mobile/lib/presentation/widgets/timeline/timeline.widget.dart b/mobile/lib/presentation/widgets/timeline/timeline.widget.dart
index 868edba64d..d0ff5c60d7 100644
--- a/mobile/lib/presentation/widgets/timeline/timeline.widget.dart
+++ b/mobile/lib/presentation/widgets/timeline/timeline.widget.dart
@@ -27,6 +27,7 @@ import 'package:immich_mobile/providers/infrastructure/settings.provider.dart';
import 'package:immich_mobile/providers/infrastructure/timeline.provider.dart';
import 'package:immich_mobile/providers/timeline/multiselect.provider.dart';
import 'package:immich_mobile/routing/app_navigation_observer.dart';
+import 'package:immich_mobile/utils/debounce.dart';
import 'package:immich_mobile/widgets/common/immich_sliver_app_bar.dart';
import 'package:immich_mobile/widgets/common/mesmerizing_sliver_app_bar.dart';
import 'package:immich_mobile/widgets/common/selection_sliver_app_bar.dart';
@@ -157,6 +158,8 @@ class _SliverTimelineState extends ConsumerState<_SliverTimeline> with WidgetsBi
double _baseScaleFactor = 3.0;
int? _restoreAssetIndex;
+ final Debouncer _fastScrollDebouncer = Debouncer(interval: const Duration(milliseconds: 100));
+
@override
void initState() {
super.initState();
@@ -265,27 +268,39 @@ class _SliverTimelineState extends ConsumerState<_SliverTimeline> with WidgetsBi
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
+ _fastScrollDebouncer.dispose();
_scrollController.dispose();
unawaited(_eventSubscription?.cancel());
super.dispose();
}
+ /// Track whether the timeline is moving fast enough to defer per-row asset loading
+ bool _onScrollVelocityNotification(ScrollNotification notification) {
+ // Only consider the primary timeline ScrollView (no nested views) and update events
+ if (notification.depth != 0 || notification is! ScrollUpdateNotification) {
+ return false;
+ }
+
+ // Use Flutter's built in fast velocity tracking
+ if (_scrollController.position.recommendDeferredLoading(context)) {
+ ref.read(timelineStateProvider.notifier).setRecommendDeferredLoading(true);
+
+ // We cannot rely on scroll end events, as the timeline scrubber jumps from position
+ // to position, resulting in large spikes in velocity followed by low velocity
+ _fastScrollDebouncer.run(() => ref.read(timelineStateProvider.notifier).setRecommendDeferredLoading(false));
+ }
+ return false;
+ }
+
void _scrollToTop() {
if (!_scrollController.hasClients) {
return;
}
- final timelineState = ref.read(timelineStateProvider.notifier);
- timelineState.setScrubbing(true);
- unawaited(
- _scrollController
- .animateTo(0, duration: const Duration(milliseconds: 250), curve: Curves.easeInOut)
- .whenComplete(() => timelineState.setScrubbing(false)),
- );
+ _scrollController.animateTo(0, duration: const Duration(milliseconds: 250), curve: Curves.easeInOut);
}
void _scrollToDate(DateTime date) {
- final timelineState = ref.read(timelineStateProvider.notifier);
final asyncSegments = ref.read(timelineSegmentProvider);
asyncSegments.whenData((segments) {
// Find the segment that contains assets from the target date
@@ -312,18 +327,11 @@ class _SliverTimelineState extends ConsumerState<_SliverTimeline> with WidgetsBi
if (fallbackSegment != null) {
// Scroll to the segment with a small offset to show the header
final targetOffset = fallbackSegment.startOffset - 50;
- timelineState.setScrubbing(true);
- unawaited(
- _scrollController
- .animateTo(
- targetOffset.clamp(0.0, _scrollController.position.maxScrollExtent),
- duration: const Duration(milliseconds: 500),
- curve: Curves.easeInOut,
- )
- .whenComplete(() => timelineState.setScrubbing(false)),
+ _scrollController.animateTo(
+ targetOffset.clamp(0.0, _scrollController.position.maxScrollExtent),
+ duration: const Duration(milliseconds: 500),
+ curve: Curves.easeInOut,
);
- } else {
- timelineState.setScrubbing(false);
}
});
}
@@ -526,7 +534,10 @@ class _SliverTimelineState extends ConsumerState<_SliverTimeline> with WidgetsBi
child: Stack(
clipBehavior: Clip.none,
children: [
- timeline,
+ NotificationListener<ScrollNotification>(
+ onNotification: _onScrollVelocityNotification,
+ child: timeline,
+ ),
if (isBottomWidgetVisible)
Positioned(
top: MediaQuery.paddingOf(context).top,