Fixed stored @State properties on App #34

Merged
Amzd merged 3 commits from State-property-on-app-fix into main 2024-06-11 14:34:06 +02:00
Amzd commented 2024-06-01 14:03:21 +02:00 (Migrated from github.com)

Steps

  • Add your name or username and a link to your GitHub profile into the Contributors.md file.
  • Build the project on your machine. If it does not compile, fix the errors.
  • Describe the purpose and approach of your pull request below.
  • Submit the pull request. Thank you very much for your contribution!

Purpose

Currently when using a State property with storage on App instead of on a View, the app ID was accessed before it was set, causing the State internals to look for the data in the folder temporary/ which usually doesn't exist because at the time you will be writing into the State the app ID will be set and it writes the value into app.id/ folder.

This happened because when the State property is on App it is initialized before GTUIApp.appID is set.

struct ExampleApp: App {
    let id = "me.amzd.Example"
    var app: GTUIApp!

    /// This State will try to read its value for the key example from disk before we can access `id`
    /// (because we need to initialize ExampleApp to access the id property)
    @State("example") var example: String?
    
    // ...
}

Approach

I fixed this by making the App.id property static and forwarding it to GTUIApp.appID first thing the program does. I have also made the GTUIApp.appID force unwrapped optional so regression of this issue is not possible.

I am open to suggestions of how to fix this differently. You can also fix this yourself in a different way without crediting, it is not that complicated.

Since it is a breaking change I still need to update the examples

## Steps - [x] Add your name or username and a link to your GitHub profile into the [Contributors.md][1] file. - [x] Build the project on your machine. If it does not compile, fix the errors. - [x] Describe the purpose and approach of your pull request below. - [x] Submit the pull request. Thank you very much for your contribution! ## Purpose Currently when using a `State` property with storage on `App` instead of on a `View`, the app ID was accessed before it was set, causing the `State` internals to look for the data in the folder `temporary/` which usually doesn't exist because at the time you will be writing into the `State` the app ID will be set and it writes the value into `app.id/` folder. This happened because when the `State` property is on `App` it is initialized before `GTUIApp.appID` is set. ```swift struct ExampleApp: App { let id = "me.amzd.Example" var app: GTUIApp! /// This State will try to read its value for the key example from disk before we can access `id` /// (because we need to initialize ExampleApp to access the id property) @State("example") var example: String? // ... } ``` ## Approach I fixed this by making the `App.id` property static and forwarding it to `GTUIApp.appID` first thing the program does. I have also made the `GTUIApp.appID` force unwrapped optional so regression of this issue is not possible. I am open to suggestions of how to fix this differently. You can also fix this yourself in a different way without crediting, it is not that complicated. Since it is a breaking change I still need to update the examples [1]: /Contributors.md
david-swift commented 2024-06-11 13:42:30 +02:00 (Migrated from github.com)

Thanks for opening the dicussion and already providing a potential solution! I prefer not to require the user to add a static variable, so I'll see whether I can make the state variables load the files after initializing the app structure. Otherwise, I'll merge your implementation.

Thanks for opening the dicussion and already providing a potential solution! I prefer not to require the user to add a static variable, so I'll see whether I can make the state variables load the files after initializing the app structure. Otherwise, I'll merge your implementation.
david-swift commented 2024-06-11 14:32:58 +02:00 (Migrated from github.com)

Instead of reading the app id at the initialization of a state variable, it will now read it as soon as the value is accessed for the first time. That way, the id property can stay on an app's instance.

Instead of reading the app id at the initialization of a state variable, it will now read it as soon as the value is accessed for the first time. That way, the id property can stay on an app's instance.
david-swift (Migrated from github.com) approved these changes 2024-06-11 14:33:46 +02:00
Sign in to join this conversation.
No Milestone
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: aparoksha/adwaita-swift#34
No description provided.