mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/CoreDataStack/Entity/App/Setting.swift

127 lines
3.8 KiB
Swift
Raw Normal View History

2021-04-08 13:47:31 +02:00
//
// Setting.swift
// CoreDataStack
//
// Created by ihugo on 2021/4/9.
//
import CoreData
import Foundation
public final class Setting: NSManagedObject {
@NSManaged public var domain: String
@NSManaged public var userID: String
// @NSManaged public var appearanceRaw: String
@available(*, deprecated, message: "We need a new core data version, so we postpone this.")
@NSManaged public var preferredTrueBlackDarkMode: Bool
@NSManaged public var preferredStaticAvatar: Bool
2021-07-23 13:33:05 +02:00
@NSManaged public var preferredStaticEmoji: Bool
@NSManaged public var preferredUsingDefaultBrowser: Bool
2021-04-08 13:47:31 +02:00
@NSManaged public private(set) var createdAt: Date
@NSManaged public private(set) var updatedAt: Date
@NSManaged private var rawRecentLanguages: Data?
@objc dynamic public var recentLanguages: [String] {
get {
if let data = rawRecentLanguages, let result = try? JSONDecoder().decode([String].self, from: data) {
return result
}
return []
}
set {
rawRecentLanguages = try? JSONEncoder().encode(Array(newValue.prefix(3)))
}
}
// one-to-many relationships
@NSManaged public var subscriptions: Set<Subscription>?
2021-04-08 13:47:31 +02:00
}
extension Setting {
2021-04-08 13:47:31 +02:00
public override func awakeFromInsert() {
super.awakeFromInsert()
let now = Date()
setPrimitiveValue(now, forKey: #keyPath(Setting.createdAt))
setPrimitiveValue(now, forKey: #keyPath(Setting.updatedAt))
2021-04-08 13:47:31 +02:00
}
@discardableResult
public static func insert(
2021-04-08 13:47:31 +02:00
into context: NSManagedObjectContext,
property: Property
) -> Setting {
let setting: Setting = context.insertObject()
// setting.appearanceRaw = property.appearanceRaw
2021-04-08 13:47:31 +02:00
setting.domain = property.domain
setting.userID = property.userID
2021-04-08 13:47:31 +02:00
return setting
}
// public func update(appearanceRaw: String) {
// guard appearanceRaw != self.appearanceRaw else { return }
// self.appearanceRaw = appearanceRaw
// didUpdate(at: Date())
// }
2021-07-05 10:07:17 +02:00
public func update(preferredStaticAvatar: Bool) {
guard preferredStaticAvatar != self.preferredStaticAvatar else { return }
self.preferredStaticAvatar = preferredStaticAvatar
didUpdate(at: Date())
}
2021-07-23 13:33:05 +02:00
public func update(preferredStaticEmoji: Bool) {
guard preferredStaticEmoji != self.preferredStaticEmoji else { return }
self.preferredStaticEmoji = preferredStaticEmoji
didUpdate(at: Date())
}
public func update(preferredUsingDefaultBrowser: Bool) {
guard preferredUsingDefaultBrowser != self.preferredUsingDefaultBrowser else { return }
self.preferredUsingDefaultBrowser = preferredUsingDefaultBrowser
didUpdate(at: Date())
}
2021-04-08 13:47:31 +02:00
public func didUpdate(at networkDate: Date) {
self.updatedAt = networkDate
2021-04-08 13:47:31 +02:00
}
2021-04-08 13:47:31 +02:00
}
extension Setting {
public struct Property {
2021-04-08 13:47:31 +02:00
public let domain: String
public let userID: String
// public let appearanceRaw: String
2021-04-08 13:47:31 +02:00
public init(
domain: String,
userID: String
// appearanceRaw: String
) {
2021-04-08 13:47:31 +02:00
self.domain = domain
self.userID = userID
// self.appearanceRaw = appearanceRaw
2021-04-08 13:47:31 +02:00
}
}
}
extension Setting: Managed {
public static var defaultSortDescriptors: [NSSortDescriptor] {
return [NSSortDescriptor(keyPath: \Setting.createdAt, ascending: false)]
}
}
extension Setting {
public static func predicate(domain: String, userID: String) -> NSPredicate {
return NSPredicate(format: "%K == %@ AND %K == %@",
#keyPath(Setting.domain), domain,
#keyPath(Setting.userID), userID
)
2021-04-08 13:47:31 +02:00
}
}