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() {}
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>
let objectWillChange = PassthroughSubject<Preferences, Never>()
// 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
}
enum CodingKeys: String, CodingKey {
case showRepliesInProfiles
case avatarStyle
case hideCustomEmojiInUsernames
extension PassthroughSubject: Codable {
public convenience init(from decoder: Decoder) throws {
self.init()
case defaultPostVisibility
case automaticallySaveDrafts
case contentWarningCopyMode
case openLinksInApps
case silentActions
case statusContentType
}
public func encode(to encoder: Encoder) throws {
}
}
extension Preferences {