data-preferences-impl¶
Implementation of application preferences using Jetpack DataStore and Protocol Buffers.
Features¶
- Type Safety: All settings are backed by
.protoschemas. - Reactive: Settings are exposed as
Flowsfor real-time UI updates. - Transaction-safe: DataStore ensures atomic updates and handles file system complexities.
- Clean Separation: Domain models are never exposed to the disk format directly; mappers ensure binary compatibility.
Internal Workflow¶
When a preference is requested:
1. Storage reads the data from the .pb file via DataStore.
2. SourceImpl takes the generated Proto model and passes it to a Mapper.
3. The Mapper returns a clean API Resource to the caller.
Adding a New Preference¶
- Define the schema in
src/main/proto. - Implement a
Serializerincore/serializer. - Create a
Storageclass insource/storage. - Implement a
Mapperincore/mapper. - Create the
SourceImplinsource/datasource. - Register the
DataStoreand bindings inDataPreferencesImplModule.
Camera-Source Preference (@since 1.4.0)¶
The camera-source preference is a full example of the workflow above. It persists the camera chosen for motion detection and MJPEG streaming (auto, front, rear, or external) — on all form-factors, not just TV — through:
- Schema:
src/main/proto/camera_data.proto(message CameraProtoModel { string mode = 1; }) CameraProtoSerializer,CameraPreferenceStorage,CameraProtobufPreferenceMapper,CameraPreferenceSourceImpl- DI:
cameraDataStore()(@Named("cameraDataStore")) persisting tocamera.pb(PreferenceConfigure.Filename.CAMERA)