perf: implement batched UI updates and add watchlist support#130
Merged
Conversation
This commit introduces a debouncing mechanism for media and user data updates to improve UI performance and reduce jank during high-frequency WebSocket traffic. It also adds a new `likes` field to the user data schema to support watchlist functionality and refactors local database patching.
### Key Changes:
* **UI Performance & Batching**:
* **ViewModel Debouncing**: Implemented `MutableSharedFlow` triggers with `debounce` (300ms–500ms) in `HomeViewModel`, `SearchViewModel`, `ItemDetailViewModel`, `LibraryContentViewModel`, and `GenreResultsViewModel`. This batches incoming WebSocket updates into single UI state changes instead of triggering a recomposition for every individual item update.
* **Live TV Optimization (`LiveTvViewModel.kt`)**: Added a `tabSwitchTrigger` with a 200ms debounce to prevent rapid-fire data reloading when quickly switching between Home, Guide, and Channel tabs.
* **Home Screen Refresh**: Introduced `layoutRefreshTrigger` in `HomeViewModel` to consolidate dynamic section updates and avoid redundant layout calculations.
* **Data & Persistence**:
* **Database Schema (`AfinityDatabase.kt`, `DatabaseMigrations.kt`)**: Bumped database version to `44` and added a migration to include the `likes` column in the `userdata` table.
* **Local Patching (`UserDataDao.kt`, `DatabaseRepository.kt`)**: Added `patchUserDataLocally` to allow direct updates to specific fields (played, favorite, likes, position) in the local database without requiring a full metadata refresh.
* **Media Change Management (`MediaChangeManager.kt`)**: Refactored `applyUserDataChangesBatch` to use the new local patching mechanism. Introduced a 1-second debounce for refreshing "Next Up" and "Continue Watching" sections after WebSocket syncs finish.
* **Item Detail Enhancements (`ItemDetailViewModel.kt`)**:
* Implemented manual unplayed count arithmetic in the UI state for Episodes and Seasons to provide immediate visual feedback upon status changes while a background sync is pending.
* Added `backgroundSyncTrigger` to debounce network synchronization after local state updates.
* **Player & Library Fixes**:
* **Audiobookshelf (`AudiobookshelfPlayer.kt`)**: Improved `MediaController` release logic by properly canceling the `controllerFuture` to prevent potential leaks or crashes during rapid playback lifecycle changes.
* **Favorites & Watchlist**: Refined `loadFavorites` and `loadWatchlist` in their respective ViewModels to prevent unnecessary reloads if data is already present in the UI state.
* **Logging**: Reduced verbosity in `NetworkModule.kt` by filtering out standard HTTP request/response headers from the logs.
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.
This commit introduces a debouncing mechanism for media and user data updates to improve UI performance and reduce jank during high-frequency WebSocket traffic. It also adds a new
likesfield to the user data schema to support watchlist functionality and refactors local database patching.Key Changes:
UI Performance & Batching:
MutableSharedFlowtriggers withdebounce(300ms–500ms) inHomeViewModel,SearchViewModel,ItemDetailViewModel,LibraryContentViewModel, andGenreResultsViewModel. This batches incoming WebSocket updates into single UI state changes instead of triggering a recomposition for every individual item update.LiveTvViewModel.kt): Added atabSwitchTriggerwith a 200ms debounce to prevent rapid-fire data reloading when quickly switching between Home, Guide, and Channel tabs.layoutRefreshTriggerinHomeViewModelto consolidate dynamic section updates and avoid redundant layout calculations.Data & Persistence:
AfinityDatabase.kt,DatabaseMigrations.kt): Bumped database version to44and added a migration to include thelikescolumn in theuserdatatable.UserDataDao.kt,DatabaseRepository.kt): AddedpatchUserDataLocallyto allow direct updates to specific fields (played, favorite, likes, position) in the local database without requiring a full metadata refresh.MediaChangeManager.kt): RefactoredapplyUserDataChangesBatchto use the new local patching mechanism. Introduced a 1-second debounce for refreshing "Next Up" and "Continue Watching" sections after WebSocket syncs finish.Item Detail Enhancements (
ItemDetailViewModel.kt):backgroundSyncTriggerto debounce network synchronization after local state updates.Player & Library Fixes:
AudiobookshelfPlayer.kt): ImprovedMediaControllerrelease logic by properly canceling thecontrollerFutureto prevent potential leaks or crashes during rapid playback lifecycle changes.loadFavoritesandloadWatchlistin their respective ViewModels to prevent unnecessary reloads if data is already present in the UI state.NetworkModule.ktby filtering out standard HTTP request/response headers from the logs.