-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace StorageCodable with StorageValue
- Loading branch information
1 parent
935a7d1
commit e32ce12
Showing
5 changed files
with
177 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// Collection+RawRepresentable.swift | ||
// SwiftUIKit | ||
// | ||
// Created by Daniel Saidi on 2023-04-24. | ||
// Copyright © 2023-2024 Daniel Saidi. All rights reserved. | ||
// | ||
// Inspiration: https://nilcoalescing.com/blog/SaveCustomCodableTypesInAppStorageOrSceneStorage/ | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
/// This extension makes `Array` able to store Codable types, | ||
/// by serializing the collection to JSON. | ||
/// | ||
/// > Important: JSON encoding may cause important type data | ||
/// to disappear. For instance, JSON encoding a `Color` will | ||
/// not include any information about alternate color values | ||
/// for light and dark mode, high constrasts, etc. | ||
extension Array: RawRepresentable where Element: Codable { | ||
|
||
public init?(rawValue: String) { | ||
guard | ||
let data = rawValue.data(using: .utf8), | ||
let result = try? JSONDecoder().decode([Element].self, from: data) | ||
else { return nil } | ||
self = result | ||
} | ||
|
||
public var rawValue: String { | ||
guard | ||
let data = try? JSONEncoder().encode(self), | ||
let result = String(data: data, encoding: .utf8) | ||
else { return "" } | ||
return result | ||
} | ||
} | ||
|
||
/// This extension makes `Dictionary` able to store `Codable` | ||
/// types, by serializing the collection to JSON. | ||
/// | ||
/// > Important: JSON encoding may cause important type data | ||
/// to disappear. For instance, JSON encoding a `Color` will | ||
/// not include any information about alternate color values | ||
/// for light and dark mode, high constrasts, etc. | ||
extension Dictionary: RawRepresentable where Key: Codable, Value: Codable { | ||
|
||
public init?(rawValue: String) { | ||
guard | ||
let data = rawValue.data(using: .utf8), | ||
let result = try? JSONDecoder().decode([Key: Value].self, from: data) | ||
else { return nil } | ||
self = result | ||
} | ||
|
||
public var rawValue: String { | ||
guard | ||
let data = try? JSONEncoder().encode(self), | ||
let result = String(data: data, encoding: .utf8) | ||
else { return "{}" } | ||
return result | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,87 @@ | ||
// | ||
// StorageValue.swift | ||
// SwiftUIKit | ||
// | ||
// Created by Daniel Saidi on 2023-04-24. | ||
// Copyright © 2023-2024 Daniel Saidi. All rights reserved. | ||
// | ||
// Inspiration: https://nilcoalescing.com/blog/SaveCustomCodableTypesInAppStorageOrSceneStorage/ | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
/// This type can wrap any `Codable` type to make it able to | ||
/// store the value in `AppStorage` and `SceneStorage`. | ||
/// | ||
/// This type uses `JSONEncoder` and `JSONDecoder` to encode | ||
/// the value to data then decode it back to the source type. | ||
/// | ||
/// > Important: JSON encoding may cause important type data | ||
/// to disappear. For instance, JSON encoding a `Color` will | ||
/// not include any information about alternate color values | ||
/// for light and dark mode, high constrasts, etc. | ||
public struct StorageValue<Value: Codable>: RawRepresentable { | ||
|
||
public let value: Value | ||
} | ||
|
||
/// This is a shorthand for ``StorageValue``. | ||
public typealias AppStorageValue = StorageValue | ||
|
||
/// This is a shorthand for ``StorageValue``. | ||
public typealias SceneStorageValue = StorageValue | ||
|
||
public extension StorageValue { | ||
|
||
init(_ value: Value) { | ||
self.value = value | ||
} | ||
|
||
init?(rawValue: String) { | ||
guard | ||
let data = rawValue.data(using: .utf8), | ||
let result = try? JSONDecoder().decode(Value.self, from: data) | ||
else { return nil } | ||
self = .init(result) | ||
} | ||
|
||
var rawValue: String { | ||
guard | ||
let data = try? JSONEncoder().encode(value), | ||
let result = String(data: data, encoding: .utf8) | ||
else { return "" } | ||
return result | ||
} | ||
} | ||
|
||
private struct User: Codable, Identifiable { | ||
|
||
var name: String | ||
var age: Int | ||
|
||
var id: String { name } | ||
} | ||
|
||
#Preview { | ||
|
||
struct Preview: View { | ||
|
||
@AppStorage("com.swiftuikit.appstorage.user") | ||
var user: AppStorageValue<User>? | ||
|
||
var body: some View { | ||
Text(user?.value.name ?? "-") | ||
|
||
Button("Toggle user") { | ||
if user == nil { | ||
user = .init(User(name: "Daniel", age: 45)) | ||
} else { | ||
user = nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
return Preview() | ||
} |
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 |
---|---|---|
|
@@ -408,6 +408,9 @@ | |
} | ||
} | ||
} | ||
}, | ||
"Toggle user" : { | ||
|
||
} | ||
}, | ||
"version" : "1.0" | ||
|
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