-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move smile config decoding and initialisation into root view model. a…
…dd a closure to send back update config to root view.
- Loading branch information
Showing
6 changed files
with
54 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import SmileID | ||
import SwiftUI | ||
|
||
class RootViewModel: ObservableObject { | ||
@Published private(set) var decodedConfig: Config? | ||
|
||
// This is set by the SettingsView | ||
@AppStorage("smileConfig") private var configJson = ( | ||
UserDefaults.standard.string(forKey: "smileConfig") ?? "" | ||
) | ||
private let jsonDecoder = JSONDecoder() | ||
|
||
init(config: Config? = nil) { | ||
// It is possible the app was built without a smile_config, so it may be null | ||
let builtInConfig = Bundle.main.url(forResource: "smile_config", withExtension: "json") | ||
.flatMap { | ||
try? jsonDecoder.decode(Config.self, from: Data(contentsOf: $0)) | ||
} | ||
let configFromUserStorage = try? jsonDecoder.decode( | ||
Config.self, | ||
from: configJson.data(using: .utf8)! | ||
) | ||
|
||
// If a config was set at runtime (i.e. saved in UserStorage) prioritize that. Fallback to | ||
// the built-in config if not set. Otherwise, ask the user to set a config. | ||
self.decodedConfig = configFromUserStorage ?? builtInConfig | ||
} | ||
|
||
func updateConfig(config: Config) { | ||
self.decodedConfig = config | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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