Data persistence and settings
Screenbox keeps user-created playlists, playback resume points, library state, and app preferences across sessions. It uses a local SQLite database for media-related state and the Windows local settings store for small preference values. The database and settings are app-local, so they support continuity on the same device rather than account sync.
Persistence model
Screenbox separates data by how safely it can be rebuilt. The library folders and media records are a scan cache. Playlists and playback progress represent user state, while settings control how the app behaves when it starts and during playback.
| State | Store | Session role |
|---|---|---|
| Library folders and media metadata | SQLite cache tables | Restores the visible library before or between scans |
| Playlists and ordered items | SQLite durable tables | Preserves user-curated collections |
| Playback positions | SQLite progress table | Supports resume from a saved position |
| User preferences | Windows local settings | Restores app, library, playback, and player choices |
The main database file is screenbox.db in the app's local data folder. The database service initializes it before data access and enables foreign-key behavior so playlist items remain tied to their parent playlist. Screenbox.Core/…/DatabaseService.cs• L9-23, Screenbox.Core/…/DatabaseService.Schema.cs• L28-76
The database and settings paths are separate persistence boundaries. A database rebuild can remove database-backed state, but it does not describe the Windows settings store; a preference reset likewise does not rebuild the library database.
References: Screenbox.Core/…/DatabaseService.cs• L9-23 Screenbox.Core/…/DatabaseService.Schema.cs• L28-76 Screenbox.Core/…/SettingsStorage.cs• L6-35
Session lifecycle
At app startup, Screenbox registers one database service, one settings storage service, and shared settings contexts. The app initializes the database before it uses database-backed state. Settings contexts read their values when needed and keep an in-memory value for the current session.
The session flow connects persisted state to the live app as follows:
flowchart TD
A[Start app] --> B[Open or create local database]
B --> C[Check schema and legacy playlist data]
C --> D[Load library cache and playlists as needed]
A --> E[Create shared settings contexts]
E --> F[Read local preference values on access]
D --> G[Run current session]
F --> G
G --> H[Flush playback progress on app suspension]
The shared service lifetime matters because pages and playback components use the same settings contexts and progress tracker. A setting change can therefore affect active UI or playback without waiting for a later app restart.
References: Screenbox/App.xaml.cs• L299-322 Screenbox.Core/…/ServiceHelpers.cs• L59-89 Screenbox.Core/…/SettingsContextBase.cs• L25-37
Library state
The library state is a rebuildable snapshot of selected media folders and the metadata found during scans. Screenbox stores music and video records by media type, replacing the corresponding folder and record set when a new scan is saved.
The cache lets the app restore a useful library view across sessions without treating cached metadata as the source of truth. A later scan can replace it when files, folders, or metadata change.
References: Screenbox.Core/…/DatabaseService.LibraryCache.cs• L13-47 Screenbox.Core/…/DatabaseService.LibraryCache.cs• L49-175 Screenbox.Core/…/DatabaseService.Schema.cs• L60-65
Playlists
Playlists are durable collections owned by the user. Each playlist has an identifier, display name, update time, and ordered media entries. The stored entry uses the media location as its stable link; when Screenbox loads a playlist, it combines that link with available library metadata and falls back to the path when cached metadata is absent.
Saving a playlist replaces its current item list in one transaction. Deleting the playlist also deletes its items, so the playlist is the lifecycle owner of its stored entries. The application keeps the loaded playlist objects in a shared context while the playlist service provides the persistence boundary.
For the database table relationship and playlist read/write behavior, see Database and playback history. Playlist files can also be imported or exported as M3U8 entries, but that file exchange is separate from the durable in-app playlist record.
References: Screenbox.Core/…/PlaylistRecordDto.cs• L6-14 Screenbox.Core/…/PlaylistService.cs• L25-80 Screenbox.Core/…/DatabaseService.Playlists.cs• L14-95 Screenbox.Core/…/DatabaseService.Schema.cs• L370-385
Playback progress
Playback progress stores the last known position for a media location. The progress tracker keeps a bounded in-memory list during playback, moves recently updated entries to the front, and writes a complete snapshot to SQLite when the app suspends.
The saved position is optional session history rather than media metadata. Loading failure is non-fatal: Screenbox starts with an empty progress list, so a damaged or unavailable progress store does not prevent playback. The Database and playback history page describes the progress table and resume flow in more detail.
The PersistPlaybackPosition and RestorePlaybackPosition preferences control whether the app keeps and uses this history. These preferences are separate from the positions themselves.
References: Screenbox.Core/…/PlaybackProgressTracker.cs• L13-39 Screenbox.Core/…/PlaybackProgressTracker.cs• L41-137 Screenbox.Core/…/DatabaseService.PlaybackProgress.cs• L12-54 Screenbox.Core/…/PlaybackSettingsContext.cs• L43-53
Schema and recovery
The database schema defines separate tables for the library cache, playback progress, playlists, and playlist items. Startup checks the expected columns and creates missing tables. Legacy JSON playlists are imported when the new playlist tables are empty, which allows older app data to move into the current store.
The recovery policy differs from the data boundary: the library cache is intended to be replaceable, but the current database initialization fallback recreates the whole database after a SQLite or file error. That can also remove playlists and playback progress. Engineers who change schema or recovery behavior must treat playlist data as durable user data even though it shares the database file with cache data.
References: Screenbox.Core/…/DatabaseService.Schema.cs• L39-75 Screenbox.Core/…/DatabaseService.Schema.cs• L78-151 Screenbox.Core/…/DatabaseService.Schema.cs• L153-272 Screenbox.Core/…/DatabaseService.Schema.cs• L331-385
User preferences
User preferences are key-value values stored in Windows ApplicationData.Current.LocalSettings. Four shared settings contexts give those values a domain boundary: general app behavior, library scanning and sorting, playback behavior, and player presentation and controls.
Each context supplies a default for a missing value. A change updates the local store, the in-memory context, and observable change messages. Other services use those messages when a setting must change live behavior, such as library scanning or player controls.
For the preference keys and storage contract, see Settings storage. That page is the detailed child page for adding or changing preference values.
| Context | Main preference area |
|---|---|
| General | Theme, language, recent items, folder enqueue behavior, and multiple instances |
| Library | Indexing, removable storage scanning, and music or album sort order |
| Playback | Volume, repeat mode, subtitle language, and progress persistence or restore |
| Player | Controls, seek steps, gestures, frame capture, visualizer, and advanced playback options |
References: Screenbox.Core/…/SettingsStorage.cs• L6-35 Screenbox.Core/…/GeneralSettingsContext.cs• L6-50 Screenbox.Core/…/LibrarySettingsContext.cs• L6-43 Screenbox.Core/…/PlaybackSettingsContext.cs• L6-58 Screenbox.Core/…/PlayerSettingsContext.cs• L8-161
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to SQLite table columns must update schema checks, creation SQL, and any legacy import path that maps old data into the table.
- Changes to playlist item identity or ordering must preserve the link between playlist records and their ordered entries.
- Changes to media cache fields must keep playlist reconstruction compatible when a playlist item has no matching cache record.
- Changes to playback persistence must keep the suspension flush, progress preference, and resume lookup aligned.
- New settings must define a stable key, a default value, and the correct settings context before consumers depend on them.
- Changes to setting types or enum values must preserve the stored representation and live change notifications.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Database schema or file initialization fails | The current recovery path recreates the database and can remove playlists and progress | Keep cache recovery separate from durable-data recovery when changing this policy |
| A playlist item has no matching library cache record | The playlist still loads, but its display metadata falls back to the media path | Use the stored media location as the playlist item's stable identity |
| Playback progress cannot load | The app starts without resume positions | Treat progress loading as non-fatal and keep the setting that controls progress use separate |
| A stored preference has a missing or incompatible value | The context returns its default instead of the stored choice | Keep keys and value types stable when changing settings |
Key files
| File | Why to read it |
|---|---|
| Screenbox.Core/…/DatabaseService.Schema.cs | Database creation, schema checks, legacy playlist import, and recovery policy |
| Screenbox.Core/…/DatabaseService.LibraryCache.cs | Library cache read and replacement behavior |
| Screenbox.Core/…/DatabaseService.Playlists.cs | Playlist persistence and metadata reconstruction |
| Screenbox.Core/…/DatabaseService.PlaybackProgress.cs | Progress snapshot read and write behavior |
| Screenbox.Core/…/PlaybackProgressTracker.cs | In-session progress history and suspension flush |
| Screenbox.Core/…/SettingsStorage.cs | Windows local settings boundary |
| Screenbox.Core/…/SettingsContextBase.cs | Shared setting defaults, caching, and change notifications |
| Screenbox.Core/…/PlaybackSettingsContext.cs | Preferences that govern playback progress and resume |
| Screenbox.Core/…/PlayerSettingsContext.cs | Player-facing preferences and playback options |