mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
fix timeline not updated on orientation change
This commit is contained in:
@@ -57,6 +57,12 @@ class _AlbumSelectorState extends ConsumerState<AlbumSelector> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
ref.listenManual(
|
||||
remoteAlbumProvider.select((state) => state.albums),
|
||||
(_, _) => sortAlbums(),
|
||||
fireImmediately: true,
|
||||
);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
final appSettings = ref.read(appSettingsServiceProvider);
|
||||
final savedSortMode = appSettings.getSetting(AppSettingsEnum.selectedAlbumSortOrder);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/domain/models/setting.model.dart';
|
||||
import 'package:immich_mobile/domain/models/timeline.model.dart';
|
||||
@@ -48,6 +49,26 @@ class TimelineArgs {
|
||||
showStorageIndicator.hashCode ^
|
||||
withStack.hashCode ^
|
||||
groupBy.hashCode;
|
||||
|
||||
TimelineArgs copyWith({
|
||||
double? maxWidth,
|
||||
double? maxHeight,
|
||||
double? spacing,
|
||||
int? columnCount,
|
||||
bool? showStorageIndicator,
|
||||
bool? withStack,
|
||||
GroupAssetsBy? groupBy,
|
||||
}) {
|
||||
return TimelineArgs(
|
||||
maxWidth: maxWidth ?? this.maxWidth,
|
||||
maxHeight: maxHeight ?? this.maxHeight,
|
||||
spacing: spacing ?? this.spacing,
|
||||
columnCount: columnCount ?? this.columnCount,
|
||||
showStorageIndicator: showStorageIndicator ?? this.showStorageIndicator,
|
||||
withStack: withStack ?? this.withStack,
|
||||
groupBy: groupBy ?? this.groupBy,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class TimelineState {
|
||||
@@ -86,25 +107,37 @@ class TimelineStateNotifier extends Notifier<TimelineState> {
|
||||
|
||||
// This provider watches the buckets from the timeline service & args and serves the segments.
|
||||
// It should be used only after the timeline service and timeline args provider is overridden
|
||||
final timelineSegmentProvider = StreamProvider.autoDispose<List<Segment>>((ref) async* {
|
||||
final args = ref.watch(timelineArgsProvider);
|
||||
final columnCount = args.columnCount;
|
||||
final spacing = args.spacing;
|
||||
final availableTileWidth = args.maxWidth - (spacing * (columnCount - 1));
|
||||
final tileExtent = math.max(0, availableTileWidth) / columnCount;
|
||||
final timelineSegmentProvider = StreamNotifierProvider<_TimelineSegmentNotifier, List<Segment>>(
|
||||
_TimelineSegmentNotifier.new,
|
||||
dependencies: [timelineServiceProvider, timelineArgsProvider],
|
||||
);
|
||||
|
||||
final groupBy = args.groupBy ?? GroupAssetsBy.values[ref.watch(settingsProvider).get(Setting.groupAssetsBy)];
|
||||
class _TimelineSegmentNotifier extends StreamNotifier<List<Segment>> {
|
||||
@override
|
||||
Stream<List<Segment>> build() async* {
|
||||
final args = ref.watch(timelineArgsProvider);
|
||||
final columnCount = args.columnCount;
|
||||
final spacing = args.spacing;
|
||||
final availableTileWidth = args.maxWidth - (spacing * (columnCount - 1));
|
||||
final tileExtent = math.max(0, availableTileWidth) / columnCount;
|
||||
final groupBy = args.groupBy ?? GroupAssetsBy.values[ref.watch(settingsProvider).get(Setting.groupAssetsBy)];
|
||||
final timelineService = ref.watch(timelineServiceProvider);
|
||||
yield* timelineService.watchBuckets().map((buckets) {
|
||||
return FixedSegmentBuilder(
|
||||
buckets: buckets,
|
||||
tileHeight: tileExtent,
|
||||
columnCount: columnCount,
|
||||
spacing: spacing,
|
||||
groupBy: groupBy,
|
||||
).generate();
|
||||
});
|
||||
}
|
||||
|
||||
final timelineService = ref.watch(timelineServiceProvider);
|
||||
yield* timelineService.watchBuckets().map((buckets) {
|
||||
return FixedSegmentBuilder(
|
||||
buckets: buckets,
|
||||
tileHeight: tileExtent,
|
||||
columnCount: columnCount,
|
||||
spacing: spacing,
|
||||
groupBy: groupBy,
|
||||
).generate();
|
||||
});
|
||||
}, dependencies: [timelineServiceProvider, timelineArgsProvider]);
|
||||
@override
|
||||
bool updateShouldNotify(AsyncValue<List<Segment>> previous, AsyncValue<List<Segment>> next) {
|
||||
final listEquals = const DeepCollectionEquality().equals;
|
||||
return !listEquals(previous.value, next.value);
|
||||
}
|
||||
}
|
||||
|
||||
final timelineStateProvider = NotifierProvider<TimelineStateNotifier, TimelineState>(TimelineStateNotifier.new);
|
||||
|
||||
@@ -71,10 +71,9 @@ class Timeline extends StatelessWidget {
|
||||
builder: (_, constraints) => ProviderScope(
|
||||
overrides: [
|
||||
timelineArgsProvider.overrideWith(
|
||||
(ref) => TimelineArgs(
|
||||
maxWidth: constraints.maxWidth,
|
||||
maxHeight: constraints.maxHeight,
|
||||
columnCount: ref.watch(settingsProvider.select((s) => s.get(Setting.tilesPerRow))),
|
||||
() => TimelineArgsNotifier(
|
||||
initialMaxWidth: constraints.maxWidth,
|
||||
initialMaxHeight: constraints.maxHeight,
|
||||
showStorageIndicator: showStorageIndicator,
|
||||
withStack: withStack,
|
||||
groupBy: groupBy,
|
||||
@@ -92,6 +91,7 @@ class Timeline extends StatelessWidget {
|
||||
persistentBottomBar: persistentBottomBar,
|
||||
snapToMonth: snapToMonth,
|
||||
maxWidth: constraints.maxWidth,
|
||||
maxHeight: constraints.maxHeight,
|
||||
loadingWidget: loadingWidget,
|
||||
),
|
||||
),
|
||||
@@ -122,6 +122,7 @@ class _SliverTimeline extends ConsumerStatefulWidget {
|
||||
this.persistentBottomBar = false,
|
||||
this.snapToMonth = true,
|
||||
this.maxWidth,
|
||||
this.maxHeight,
|
||||
this.loadingWidget,
|
||||
});
|
||||
|
||||
@@ -134,6 +135,7 @@ class _SliverTimeline extends ConsumerStatefulWidget {
|
||||
final bool persistentBottomBar;
|
||||
final bool snapToMonth;
|
||||
final double? maxWidth;
|
||||
final double? maxHeight;
|
||||
final Widget? loadingWidget;
|
||||
|
||||
@override
|
||||
@@ -172,13 +174,21 @@ class _SliverTimelineState extends ConsumerState<_SliverTimeline> {
|
||||
@override
|
||||
void didUpdateWidget(covariant _SliverTimeline oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.maxWidth != oldWidget.maxWidth) {
|
||||
final asyncSegments = ref.read(timelineSegmentProvider);
|
||||
asyncSegments.whenData((segments) {
|
||||
final index = _getCurrentAssetIndex(segments);
|
||||
// Refresh to wait for new segments to be generated with the updated width before restoring the scroll position
|
||||
final _ = ref.refresh(timelineArgsProvider);
|
||||
_restoreAssetIndex = index;
|
||||
if (widget.maxWidth != oldWidget.maxWidth || widget.maxHeight != oldWidget.maxHeight) {
|
||||
final segments = ref.read(timelineSegmentProvider).value;
|
||||
int? restoreAssetIndex;
|
||||
if (segments != null) {
|
||||
restoreAssetIndex = _getCurrentAssetIndex(segments);
|
||||
}
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
ref
|
||||
.read(timelineArgsProvider.notifier)
|
||||
.updateConstraints(maxWidth: widget.maxWidth!, maxHeight: widget.maxHeight!);
|
||||
final _ = ref.refresh(timelineSegmentProvider);
|
||||
_restoreAssetIndex = restoreAssetIndex;
|
||||
_restoreAssetPosition(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -200,26 +210,40 @@ class _SliverTimelineState extends ConsumerState<_SliverTimeline> {
|
||||
}
|
||||
}
|
||||
|
||||
void _restorePosition(List<Segment> segments) {
|
||||
final targetSegment = segments.lastWhereOrNull((segment) => segment.firstAssetIndex <= _restoreAssetIndex!);
|
||||
if (targetSegment == null) {
|
||||
_restoreAssetIndex = null;
|
||||
return;
|
||||
}
|
||||
final assetIndexInSegment = _restoreAssetIndex! - targetSegment.firstAssetIndex;
|
||||
final newColumnCount = ref.read(timelineArgsProvider).columnCount;
|
||||
final rowIndexInSegment = (assetIndexInSegment / newColumnCount).floor();
|
||||
final targetRowIndex = targetSegment.firstIndex + 1 + rowIndexInSegment;
|
||||
final targetOffset = targetSegment.indexToLayoutOffset(targetRowIndex);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted && _scrollController.hasClients) {
|
||||
_scrollController.jumpTo(targetOffset.clamp(0.0, _scrollController.position.maxScrollExtent));
|
||||
}
|
||||
});
|
||||
_restoreAssetIndex = null;
|
||||
}
|
||||
|
||||
void _restoreAssetPosition(_) {
|
||||
if (_restoreAssetIndex == null) return;
|
||||
|
||||
final asyncSegments = ref.read(timelineSegmentProvider);
|
||||
asyncSegments.whenData((segments) {
|
||||
final targetSegment = segments.lastWhereOrNull((segment) => segment.firstAssetIndex <= _restoreAssetIndex!);
|
||||
if (targetSegment != null) {
|
||||
final assetIndexInSegment = _restoreAssetIndex! - targetSegment.firstAssetIndex;
|
||||
final newColumnCount = ref.read(timelineArgsProvider).columnCount;
|
||||
final rowIndexInSegment = (assetIndexInSegment / newColumnCount).floor();
|
||||
final targetRowIndex = targetSegment.firstIndex + 1 + rowIndexInSegment;
|
||||
final targetOffset = targetSegment.indexToLayoutOffset(targetRowIndex);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
_scrollController.jumpTo(targetOffset.clamp(0.0, _scrollController.position.maxScrollExtent));
|
||||
}
|
||||
});
|
||||
if (asyncSegments is AsyncData<List<Segment>>) {
|
||||
_restorePosition(asyncSegments.value);
|
||||
return;
|
||||
}
|
||||
late ProviderSubscription<AsyncValue<List<Segment>>> sub;
|
||||
sub = ref.listenManual<AsyncValue<List<Segment>>>(timelineSegmentProvider, (_, next) {
|
||||
if (next is AsyncData<List<Segment>>) {
|
||||
sub.close();
|
||||
_restorePosition(next.value);
|
||||
}
|
||||
});
|
||||
_restoreAssetIndex = null;
|
||||
}
|
||||
|
||||
void _onMultiSelectionToggled(_, bool isEnabled) {
|
||||
|
||||
Reference in New Issue
Block a user