Use @Published for preferences and manually encode/decode

This commit is contained in:
Shadowfacts 2019-09-14 12:15:40 -04:00
parent 8bb6e9403d
commit 6ab8f99cc2
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 53 additions and 17 deletions

View File

@ -35,34 +35,70 @@ class Preferences: Codable, ObservableObject {
private init() {} 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<Preferences, Never> typealias ObjectWillChangePublisher = PassthroughSubject<Preferences, Never>
let objectWillChange = PassthroughSubject<Preferences, Never>() let objectWillChange = PassthroughSubject<Preferences, Never>()
// MARK: - Appearance // MARK: - Appearance
var showRepliesInProfiles = false { willSet { objectWillChange.send(self) } } @Published var showRepliesInProfiles = false
var avatarStyle = AvatarStyle.roundRect { willSet { objectWillChange.send(self) } } @Published var avatarStyle = AvatarStyle.roundRect
var hideCustomEmojiInUsernames = false { willSet { objectWillChange.send(self) } } @Published var hideCustomEmojiInUsernames = false
// MARK: - Behavior // MARK: - Behavior
var defaultPostVisibility = Status.Visibility.public { willSet { objectWillChange.send(self) } } @Published var defaultPostVisibility = Status.Visibility.public
var automaticallySaveDrafts = true { willSet { objectWillChange.send(self) } } @Published var automaticallySaveDrafts = true
var contentWarningCopyMode = ContentWarningCopyMode.asIs { willSet { objectWillChange.send(self) } } @Published var contentWarningCopyMode = ContentWarningCopyMode.asIs
var openLinksInApps = true { willSet { objectWillChange.send(self) } } @Published var openLinksInApps = true
// MARK: - Advanced // MARK: - Advanced
var silentActions: [String: Permission] = [:] { willSet { objectWillChange.send(self) } } @Published var silentActions: [String: Permission] = [:]
var statusContentType: StatusContentType = .plain { willSet { objectWillChange.send(self) } } @Published var statusContentType: StatusContentType = .plain
}
extension PassthroughSubject: Codable { enum CodingKeys: String, CodingKey {
public convenience init(from decoder: Decoder) throws { case showRepliesInProfiles
self.init() 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 { extension Preferences {