Add @AppStorage#60
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@AppStorage("key")forBool,Int,Double, andString— values that outlive the process.Keeping the core platform-free
Persistence needs a platform store, but the core owns no platform APIs. So the backing store is a protocol the host installs: an in-memory store by default (which is what the macOS tests run against), and a JSON file under the app's private files directory on Android.
The path comes through the existing
Contextbinding (getFilesDir()), so this adds no bridge surface — consistent with the O(1)-bridge rule. Wiring it happens inMainActivity.onCreateSwiftbefore the first view is built, so the opening evaluation already reads saved values rather than flashing defaults.Reads and writes go through the same
StateBoxmachinery@Stateuses, so changing a stored value marks the view dirty exactly like any other state write, and$valueprojects a normalBindingthat writes through to disk.Verification
swift test— 4 new tests, 105 passing: a fresh wrapper reads what a previous one wrote (the relaunch in miniature), all four types round-trip under independent keys, the projected binding writes through from a realTextFieldcallback, and a secondFileAppStorageinstance reloads what the first wrote (including that removal sticks).files/app-storage.jsoncontained{"demo.launches":3}, force-stopped the process (verified 0 processes alive), relaunched, and the counter still read 3. Then exercised the other types through their bindings, leaving{"demo.volume":0.926,"demo.notify":true,"demo.launches":4}on disk.Scope
Four value types only —
RawRepresentable,URL, andDataoverloads aren't included, and there's no@SceneStorage. The file is rewritten whole on each write, which is fine for preferences and deliberately not a database.AppStorageStore.backendis process-wide, so it's set once at startup rather than per-view.