Introducing a new way to review your PRs.

Read the announcement

metrolistgroup / metrolist

Material Design YouTube Music client for Android, iOS and desktop

View on GitHub

19 documents · Updated Nov 4, 2026

docs / local-library

Local library and data management

The local library is the app’s durable view of music, playlists, listening history, recognition history, and user settings. It joins local records with remote YouTube Music data so screens and playback can use one consistent set of media objects.

Data ownership boundaries

The system separates durable media data from small user choices. Room stores the library and its relations in the MusicDatabase, while Android DataStore stores preferences that control how the library is shown and how history is recorded.

flowchart LR
    R[Remote music data] --> T[Metadata transformation]
    T --> D[Local library database]
    P[User preferences] --> V[Library and history views]
    D --> V
    D --> X[Playback and statistics]
    V --> U[User actions]
    U --> D
    U --> P

The database is not only a cache of remote results. It also keeps local membership, playlist order, download state, play totals, and other state needed when the network is not available.

Media records

Media identity

Media records use a stable song ID as their main identity. A song can also refer to artists and albums through relations, and its flags distinguish library membership, likes, downloads, uploads, videos, and podcast episodes. This lets one record support several library views without copying the same song for each view.

References: app/…/SongEntity.kt• L19-67 app/…/Song.kt• L13-68 app/…/DatabaseDao.kt• L82-150

Library collections

Songs, artists, albums, playlists, and podcasts form the main local collections. A collection view is derived from saved state such as inLibrary, bookmarkedAt, isUploaded, or episode status rather than from a separate copy of the collection. The library can therefore show liked, saved, downloaded, uploaded, and podcast content from related records.

References: app/…/Album.kt• L13-43 app/…/Artist.kt• L11-24 app/…/Playlist.kt• L13-45 app/…/PodcastEntity.kt• L23-42 app/…/DatabaseDao.kt• L1091-1137

Playlist membership

Playlist state

Playlists can represent a remote YouTube Music playlist or a local playlist. A remote playlist has a browse ID and may sync with YouTube Music. A local playlist has no remote identity and remains an app-owned collection. Special playlists provide views such as liked songs, downloads, and listening statistics.

References: app/…/PlaylistEntity.kt• L25-53 app/…/DatabaseDao.kt• L1095-1145

Ordered membership

The playlist membership record connects one song to one playlist and stores its position. This separate relationship preserves order and allows the same song to appear in many playlists. Playlist changes update membership and playlist state together so the displayed order and the playlist’s update time stay aligned.

References: app/…/PlaylistSongMap.kt• L13-36 app/…/DatabaseDao.kt• L1153-1214 app/…/Playlist.kt• L14-30

History and preferences

Listening history

Listening history records play events separately from the song’s lifetime totals. The history view groups the latest event for each song into time periods, while statistics use the event data to calculate play time and counts. The app can clear these events without removing the library record.

References: app/…/Event.kt• L15-32 app/…/DatabaseDao.kt• L1473-1516 app/…/HistoryViewModel.kt• L42-90

Search and recognition history

Search history keeps unique query text for later suggestions. Recognition history keeps the result of identifying a track, including its display data and recognition time. These histories have their own clear and delete actions, so they do not affect listening history or library membership.

References: app/…/SearchHistory.kt• L12-24 app/…/RecognitionHistory.kt• L13-40 app/…/DatabaseDao.kt• L1519-1543

User preferences

Preferences store user choices that shape library queries and screens. They include collection filters, sort order, content filters, history pause controls, sync markers, and view choices. DataStore exposes these values as a stream, so a preference change can change the active database query without rebuilding the stored media data.

References: app/…/DataStore.kt• L32-127 app/…/PreferenceKeys.kt• L93-105 app/…/PreferenceKeys.kt• L160-162 app/…/LibraryViewModels.kt• L93-114

Data transformations

Remote metadata mapping

Remote song and episode results are first mapped to MediaMetadata, the app-facing media shape used by the player and local writes. The mapping keeps identity, title, artists, album, media type, episode state, and library tokens in one form. A second mapping turns that form into a database song and its related artist records.

References: app/…/MediaMetadata.kt• L19-69 app/…/MediaMetadata.kt• L122-179 app/…/DatabaseDao.kt• L1699-1725

Local media mapping

Database relation objects combine a base record with its artists, album, format, or playlist members. When the app sends local media to the player or another UI surface, it maps these relation objects back to MediaMetadata. This keeps storage relations separate from the simpler object used by playback and presentation.

References: app/…/Song.kt• L13-91 app/…/Album.kt• L13-50 app/…/MediaMetadata.kt• L86-116

Synchronization boundary

Remote library synchronization

Synchronization compares remote library results with local records and then updates membership, metadata, and playlist contents. It avoids treating an incomplete or empty remote response as proof that the user removed all data. Playlist synchronization also preserves local additions when remote membership changes.

References: app/…/SyncUtils.kt• L858-913 app/…/SyncUtils.kt• L1456-1536 app/…/SyncUtils.kt• L1568-1645

Local and remote actions

A library action can change local state and request the matching remote action. Song library membership and playlist bookmarks use local timestamps for immediate views, while remote calls keep the signed-in YouTube Music library aligned. A local playlist can remain independent when the user does not request remote creation.

References: app/…/SongEntity.kt• L74-105 app/…/PlaylistEntity.kt• L55-71 app/…/SyncUtils.kt• L1706-1727

For the database schema, migrations, and transaction rules, see Room persistence. For user-facing library actions and sync flows, see Library workflows.

Change checklist

When these files or sections are changed, remember to consider these:

  • Changes to media identity or relation fields must preserve the remote-to-MediaMetadata and MediaMetadata-to-database mappings.
  • Changes to library membership flags must update the DAO queries that define library, liked, uploaded, downloaded, and saved-episode views.
  • Changes to playlist membership must preserve item positions and the sync rule for local additions.
  • Changes to history event shape or time rules must keep the history view and statistics queries aligned.
  • Changes to preference keys or enum values must preserve the defaults used by library view models.

Important failure modes

TriggerConsequenceGuard
An incomplete remote library response is treated as completeValid local membership is clearedSync skips remote removals when the response is empty or incomplete
A song is inserted without its artist relationsLibrary rows lose artist names and artist-based views become incompleteMetadata insertion creates the song and its artist links in one transaction
Playlist positions are changed without the membership updateSongs display in the wrong order or later syncs misread local changesPlaylist edits update ordered membership inside a transaction
A history event points to a missing songHistory hydration and statistics cannot resolve the mediaThe event relation uses a song foreign key with cascade cleanup
A preference key changes without its reader defaultA library screen can select an invalid filter or sort modeView models decode keys with explicit enum defaults

Key files

FileWhy to read it
app/…/MusicDatabase.ktDefines the durable database boundary and migration path.
app/…/DatabaseDao.ktShows the library, playlist, history, and transformation-facing data contracts.
app/…/SongEntity.ktDefines the state carried by each media record.
app/…/PlaylistEntity.ktDefines local and remote playlist identity.
app/…/PlaylistSongMap.ktDefines ordered playlist membership.
app/…/MediaMetadata.ktDefines the shared media shape between remote data, storage, and playback.
app/…/SyncUtils.ktShows how remote library state becomes local state.
app/…/DataStore.ktDefines the preference stream and safe write boundary.
app/…/LibraryViewModels.ktShows how preferences select local library views.
app/…/HistoryViewModel.ktShows how stored events become grouped history sections.