forked from shadowfacts/Tusker
61 lines
1.6 KiB
Swift
61 lines
1.6 KiB
Swift
// Preference.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 6/13/19.
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
@propertyWrapper
|
|
struct Preference<Value> {
|
|
let path: WritableKeyPath<Preferences, Value>
|
|
let binding: Binding<Value>
|
|
|
|
init(_ path: WritableKeyPath<Preferences, Value>) {
|
|
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<Value, PrefValue> {
|
|
let path: WritableKeyPath<Preferences, PrefValue>
|
|
let fromPref: (PrefValue) -> Value
|
|
let toPref: (Value) -> PrefValue
|
|
let binding: Binding<Value>
|
|
|
|
init(_ path: WritableKeyPath<Preferences, PrefValue>, 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)
|
|
}
|
|
}
|
|
}
|