-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: [vision-os-sample-app][2/n] App State Storage (#618)
Co-authored-by: Ahmed Ali <[email protected]>
- Loading branch information
Showing
4 changed files
with
144 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Foundation | ||
|
||
class AppState: ObservableObject { | ||
static let shared = AppState() | ||
|
||
@Published var profile: Profile = .loadFromStorage() { | ||
didSet { | ||
UserDefaults.standard.setValue( | ||
profile.toJson(), | ||
forKey: Profile.storageKey() | ||
) | ||
} | ||
} | ||
|
||
@Published var workspaceSettings: WorkspaceSettings = .loadFromStorage() { | ||
didSet { | ||
UserDefaults.standard.setValue( | ||
workspaceSettings.toJson(), | ||
forKey: WorkspaceSettings.storageKey() | ||
) | ||
} | ||
} | ||
} |
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,66 @@ | ||
import CioTracking | ||
import Foundation | ||
|
||
extension Region: CaseIterable, Codable { | ||
public static var allCases: [Region] = [.EU, .US] | ||
} | ||
|
||
struct WorkspaceSettings: UserDefaultsCodable { | ||
var siteId: String | ||
var apiKey: String | ||
var region: Region = .EU | ||
|
||
static func storageKey() -> String { | ||
"UserDefaultsCodable" | ||
} | ||
|
||
static func empty() -> Self { | ||
WorkspaceSettings(siteId: "", apiKey: "") | ||
} | ||
|
||
func isSet() -> Bool { | ||
!siteId.isEmpty && !apiKey.isEmpty | ||
} | ||
} | ||
|
||
struct Profile: UserDefaultsCodable { | ||
var id: String | ||
var properties: [Property] | ||
var loggedIn: Bool | ||
|
||
static func empty() -> Profile { | ||
Profile(id: UUID().uuidString, properties: [], loggedIn: false) | ||
} | ||
|
||
static func storageKey() -> String { | ||
"Profile" | ||
} | ||
} | ||
|
||
struct Property: Codable, Identifiable, Comparable, Equatable { | ||
static func < (lhs: Property, rhs: Property) -> Bool { | ||
lhs.name == rhs.name | ||
} | ||
|
||
let id: String | ||
var name: String | ||
var value: String | ||
|
||
init(name: String, value: String) { | ||
self.id = UUID().uuidString | ||
self.name = name | ||
self.value = value | ||
} | ||
} | ||
|
||
extension [Property] { | ||
func toDictionary() -> [String: String] { | ||
var res: [String: String] = [:] | ||
forEach { p in | ||
if !p.name.isEmpty { | ||
res[p.name] = p.value | ||
} | ||
} | ||
return res | ||
} | ||
} |
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,34 @@ | ||
import Foundation | ||
|
||
protocol UserDefaultsCodable: Codable { | ||
static func storageKey() -> String | ||
static func empty() -> Self | ||
} | ||
|
||
extension UserDefaultsCodable { | ||
func toJson() -> Data { | ||
let encoder = JSONEncoder() | ||
return try! encoder.encode(self) | ||
} | ||
|
||
static func from(_ data: Data) -> Self? { | ||
let decoder = JSONDecoder() | ||
return try? decoder.decode(Self.self, from: data) | ||
} | ||
|
||
static func loadFromStorage() -> Self { | ||
if let data = | ||
UserDefaults.standard.object(forKey: storageKey()) as? Data, | ||
let storedInstance = from(data) { | ||
return storedInstance | ||
} | ||
|
||
let content = Self.empty() | ||
UserDefaults.standard.setValue( | ||
content.toJson(), | ||
forKey: Self.storageKey() | ||
) | ||
|
||
return content | ||
} | ||
} |