Use ObservedObject for binding to prefs in SwiftUI instead of custom property wrappers

This commit is contained in:
Shadowfacts 2019-11-14 19:53:00 -05:00
parent b4d41ac9b7
commit 5076aec54e
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
7 changed files with 25 additions and 94 deletions

View File

@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
0411610022B442870030A9B7 /* AttachmentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 041160FE22B442870030A9B7 /* AttachmentViewController.swift */; }; 0411610022B442870030A9B7 /* AttachmentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 041160FE22B442870030A9B7 /* AttachmentViewController.swift */; };
0411610122B442870030A9B7 /* AttachmentViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 041160FF22B442870030A9B7 /* AttachmentViewController.xib */; }; 0411610122B442870030A9B7 /* AttachmentViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 041160FF22B442870030A9B7 /* AttachmentViewController.xib */; };
0427033622B30B3D000D31B6 /* Preference.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0427033522B30B3D000D31B6 /* Preference.swift */; };
0427033822B30F5F000D31B6 /* BehaviorPrefsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0427033722B30F5F000D31B6 /* BehaviorPrefsView.swift */; }; 0427033822B30F5F000D31B6 /* BehaviorPrefsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0427033722B30F5F000D31B6 /* BehaviorPrefsView.swift */; };
0427033A22B31269000D31B6 /* AdvancedPrefsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0427033922B31269000D31B6 /* AdvancedPrefsView.swift */; }; 0427033A22B31269000D31B6 /* AdvancedPrefsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0427033922B31269000D31B6 /* AdvancedPrefsView.swift */; };
0427037C22B316B9000D31B6 /* SilentActionPrefs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0427037B22B316B9000D31B6 /* SilentActionPrefs.swift */; }; 0427037C22B316B9000D31B6 /* SilentActionPrefs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0427037B22B316B9000D31B6 /* SilentActionPrefs.swift */; };
@ -269,7 +268,6 @@
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
041160FE22B442870030A9B7 /* AttachmentViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachmentViewController.swift; sourceTree = "<group>"; }; 041160FE22B442870030A9B7 /* AttachmentViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachmentViewController.swift; sourceTree = "<group>"; };
041160FF22B442870030A9B7 /* AttachmentViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AttachmentViewController.xib; sourceTree = "<group>"; }; 041160FF22B442870030A9B7 /* AttachmentViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AttachmentViewController.xib; sourceTree = "<group>"; };
0427033522B30B3D000D31B6 /* Preference.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preference.swift; sourceTree = "<group>"; };
0427033722B30F5F000D31B6 /* BehaviorPrefsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BehaviorPrefsView.swift; sourceTree = "<group>"; }; 0427033722B30F5F000D31B6 /* BehaviorPrefsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BehaviorPrefsView.swift; sourceTree = "<group>"; };
0427033922B31269000D31B6 /* AdvancedPrefsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdvancedPrefsView.swift; sourceTree = "<group>"; }; 0427033922B31269000D31B6 /* AdvancedPrefsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdvancedPrefsView.swift; sourceTree = "<group>"; };
0427037B22B316B9000D31B6 /* SilentActionPrefs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SilentActionPrefs.swift; sourceTree = "<group>"; }; 0427037B22B316B9000D31B6 /* SilentActionPrefs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SilentActionPrefs.swift; sourceTree = "<group>"; };
@ -876,7 +874,6 @@
children = ( children = (
D663626121360B1900C9CBA2 /* Preferences.swift */, D663626121360B1900C9CBA2 /* Preferences.swift */,
D663626321360D2300C9CBA2 /* AvatarStyle.swift */, D663626321360D2300C9CBA2 /* AvatarStyle.swift */,
0427033522B30B3D000D31B6 /* Preference.swift */,
D6DD353C22F28CD000A9563A /* ContentWarningCopyMode.swift */, D6DD353C22F28CD000A9563A /* ContentWarningCopyMode.swift */,
D6DD353E22F502EC00A9563A /* Preferences+Notification.swift */, D6DD353E22F502EC00A9563A /* Preferences+Notification.swift */,
D6BC9DB4232D4CE3002CA326 /* NotificationsMode.swift */, D6BC9DB4232D4CE3002CA326 /* NotificationsMode.swift */,
@ -1606,7 +1603,6 @@
D6C693EF216192C2007D6A6D /* TuskerNavigationDelegate.swift in Sources */, D6C693EF216192C2007D6A6D /* TuskerNavigationDelegate.swift in Sources */,
D6C94D872139E62700CB5196 /* LargeImageViewController.swift in Sources */, D6C94D872139E62700CB5196 /* LargeImageViewController.swift in Sources */,
D6434EB3215B1856001A919A /* XCBRequest.swift in Sources */, D6434EB3215B1856001A919A /* XCBRequest.swift in Sources */,
0427033622B30B3D000D31B6 /* Preference.swift in Sources */,
D663626221360B1900C9CBA2 /* Preferences.swift in Sources */, D663626221360B1900C9CBA2 /* Preferences.swift in Sources */,
D6333B792139AEFD00CE884A /* Date+TimeAgo.swift in Sources */, D6333B792139AEFD00CE884A /* Date+TimeAgo.swift in Sources */,
D641C77F213DC78A004B4513 /* InlineTextAttachment.swift in Sources */, D641C77F213DC78A004B4513 /* InlineTextAttachment.swift in Sources */,

