Fix favorite/watched toggle appearing to no-op on repeat taps - #84
Merged
Conversation
Two compounding bugs on asset detail pages' Favorite/Watched buttons: 1. HeroActionButtons' toolbar-hosted Button/Menu-row action closures could keep firing with the MediaItem snapshot captured whenever that toolbar content was first built, not the latest render — confirmed live: the icon itself updated correctly after a tap, but the *next* tap's currentlyFavorite still read the pre-first-tap value, silently repeating the same write instead of reversing it. Fixed by having the toggle read current status through AssetDetailViewModel directly (currentFavoriteWatchedStatus(forItemID:)) at the moment it fires, rather than trusting the closure's own captured value. 2. Jellyfin's favorite/watched write endpoints return success immediately but commit the userData change asynchronously — confirmed live that this can take several *minutes* on a real server, an order of magnitude past the ~13s confirmation-poll budget. When that happened, the poll kept patching `item` with the still-stale fetched value on every attempt and gave up showing it — indistinguishable from the tap having done nothing. Fixed with an optimistic update (MediaItem.withOptimisticFavoriteWatched, applied the moment the write itself succeeds) plus tightening the poll to only ever adopt a confirmed fetch, so it can no longer regress the optimistic value back to stale data mid-poll. Also added the Menu-row (Show-content page) equivalent of the plain button's .disabled(isPending) guard, closing a related gap where re-opening the menu mid-toggle could fire a second concurrent write. Root-caused in the Simulator against a real Jellyfin server, confirming server-side truth via curl at each step rather than trusting the UI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On asset detail pages, tapping Favorite/Watched once worked, but a second tap looked like it did nothing — no icon change, and no way to tell if the server got the update.
Root cause was two compounding bugs, found by reproducing live in the Simulator against a real Jellyfin server and checking server-side truth via
curlat each step (not just what the UI showed):Stale closure in the toolbar button.
HeroActionButtonsis hosted as aToolbarItem. Its displayed icon reliably tracks the view model reactively, but itsButton/Menurow action closures could keep firing with theMediaItemsnapshot captured whenever that toolbar content was first built — not the latest render. Confirmed directly: the icon visibly flipped after tap 1, but tap 2'scurrentlyFavoritestill read the value from before tap 1, so it silently repeated the same write instead of reversing it.Fixed by having the toggle look up current status through
AssetDetailViewModel(currentFavoriteWatchedStatus(forItemID:)) fresh at the moment it fires, rather than trusting the closure's captured value.Slow server-side commit outlasting the confirmation poll. Jellyfin's favorite/watched write endpoints return success immediately but commit the actual userData change asynchronously. Confirmed live that this can take several minutes on a real server — an order of magnitude past the app's ~13s confirmation-poll budget. When that happened, the poll kept patching
itemwith the still-stale fetched value on every attempt, so once it gave up, the button looked exactly like the tap had done nothing.Fixed with an optimistic update (
MediaItem.withOptimisticFavoriteWatched, applied the moment the write itself succeeds) plus tightening the poll to only ever adopt a confirmed fetch — it can no longer regress the optimistic value back to stale data mid-poll.Also closed a related gap: the Show-content page's Menu rows (Show/Season/Episode) didn't have the plain button's
.disabled(isPending)guard, so re-opening the menu mid-toggle could fire a second, concurrent write.Testing
AssetDetailViewModelTestspin the optimistic-update-survives-an-unconfirmed-poll behavior and the newcurrentFavoriteWatchedStatus(forItemID:)lookup.MediaItemTestscoverwithOptimisticFavoriteWatched.curlafter each tap).🤖 Generated with Claude Code