Automattic / Gravatar-SDK-iOS

Gravatar SDK is a Swift library that allows you to integrate Gravatar features into your own iOS applications.
https://gravatar.com
Mozilla Public License 2.0
52 stars 5 forks source link

Fix initialization of `@State` var #494

Closed pinarol closed 1 month ago

pinarol commented 1 month ago

Closes #

Description

This fixes a bug i detected during the Jetpack integration. Because of the way we initialize the @State variable, the token was not being set successfully. This article does a good job explaining why: https://www.swiftcraft.io/articles/how-to-initialize-state-inside-the-views-init-

From an Apple dev:

Although that will compile, @State variables in SwiftUI should not be initialized from data you pass down through the initializer; since the model is maintained outside of the view, there is no guarantee that the value will really be used. The correct thing to do is to set your initial state values inline: @State private var viewModel = SignInViewModel(...)

So I am separating the local @State token from the one passed from outside. Also removing the @State from the scope variable as well, it doesn't need to be a @State. This is not directly related with the bug, but it's the same wrong usage as well thus needs fixing.

I also rearranged the lines a bit.

Testing Steps

Go to UIKit Demo app > QE Enter email Enter token <-- important

Tap Logout if the button is present

Open the QE, verify it does NOT go through OAuth and goes directly to QE.

wpmobilebot commented 1 month ago
Gravatar SwiftUI Prototype Build📲 You can test the changes from this Pull Request in Gravatar SwiftUI Prototype Build by scanning the QR code below to install the corresponding build.
App NameGravatar SwiftUI Prototype Build Gravatar SwiftUI Prototype Build
Build Number1492
Version1.0
Bundle IDcom.automattic.gravatar-sdk-demo-swiftui.prototype-build
Commit87ea511d88a07c216711fc36b2bdc9fe0503c612
App Center BuildGravatar SDK Demo - SwiftUI #267
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.
wpmobilebot commented 1 month ago
Gravatar UIKit Prototype Build📲 You can test the changes from this Pull Request in Gravatar UIKit Prototype Build by scanning the QR code below to install the corresponding build.
App NameGravatar UIKit Prototype Build Gravatar UIKit Prototype Build
Build Number1492
Version1.0
Bundle IDcom.automattic.gravatar-sdk-demo-uikit.prototype-build
Commit87ea511d88a07c216711fc36b2bdc9fe0503c612
App Center BuildGravatar SDK Demo - UIKit #267
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.