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 {
|
2021-04-26 10:57:50 +02:00
|
|
|
|
|
|
|
@NSManaged public var domain: String
|
|
|
|
@NSManaged public var userID: String
|
2021-07-07 08:55:41 +02:00
|
|
|
|
2021-04-08 13:47:31 +02:00
|
|
|
@NSManaged public private(set) var createdAt: Date
|
|
|
|
@NSManaged public private(set) var updatedAt: Date
|
|
|
|
|
2023-01-24 01:50:10 +01:00
|
|
|
@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)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
// one-to-many relationships
|
|
|
|
@NSManaged public var subscriptions: Set<Subscription>?
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
extension Setting {
|
2021-04-08 13:47:31 +02:00
|
|
|
|
2021-04-26 10:57:50 +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
|
2021-04-26 10:57:50 +02:00
|
|
|
public static func insert(
|
2021-04-08 13:47:31 +02:00
|
|
|
into context: NSManagedObjectContext,
|
|
|
|
property: Property
|
|
|
|
) -> Setting {
|
|
|
|
let setting: Setting = context.insertObject()
|
|
|
|
setting.domain = property.domain
|
2021-04-17 20:02:08 +02:00
|
|
|
setting.userID = property.userID
|
2021-04-08 13:47:31 +02:00
|
|
|
return setting
|
|
|
|
}
|
2021-07-05 10:07:17 +02:00
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
public func didUpdate(at networkDate: Date) {
|
|
|
|
self.updatedAt = networkDate
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
extension Setting {
|
|
|
|
public struct Property {
|
2021-04-08 13:47:31 +02:00
|
|
|
public let domain: String
|
2021-04-17 20:02:08 +02:00
|
|
|
public let userID: String
|
2021-04-08 13:47:31 +02:00
|
|
|
|
2021-10-08 12:47:46 +02:00
|
|
|
public init(
|
|
|
|
domain: String,
|
|
|
|
userID: String
|
|
|
|
) {
|
2021-04-08 13:47:31 +02:00
|
|
|
self.domain = domain
|
2021-04-17 20:02:08 +02:00
|
|
|
self.userID = userID
|
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 {
|
2021-04-17 20:02:08 +02:00
|
|
|
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
|
|
|
}
|
2023-06-30 09:14:38 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Setting {
|
|
|
|
public var activeSubscription: Subscription? {
|
|
|
|
return (subscriptions ?? Set())
|
|
|
|
.sorted(by: { $0.activedAt > $1.activedAt })
|
|
|
|
.first
|
|
|
|
}
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|