Skip to content

perf: implement batched UI updates and add watchlist support#130

Merged
MakD merged 1 commit into
masterfrom
websocket-update
May 21, 2026
Merged

perf: implement batched UI updates and add watchlist support#130
MakD merged 1 commit into
masterfrom
websocket-update

Conversation

@MakD

@MakD MakD commented May 21, 2026

Copy link
Copy Markdown
Owner

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 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.
@MakD MakD merged commit da13595 into master May 21, 2026
1 check failed
@MakD MakD deleted the websocket-update branch May 21, 2026 18:58
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.

1 participant