Skip to content

fix: remember scroll position per folder (#104) - #118

Closed
joelkanyi wants to merge 1 commit into
mainfrom
fix/folder-scroll-memory
Closed

fix: remember scroll position per folder (#104)#118
joelkanyi wants to merge 1 commit into
mainfrom
fix/folder-scroll-memory

Conversation

@joelkanyi

Copy link
Copy Markdown
Collaborator

Closes #104.

When navigating in/out of nested folders, the screen now restores the scroll position the user left, per folder path. Each folder remembers its own position so deep trees feel natural to navigate.

How it works

FoldersViewModel holds a Map<String, (index, offset)> keyed by folder path. The FoldersAndTracks composable runs a LaunchedEffect on currentFolder.path that:

  1. Looks up the saved position for the path.
  2. If found, waits until the lazy column has at least one item rendered, then scrollToItem(index, offset).
  3. Then starts collecting scroll changes from lazyColumnState and saves them back through the VM.

The order matters. If save monitoring started immediately, the initial (0, 0) from the freshly-rebuilt paging source would overwrite the real saved value before we could restore it. .drop(1) on the snapshotFlow skips that first emission.

Path normalization

Folder.path carries a trailing slash for some paths (/home/cwilvx/Music/) and not for others (/home/cwilvx/Music). The VM already trims trailing slashes when matching breadcrumb entries, but the raw Folder.path on _currentFolder varies depending on entry path. Without normalization in our scroll map the same folder would key two entries, and a back-nav would miss the save. The fix calls trimEnd('/') on both save and lookup.

Tested on Pixel 7 Pro

Verified all the cases:

  • Scroll deep in a folder, enter a subfolder, back. Restores.
  • Multi-level: A → A/B → A/B/C, then back-back-back. Each level restores its own position.
  • Switch tabs (Folders → Albums → Folders). Position survives.
  • Mixed breadcrumb back-nav + tap-album-then-back. Both paths use the same map.

Diag logs during dev confirmed each restore start had the right (index, offset) and restore done fired immediately after items loaded.

Keep a per-path map of (firstVisibleIndex, firstVisibleOffset) in FoldersViewModel.
When the LazyColumn observes a folder change, wait for items to load then restore
the saved position if any. Subsequent scroll changes save back to the map.

Path is normalized with trimEnd('/') on both save and lookup. The VM already
normalizes when matching breadcrumb entries, but the raw Folder.path varies
(some paths carry a trailing slash, others don't), which would otherwise key
two distinct entries for the same folder and break restore on back-nav.
@joelkanyi

Copy link
Copy Markdown
Collaborator Author

Superseded by #120, which combines scroll memory with per-path Flow caching. Closing in favor of the combined PR.

@joelkanyi joelkanyi closed this Jun 10, 2026
@joelkanyi
joelkanyi deleted the fix/folder-scroll-memory branch June 10, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request] Remember position of last opened tab

1 participant