Database and playback history
Screenbox uses one local SQLite database to make library loading fast and to keep user-created playlists and playback positions across app runs. The database is initialized at startup, while the library cache can be rebuilt from storage and playback history is kept as a small saved snapshot.
Database lifecycle
The database lifecycle turns local app data into a ready store before other services read it. Screenbox.Core/…/DatabaseService.cs places screenbox.db in the app's local data folder and shares one initialization path for all database operations.
Schema initialization
Schema initialization creates the library, playlist, and playback-history records when the database is new. It also checks table columns on later starts and imports old JSON playlists when the new playlist tables are empty.
The main data areas are:
| Data area | Role | Recovery expectation |
|---|---|---|
| Library folders and media records | Rebuildable view of known music and video files | Safe to recreate from the device library |
| Playback progress | Last saved position for recent media | Can start empty if it cannot be loaded |
| Playlists and playlist items | User-created playlist names and ordered media paths | Intended to persist across library rescans |
Database recovery
SQLite or file errors during schema setup cause the database file to be removed and created again with all known tables. This lets the app start with a usable store, but it can also remove playlists because the recovery boundary is the whole database file.
The app logs startup initialization errors and continues its startup path. Later reads treat a missing or unreadable library cache as a reason to scan storage again.
References: Screenbox.Core/…/DatabaseService.cs• L9-69 Screenbox.Core/…/DatabaseService.Schema.cs• L15-76 Screenbox/App.xaml.cs• L317-328
Library cache
The library cache is a saved snapshot of library folders and media metadata. Screenbox.Core/…/LibraryService.cs uses it to avoid a full file and metadata scan when the saved folders and storage change history still match the current library.
Cache refresh
Music and video use separate cache snapshots. Each refresh replaces the folders and media records for one media type in one database transaction, so the cache represents the latest scan rather than a long-lived record of every file ever seen.
The normal read path is:
- Load the saved folders and media records for the requested media type.
- Compare the saved folders with the current library.
- Apply tracked file changes when the cache still matches.
- Run a full scan and write a new snapshot when the cache is missing, stale, or cannot resolve changes.
This flow keeps cached metadata useful without treating it as the source of truth. The files and the current storage library remain authoritative.
References: Screenbox.Core/…/LibraryService.cs• L110-214 Screenbox.Core/…/LibraryService.cs• L217-305 Screenbox.Core/…/DatabaseService.LibraryCache.cs• L12-175
Playlist persistence
Playlist persistence keeps named, ordered selections of media separate from the rebuildable library cache. Screenbox.Core/…/PlaylistService.cs saves a playlist as one record plus its ordered media paths and exposes the result to playlist screens through the shared playlist context.
Playlist records
Saving a playlist replaces its current item list as one operation. Loading joins each saved path with matching library metadata when available, so a playlist can still identify an item after a cache refresh. If no cached metadata exists, the path supplies a basic fallback identity.
Playlist changes have these effects:
| Operation | Result |
|---|---|
| Save or add items | Updates the playlist timestamp and rewrites its ordered items |
| List playlists | Returns playlists by most recent update with their items |
| Delete a playlist | Removes the playlist and its child items |
| Import or export | Reads or writes playlist entries through a file format without replacing the saved database playlist until a save occurs |
References: Screenbox.Core/…/PlaylistService.cs• L14-80 Screenbox.Core/…/DatabaseService.Playlists.cs• L11-136 Screenbox.Core/…/DatabaseService.Playlists.cs• L138-191 Screenbox.Core/…/PlaylistsContext.cs• L7-16
Playback progress
Playback progress provides resume-from-position behavior for media locations. Screenbox.Core/…/PlaybackProgressTracker.cs keeps recent positions in memory, limits the list to 64 entries, and writes the full current snapshot to the database when the app suspends.
Progress flow
The tracker uses the media location as the identity for a saved position. A new playback update moves that location to the front of the recent list; asking for an unknown location returns zero.
This diagram shows how playback state moves between the player, the in-memory tracker, and the database.
flowchart LR
A[Playback position update] --> B[In-memory recent positions]
B --> C[Resume lookup]
B --> D[App suspension]
D --> E[Replace saved progress snapshot]
E --> F[(SQLite playback history)]
F --> G[Load on next app run]
G --> B
The tracker loads saved positions when requested and saves them in one replacement transaction. A load or save error is non-fatal: the app can continue with the positions that remain in memory, or with an empty list after a failed load.
References: Screenbox.Core/…/PlaybackProgressTracker.cs• L13-39 Screenbox.Core/…/PlaybackProgressTracker.cs• L41-137 Screenbox.Core/…/DatabaseService.PlaybackProgress.cs• L9-54 Screenbox/App.xaml.cs• L293-315
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the table shape, cache DTOs, and media-type filters aligned when adding or removing cached metadata.
- Preserve the playlist item order contract between saved
sort_ordervalues and playlist display order. - Keep playlist loading usable when a media path is absent from the library cache.
- Treat database-file recreation and schema-drift handling as a playlist data-loss boundary.
- Keep suspension handling connected to the progress snapshot save so recent positions are not left only in memory.
- Keep the media-location identity consistent between playback updates, progress reads, and saved rows.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Database schema setup hits a SQLite or file error | The whole database is recreated and saved playlists can be lost | Log the failure and treat the library cache as rebuildable; protect playlist data before changing recovery behavior |
| Saved library folders or tracked changes no longer match storage | The app discards the cache path and performs a full scan | Use the current storage library as the source of truth |
| A playlist item has no matching media-cache record | The playlist still loads, but its metadata is limited to path-based fallback data | Do not make playlist loading depend on a cache row |
| Playback progress cannot load from SQLite | Resume positions start empty for that run | Keep playback usable without saved history |
| The app suspends before progress is saved | Recent position changes can be lost | Complete the suspension deferral and await the progress save |
Key files
| File | Why to read it |
|---|---|
| Screenbox.Core/…/DatabaseService.Schema.cs | Shows database creation, schema checks, legacy playlist import, and recovery boundaries |
| Screenbox.Core/…/DatabaseService.LibraryCache.cs | Shows the cache snapshot contract for music and video |
| Screenbox.Core/…/LibraryService.cs | Shows when the app trusts, updates, or rebuilds the library cache |
| Screenbox.Core/…/DatabaseService.Playlists.cs | Shows playlist records, ordered items, and metadata fallback behavior |
| Screenbox.Core/…/PlaylistService.cs | Shows the product-level playlist save, update, delete, import, and export flow |
| Screenbox.Core/…/PlaybackProgressTracker.cs | Shows recent-position limits and the in-memory to disk lifecycle |
| Screenbox.Core/…/DatabaseService.PlaybackProgress.cs | Shows the saved playback-progress snapshot contract |
| Screenbox/App.xaml.cs | Shows startup database initialization and suspension-time save coordination |