Skip to content

Commit

Permalink
fix(store): failed to decode state after upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
mhamann committed Apr 7, 2023
1 parent 41a30b2 commit c2631e3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
"version" : "2.2.1"
}
},
{
"identity" : "factory",
"kind" : "remoteSourceControl",
"location" : "https://github.com/hmlongco/Factory",
"state" : {
"revision" : "39ff6a675cd0272d833d184d35add0f8fddd63de",
"version" : "1.3.7"
}
},
{
"identity" : "get",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -72,6 +81,15 @@
"version" : "2.6.3"
}
},
{
"identity" : "kronos",
"kind" : "remoteSourceControl",
"location" : "https://github.com/MobileNativeFoundation/Kronos.git",
"state" : {
"revision" : "b7f54653a8bb503f42b59ab3160eb11f333b9d3f",
"version" : "4.2.1"
}
},
{
"identity" : "lbbottomsheet",
"kind" : "remoteSourceControl",
Expand All @@ -91,12 +109,12 @@
}
},
{
"identity" : "mocker#activating-the-mocker",
"identity" : "mocker",
"kind" : "remoteSourceControl",
"location" : "https://github.com/WeTransfer/Mocker#activating-the-mocker",
"location" : "git@github.com:WeTransfer/Mocker.git",
"state" : {
"revision" : "e7165fb0378193c6784f1d46a9ab2b6184c384fa",
"version" : "2.7.0"
"revision" : "4384e015cae4916a6828252467a4437173c7ae17",
"version" : "3.0.1"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,57 @@
<dict>
<key>SchemeUserState</key>
<dict>
<key>AnyCodable (Playground) 1.xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>9</integer>
</dict>
<key>AnyCodable (Playground) 2.xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>10</integer>
</dict>
<key>AnyCodable (Playground).xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>8</integer>
</dict>
<key>JWTDecode (Playground) 1.xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>6</integer>
</dict>
<key>JWTDecode (Playground) 2.xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>7</integer>
</dict>
<key>JWTDecode (Playground).xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>5</integer>
</dict>
<key>Rownd.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>11</integer>
<integer>1</integer>
</dict>
<key>RowndFrameworkTestApp.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>10</integer>
<integer>0</integer>
</dict>
<key>RowndTests.xcscheme_^#shared#^_</key>
<dict>
Expand Down
18 changes: 16 additions & 2 deletions Sources/Rownd/Models/Context/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ extension RowndState {
case appConfig, auth, user, signIn
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
appConfig = try container.decode(AppConfigState.self, forKey: .appConfig)
auth = try container.decode(AuthState.self, forKey: .auth)
user = try container.decode(UserState.self, forKey: .user)
signIn = try container.decodeIfPresent(SignInState.self, forKey: .signIn) ?? SignInState()
}

static func save(state: RowndState) {
Task {
if let encoded = try? state.toJson() {
Expand All @@ -37,12 +45,18 @@ extension RowndState {
let existingStateStr = Storage.store?.object(forKey: STORAGE_STATE_KEY) as? String ?? String("{}")
// logger.trace("initial store state: \(existingStateStr)")

let decoder = JSONDecoder()
if var decoded = try? decoder.decode(RowndState.self, from: (existingStateStr.data(using: .utf8) ?? Data())) {
do {
let decoder = JSONDecoder()
var decoded = try decoder.decode(
RowndState.self,
from: (existingStateStr.data(using: .utf8) ?? Data())
)
decoded.isInitialized = true
DispatchQueue.main.async {
store.dispatch(InitializeRowndState(payload: decoded))
}
} catch {
logger.debug("Failed decoding state from storage (if this is the first time launching the app, this is expected): \(String(describing: error))")
}
}

Expand Down

0 comments on commit c2631e3

Please sign in to comment.