From 6ab8f99cc29bf1f295568250e68907680ebdf22b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 14 Sep 2019 12:15:40 -0400 Subject: [PATCH] Use @Published for preferences and manually encode/decode --- Tusker/Preferences/Preferences.swift | 70 +++++++++++++++++++++------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/Tusker/Preferences/Preferences.swift b/Tusker/Preferences/Preferences.swift index 50d9eb51..e8e60836 100644 --- a/Tusker/Preferences/Preferences.swift +++ b/Tusker/Preferences/Preferences.swift @@ -35,34 +35,70 @@ class Preferences: Codable, ObservableObject { private init() {} + required init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + + self.showRepliesInProfiles = try container.decode(Bool.self, forKey: .showRepliesInProfiles) + self.avatarStyle = try container.decode(AvatarStyle.self, forKey: .avatarStyle) + self.hideCustomEmojiInUsernames = try container.decode(Bool.self, forKey: .hideCustomEmojiInUsernames) + + self.defaultPostVisibility = try container.decode(Status.Visibility.self, forKey: .defaultPostVisibility) + self.automaticallySaveDrafts = try container.decode(Bool.self, forKey: .automaticallySaveDrafts) + self.contentWarningCopyMode = try container.decode(ContentWarningCopyMode.self, forKey: .contentWarningCopyMode) + self.openLinksInApps = try container.decode(Bool.self, forKey: .openLinksInApps) + + self.silentActions = try container.decode([String: Permission].self, forKey: .silentActions) + self.statusContentType = try container.decode(StatusContentType.self, forKey: .statusContentType) + } + + func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + + try container.encode(showRepliesInProfiles, forKey: .showRepliesInProfiles) + try container.encode(avatarStyle, forKey: .avatarStyle) + try container.encode(hideCustomEmojiInUsernames, forKey: .hideCustomEmojiInUsernames) + + try container.encode(defaultPostVisibility, forKey: .defaultPostVisibility) + try container.encode(automaticallySaveDrafts, forKey: .automaticallySaveDrafts) + try container.encode(contentWarningCopyMode, forKey: .contentWarningCopyMode) + try container.encode(openLinksInApps, forKey: .openLinksInApps) + + try container.encode(silentActions, forKey: .silentActions) + try container.encode(statusContentType, forKey: .statusContentType) + } + typealias ObjectWillChangePublisher = PassthroughSubject let objectWillChange = PassthroughSubject() // MARK: - Appearance - var showRepliesInProfiles = false { willSet { objectWillChange.send(self) } } - var avatarStyle = AvatarStyle.roundRect { willSet { objectWillChange.send(self) } } - var hideCustomEmojiInUsernames = false { willSet { objectWillChange.send(self) } } + @Published var showRepliesInProfiles = false + @Published var avatarStyle = AvatarStyle.roundRect + @Published var hideCustomEmojiInUsernames = false // MARK: - Behavior - var defaultPostVisibility = Status.Visibility.public { willSet { objectWillChange.send(self) } } - var automaticallySaveDrafts = true { willSet { objectWillChange.send(self) } } - var contentWarningCopyMode = ContentWarningCopyMode.asIs { willSet { objectWillChange.send(self) } } - var openLinksInApps = true { willSet { objectWillChange.send(self) } } + @Published var defaultPostVisibility = Status.Visibility.public + @Published var automaticallySaveDrafts = true + @Published var contentWarningCopyMode = ContentWarningCopyMode.asIs + @Published var openLinksInApps = true // MARK: - Advanced - var silentActions: [String: Permission] = [:] { willSet { objectWillChange.send(self) } } - var statusContentType: StatusContentType = .plain { willSet { objectWillChange.send(self) } } - -} + @Published var silentActions: [String: Permission] = [:] + @Published var statusContentType: StatusContentType = .plain -extension PassthroughSubject: Codable { - public convenience init(from decoder: Decoder) throws { - self.init() + enum CodingKeys: String, CodingKey { + case showRepliesInProfiles + case avatarStyle + case hideCustomEmojiInUsernames + + case defaultPostVisibility + case automaticallySaveDrafts + case contentWarningCopyMode + case openLinksInApps + + case silentActions + case statusContentType } - public func encode(to encoder: Encoder) throws { - - } } extension Preferences {