View File

@ -1,60 +0,0 @@
// 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)
}
}
}

View File

@ -73,9 +73,6 @@ class Preferences: Codable, ObservableObject {
try container.encode(statusContentType, forKey: .statusContentType) try container.encode(statusContentType, forKey: .statusContentType)
} }
typealias ObjectWillChangePublisher = PassthroughSubject<Preferences, Never>
let objectWillChange = PassthroughSubject<Preferences, Never>()
// MARK: - Appearance // MARK: - Appearance
@Published var showRepliesInProfiles = false @Published var showRepliesInProfiles = false
@Published var avatarStyle = AvatarStyle.roundRect @Published var avatarStyle = AvatarStyle.roundRect

View File

@ -9,8 +9,8 @@ import SwiftUI
import Pachyderm import Pachyderm
struct AdvancedPrefsView : View { struct AdvancedPrefsView : View {
@Preference(\.statusContentType) var statusContentType: StatusContentType @ObservedObject var preferences = Preferences.shared
var body: some View { var body: some View {
List { List {
formattingSection formattingSection
@ -25,7 +25,7 @@ struct AdvancedPrefsView : View {
var formattingSection: some View { var formattingSection: some View {
Section(footer: formattingFooter) { Section(footer: formattingFooter) {
Picker(selection: _statusContentType.binding, label: Text("Post Content Type")) { Picker(selection: $preferences.statusContentType, label: Text("Post Content Type")) {
ForEach(StatusContentType.allCases, id: \.self) { type in ForEach(StatusContentType.allCases, id: \.self) { type in
Text(type.displayName).tag(type) Text(type.displayName).tag(type)
}//.navigationBarTitle("Post Content Type") }//.navigationBarTitle("Post Content Type")

View File

@ -8,21 +8,23 @@
import SwiftUI import SwiftUI
struct AppearancePrefsView : View { struct AppearancePrefsView : View {
@Preference(\.showRepliesInProfiles) var showRepliesInProfiles: Bool @ObservedObject var preferences = Preferences.shared
@Preference(\.hideCustomEmojiInUsernames) var hideCustomEmojiInUsernames: Bool
var useCircularAvatars: Binding<Bool> = Binding(get: {
@MappedPreference(\.avatarStyle, fromPref: { $0 == .circle }, toPref: { $0 ? .circle : .roundRect }) Preferences.shared.avatarStyle == .circle
var useCircularAvatars: Bool }) {
Preferences.shared.avatarStyle = $0 ? .circle : .roundRect
}
var body: some View { var body: some View {
List { List {
Toggle(isOn: _showRepliesInProfiles.binding) { Toggle(isOn: $preferences.showRepliesInProfiles) {
Text("Show Replies in Profiles") Text("Show Replies in Profiles")
} }
Toggle(isOn: _useCircularAvatars.binding) { Toggle(isOn: useCircularAvatars) {
Text("Use Circular Avatars") Text("Use Circular Avatars")
} }
Toggle(isOn: _hideCustomEmojiInUsernames.binding) { Toggle(isOn: $preferences.hideCustomEmojiInUsernames) {
Text("Hide Custom Emoji in Usernames") Text("Hide Custom Emoji in Usernames")
} }
} }

View File

@ -8,12 +8,9 @@
import SwiftUI import SwiftUI
import Pachyderm import Pachyderm
struct BehaviorPrefsView : View { struct BehaviorPrefsView: View {
@Preference(\.defaultPostVisibility) var defaultPostVisibility: Status.Visibility @ObservedObject var preferences = Preferences.shared
@Preference(\.automaticallySaveDrafts) var automaticallySaveDrafts: Bool
@Preference(\.contentWarningCopyMode) var contentWarningCopyMode: ContentWarningCopyMode
@Preference(\.openLinksInApps) var openLinksInApps: Bool
var body: some View { var body: some View {
List { List {
section1 section1
@ -24,7 +21,7 @@ struct BehaviorPrefsView : View {
var section1: some View { var section1: some View {
Section(header: Text("COMPOSING")) { Section(header: Text("COMPOSING")) {
Picker(selection: _defaultPostVisibility.binding, label: Text("Default Post Visibility")) { Picker(selection: $preferences.defaultPostVisibility, label: Text("Default Post Visibility")) {
ForEach(Status.Visibility.allCases, id: \.self) { visibility in ForEach(Status.Visibility.allCases, id: \.self) { visibility in
HStack { HStack {
Image(systemName: visibility.imageName) Image(systemName: visibility.imageName)
@ -34,10 +31,10 @@ struct BehaviorPrefsView : View {
}//.navigationBarTitle("Default Post Visibility") }//.navigationBarTitle("Default Post Visibility")
// navbar title on the ForEach is currently incorrectly applied when the picker is not expanded, see FB6838291 // navbar title on the ForEach is currently incorrectly applied when the picker is not expanded, see FB6838291
} }
Toggle(isOn: _automaticallySaveDrafts.binding) { Toggle(isOn: $preferences.automaticallySaveDrafts) {
Text("Automatically Save Drafts") Text("Automatically Save Drafts")
} }
Picker(selection: _contentWarningCopyMode.binding, label: Text("Content Warning Copy Style")) { Picker(selection: $preferences.contentWarningCopyMode, label: Text("Content Warning Copy Style")) {
Text("As-is").tag(ContentWarningCopyMode.asIs) Text("As-is").tag(ContentWarningCopyMode.asIs)
Text("Prepend 're: '").tag(ContentWarningCopyMode.prependRe) Text("Prepend 're: '").tag(ContentWarningCopyMode.prependRe)
Text("Don't copy").tag(ContentWarningCopyMode.doNotCopy) Text("Don't copy").tag(ContentWarningCopyMode.doNotCopy)
@ -47,7 +44,7 @@ struct BehaviorPrefsView : View {
var section2: some View { var section2: some View {
Section(header: Text("READING")) { Section(header: Text("READING")) {
Toggle(isOn: _openLinksInApps.binding) { Toggle(isOn: $preferences.openLinksInApps) {
Text("Open Links in Apps") Text("Open Links in Apps")
} }
} }

View File

@ -9,9 +9,8 @@
import SwiftUI import SwiftUI
struct WellnessPrefsView: View { struct WellnessPrefsView: View {
@Preference(\.showFavoriteAndReblogCounts) var showFavoriteAndReblogCounts: Bool @ObservedObject var preferences = Preferences.shared
@Preference(\.defaultNotificationsMode) var defaultNotificationsMode: NotificationsMode
var body: some View { var body: some View {
List { List {
showFavAndReblogCountSection showFavAndReblogCountSection
@ -22,7 +21,7 @@ struct WellnessPrefsView: View {
var showFavAndReblogCountSection: some View { var showFavAndReblogCountSection: some View {
Section(footer: showFavAndReblogCountFooter) { Section(footer: showFavAndReblogCountFooter) {
Toggle(isOn: _showFavoriteAndReblogCounts.binding) { Toggle(isOn: $preferences.showFavoriteAndReblogCounts) {
Text("Show Favorite and Reblog Counts") Text("Show Favorite and Reblog Counts")
} }
} }
@ -34,7 +33,7 @@ struct WellnessPrefsView: View {
var notificationsModeSection: some View { var notificationsModeSection: some View {
Section(footer: notificationsModeFooter) { Section(footer: notificationsModeFooter) {
Picker(selection: _defaultNotificationsMode.binding, label: Text("Default Notifications Mode")) { Picker(selection: $preferences.defaultNotificationsMode, label: Text("Default Notifications Mode")) {
ForEach(NotificationsMode.allCases, id: \.self) { type in ForEach(NotificationsMode.allCases, id: \.self) { type in
Text(type.displayName).tag(type) Text(type.displayName).tag(type)
} }