Library, playlist, and preference workflows
The library workflow turns saved media and account data into live app views. It joins the local database with YouTube Music sync, while DataStore preferences control filters, order, visibility, and view state. The same model supports songs, albums, artists, playlists, podcasts, listening history, and account settings.
Library views
The library presents one entry point for saved playlists, songs, albums, artists, podcasts, or a combined mix. Each view model observes database data and preference changes, then exposes a state flow that the screen can render and search. The combined mix also joins saved songs with songs found in bookmarked playlists so the user can treat both sources as library content. [app/src/main/kotlin/com/metrolist/music/ui/screens/library/LibraryScreen.kt#L26-L82] [app/src/main/kotlin/com/metrolist/music/viewmodels/LibraryViewModels.kt#L74-L127] [app/src/main/kotlin/com/metrolist/music/viewmodels/LibraryViewModels.kt#L337-L407]
Saved media views
Saved media views keep local data useful without requiring a network request for each screen load. A view applies the user's chosen source, sort order, direction, and content filters to database results. Search then narrows the current result set, while limited remote refreshes fill missing artist or album details. [app/src/main/kotlin/com/metrolist/music/viewmodels/LibraryViewModels.kt#L129-L263] [app/src/main/kotlin/com/metrolist/music/db/DatabaseDao.kt#L157-L243] [app/src/main/kotlin/com/metrolist/music/db/DatabaseDao.kt#L850-L872]
References: app/…/LibraryViewModels.kt• L129-263 app/…/DatabaseDao.kt• L157-243 app/…/DatabaseDao.kt• L850-872
Podcast views
Podcast views combine locally saved or downloaded episodes with subscribed podcast channels. Channel data may come from the account service or local artist bookmarks, and duplicate channel IDs resolve in favor of the account result. The view also exposes service playlists for saved episodes and new episodes, then refreshes them with podcast sync. [app/src/main/kotlin/com/metrolist/music/viewmodels/LibraryViewModels.kt#L452-L599]
References: app/…/LibraryViewModels.kt• L452-599 app/…/DatabaseDao.kt• L1337-1382 app/…/DatabaseDao.kt• L2019-2033
Account synchronization
Account synchronization makes YouTube Music the source for remote saved state and stores a local copy for fast display. SyncUtils accepts named sync operations, skips account work when no authenticated account exists, and protects direct sync runs with one execution lock. Full sync also has a cooldown so repeated refresh actions do not create unnecessary remote work. [app/src/main/kotlin/com/metrolist/music/utils/SyncUtils.kt#L59-L77] [app/src/main/kotlin/com/metrolist/music/utils/SyncUtils.kt#L470-L486]
The sync path follows this broad sequence:
flowchart LR
A[User refresh or app sync trigger] --> B[Sync operation]
B --> C[Remote account data]
C --> D[Compare remote and local state]
D --> E[Update local database]
E --> F[View model state flows]
F --> G[Library and playlist screens]
Saved item synchronization
Song, album, artist, and playlist syncs update membership flags and metadata in the local database. A remote item that was previously seen but is now absent can lose its saved membership, while local-only items are kept until the remote state is known to be complete. This protects local additions from a partial or failed remote response. [app/src/main/kotlin/com/metrolist/music/utils/SyncUtils.kt#L79-L93] [app/src/main/kotlin/com/metrolist/music/utils/SyncUtils.kt#L798-L913] [app/src/main/kotlin/com/metrolist/music/utils/SyncUtils.kt#L980-L1169] [app/src/main/kotlin/com/metrolist/music/utils/SyncUtils.kt#L1456-L1536]
References: app/…/SyncUtils.kt• L79-93 app/…/SyncUtils.kt• L798-913 app/…/SyncUtils.kt• L980-1169 app/…/SyncUtils.kt• L1456-1536
Playlist synchronization
Playlist synchronization keeps both the playlist record and its ordered song membership aligned with the remote playlist. It avoids replacing a playlist during an active local removal, rejects an empty response when the remote service still reports songs, and preserves local entries that have not yet received remote identity data. Auto-sync applies the same process to playlists marked for ongoing refresh. [app/src/main/kotlin/com/metrolist/music/utils/SyncUtils.kt#L1538-L1637]
References: app/…/SyncUtils.kt• L1538-1637 app/…/SyncUtils.kt• L59-76
Podcast synchronization
Podcast synchronization imports subscribed shows, subscribed channels, and saved episodes into the same local library model. The remote service remains authoritative for saved episode membership, so episodes absent from the remote “for later” list lose their local library flag. Podcast screens can request a full refresh or react to a delayed refresh trigger after a subscribe or unsubscribe action. [app/src/main/kotlin/com/metrolist/music/utils/SyncUtils.kt#L1172-L1346] [app/src/main/kotlin/com/metrolist/music/utils/SyncUtils.kt#L1348-L1454] [app/src/main/kotlin/com/metrolist/music/viewmodels/LibraryViewModels.kt#L555-L599]
References: app/…/SyncUtils.kt• L1172-1346 app/…/SyncUtils.kt• L1348-1454 app/…/LibraryViewModels.kt• L555-599
Playlist workflows
Playlist workflows support three linked states: a saved playlist record, its ordered local song maps, and an optional remote playlist. The playlist list view exposes saved records for selection, while the detail view exposes songs and edit actions. Changes to local order or membership can also be sent back to YouTube Music when the playlist has a remote identity. [app/src/main/kotlin/com/metrolist/music/viewmodels/PlaylistsViewModel.kt#L30-L51] [app/src/main/kotlin/com/metrolist/music/viewmodels/LocalPlaylistViewModel.kt#L40-L117]
Playlist detail state
The detail view model combines playlist songs with per-playlist sort and video visibility preferences. Custom order is the stable editing order; other modes present a derived order for reading and playback. When the playlist has a remote browse ID, the view model also loads remote header data so the screen can show account-side details. [app/src/main/kotlin/com/metrolist/music/viewmodels/LocalPlaylistViewModel.kt#L48-L117]
References: app/…/LocalPlaylistViewModel.kt• L40-117 app/…/DatabaseDao.kt• L261-282
Playlist edits
Playlist edits update local state first so the screen responds at once, then use the remote playlist identity when a matching account playlist exists. Rename and delete affect both sides when possible. Reorder and remove actions preserve local position data and send the matching remote song identity so the service can apply the same change. [app/src/main/kotlin/com/metrolist/music/ui/screens/playlist/LocalPlaylistScreen.kt#L286-L317] [app/src/main/kotlin/com/metrolist/music/ui/screens/playlist/LocalPlaylistScreen.kt#L438-L461] [app/src/main/kotlin/com/metrolist/music/ui/screens/playlist/LocalPlaylistScreen.kt#L559-L579]
References: app/…/LocalPlaylistScreen.kt• L286-317 app/…/LocalPlaylistScreen.kt• L438-461 app/…/LocalPlaylistScreen.kt• L559-579
History views
History offers a local playback view and an authenticated remote YouTube Music view. Local history comes from recorded events and is grouped into today, yesterday, this week, last week, or month buckets with duplicate songs removed inside each bucket. Remote history is fetched on demand and remains separate from local events. [app/src/main/kotlin/com/metrolist/music/viewmodels/HistoryViewModel.kt#L36-L105] [app/src/main/kotlin/com/metrolist/music/db/DatabaseDao.kt#L1480-L1516]
The screen selects the source only when the account state permits remote history:
flowchart TD
A[History screen] --> B{Source}
B -->|Local| C[Database events]
C --> D[Date groups and filters]
B -->|Remote and signed in| E[YouTube Music history]
E --> F[Remote sections and filters]
D --> G[Playback and item actions]
F --> G
References: app/…/HistoryViewModel.kt• L42-105 app/…/DatabaseDao.kt• L1480-1516 app/…/HistoryScreen.kt• L134-246
Preference-driven view models
Preferences are a shared contract between settings screens and library view models. DataStore stores the user's choices, and view models observe those choices so a change updates the visible state without rebuilding the library data by hand. The relevant settings include source filters, sort modes, sort direction, hidden video or explicit content, view type, and full-sync timing. [app/src/main/kotlin/com/metrolist/music/constants/PreferenceKeys.kt#L93-L105] [app/src/main/kotlin/com/metrolist/music/constants/PreferenceKeys.kt#L215-L245] [app/src/main/kotlin/com/metrolist/music/constants/PreferenceKeys.kt#L263-L348]
The main preference effects are:
| Preference group | User-visible result |
|---|---|
| Library source filters | Shows library, liked, downloaded, or uploaded media where supported |
| Sort and direction | Changes order for songs, albums, artists, playlists, and playlist songs |
| Content visibility | Hides explicit songs, video songs, or YouTube Shorts |
| View type | Switches library collections between list and grid presentation |
| Sync markers | Records the last full sync and limits repeated full refreshes |
References: app/…/PreferenceKeys.kt• L93-105 app/…/PreferenceKeys.kt• L215-245 app/…/PreferenceKeys.kt• L263-348 app/…/LibraryViewModels.kt• L93-114
Account data lifecycle
Account settings control whether remote saved data can continue to affect the local library. Logout may keep local data for offline use, or it may forget the account before clearing the library. Clearing the account first prevents background sync from seeing an empty database and adding remote data back during cleanup. [app/src/main/kotlin/com/metrolist/music/viewmodels/AccountSettingsViewModel.kt#L35-L73]
Credential changes are saved as one DataStore update before the app restarts. This gives later sync operations a complete account identity instead of a partial set of credentials. [app/src/main/kotlin/com/metrolist/music/viewmodels/AccountSettingsViewModel.kt#L75-L110]
References: app/…/AccountSettingsViewModel.kt• L35-73 app/…/AccountSettingsViewModel.kt• L75-110 app/…/SyncUtils.kt• L798-802
Change checklist
When these files or sections are changed, remember to consider these:
- Keep each library preference key aligned with the view model that reads it and the screen that writes it.
- Keep remote membership cleanup tied to complete-response checks or prior remote snapshots.
- Preserve playlist position and remote song identity together when changing playlist order or membership.
- Keep account clearing ahead of database clearing so sync jobs cannot repopulate removed data.
- Keep podcast refresh triggers coordinated with the delayed remote update after subscribe or unsubscribe actions.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Remote saved-list response is incomplete | Valid local saved items may be removed | Remove remote membership only after a complete response or known remote snapshot |
| Playlist fetch is empty while the service reports songs | A valid local playlist could be erased | Reject the empty result unless it is a genuine empty playlist |
| Account is forgotten after local data is cleared | Background sync can restore account data during cleanup | Clear account credentials before clearing library data |
| Playlist reorder lacks the remote set-song identity | Local and remote order can diverge | Retain playlist map identity and send the matching remote successor |
| Remote history is requested without account credentials | The history view has no usable remote data | Expose remote history only when the account is authenticated |
Key files
| File | Why to read it |
|---|---|
| app/…/LibraryViewModels.kt | Main library state, filters, refresh actions, podcast views, and combined library mix |
| app/…/SyncUtils.kt | Remote-to-local sync contract for saved media, playlists, podcasts, and account cleanup |
| app/…/DatabaseDao.kt | Local queries and ordered flows consumed by library and history view models |
| app/…/LocalPlaylistViewModel.kt | Playlist detail state, song order, filters, and remote header loading |
| app/…/LocalPlaylistScreen.kt | User-facing playlist edit flow and local-to-remote edit propagation |
| app/…/HistoryViewModel.kt | Local history grouping and remote history loading |
| app/…/PreferenceKeys.kt | Shared preference names and library sort, filter, and view contracts |
| app/…/AccountSettingsViewModel.kt | Account logout, library cleanup, and credential save ordering |