Last active
October 10, 2023 16:00
-
-
Save levochkaa/58563c4df77b821e8c7fa4625cf94800 to your computer and use it in GitHub Desktop.
@PublishedAppStorage
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
// PublishedAppStorage.swift | |
import Combine | |
import SwiftUI | |
@propertyWrapper | |
struct PublishedAppStorage<Value> { | |
@UserDefault private var storedValue: Value | |
private var publisher: Publisher? | |
private var objectWillChange: ObservableObjectPublisher? | |
// swiftlint:disable nesting | |
struct Publisher: Combine.Publisher { | |
typealias Output = Value | |
typealias Failure = Never | |
func receive<S: Subscriber>(subscriber: S) where S.Input == Value, S.Failure == Never { | |
subject.subscribe(subscriber) | |
} | |
fileprivate let subject: CurrentValueSubject<Value, Never> | |
fileprivate init(_ output: Output) { | |
self.subject = .init(output) | |
} | |
} | |
// swiftlint:enable nesting | |
var projectedValue: Publisher { | |
mutating get { | |
if let publisher { | |
return publisher | |
} | |
let publisher = Publisher(storedValue) | |
self.publisher = publisher | |
return publisher | |
} | |
} | |
@available(*, unavailable, message: "@Published is only available on properties of classes") | |
var wrappedValue: Value { | |
get { fatalError() } | |
set { fatalError() } // swiftlint:disable:this unused_setter_value | |
} | |
static subscript<EnclosingSelf: ObservableObject>( | |
_enclosingInstance object: EnclosingSelf, | |
wrapped _: ReferenceWritableKeyPath<EnclosingSelf, Value>, | |
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, PublishedAppStorage<Value>> | |
) -> Value { | |
get { object[keyPath: storageKeyPath].storedValue } | |
set { | |
(object.objectWillChange as? ObservableObjectPublisher)?.send() | |
object[keyPath: storageKeyPath].publisher?.subject.send(newValue) | |
object[keyPath: storageKeyPath].storedValue = newValue | |
} | |
} | |
init(wrappedValue: Value, _ key: String, store: UserDefaults = .standard) { | |
self._storedValue = UserDefault(wrappedValue: wrappedValue, key, store: store) | |
} | |
} |
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
// UserDefault.swift | |
import Foundation | |
@propertyWrapper | |
struct UserDefault<Value> { | |
let defaultValue: Value | |
let key: String | |
var store: UserDefaults | |
init(wrappedValue defaultValue: Value, _ key: String, store: UserDefaults = .standard) { | |
self.defaultValue = defaultValue | |
self.key = key | |
self.store = store | |
} | |
var wrappedValue: Value { | |
get { store.object(forKey: key) as? Value ?? defaultValue } | |
set { store.set(newValue, forKey: key) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UserDefault is a property wrapper just to easily use UserDefaults with any objects.
PublishedAppStorage is a much more complex property wrapper, that uses UserDefault property wrapper to save properties as it does AppStorage in SwiftUI and updates corresponding views using
objectWillChange
of a parentObservableObject