// Preference.swift // Tusker // // Created by Shadowfacts on 6/13/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import SwiftUI @propertyWrapper struct Preference { let path: WritableKeyPath let binding: Binding init(_ path: WritableKeyPath) { self.path = path self.binding = Binding(get: { return Preferences.shared[keyPath: path] }, set: { (newValue) in Preferences.shared[keyPath: path] = newValue }) } var wrappedValue: Value { get { return Preferences.shared[keyPath: path] } set { Preferences.shared[keyPath: path] = newValue } } } @propertyWrapper struct MappedPreference { let path: WritableKeyPath let fromPref: (PrefValue) -> Value let toPref: (Value) -> PrefValue let binding: Binding init(_ path: WritableKeyPath, fromPref: @escaping (PrefValue) -> Value, toPref: @escaping (Value) -> PrefValue) { self.path = path self.fromPref = fromPref self.toPref = toPref self.binding = Binding(get: { return fromPref(Preferences.shared[keyPath: path]) }, set: { (newValue) in Preferences.shared[keyPath: path] = toPref(newValue) }) } var wrappedValue: Value { get { return fromPref(Preferences.shared[keyPath: path]) } set { Preferences.shared[keyPath: path] = toPref(newValue) } } }