2021-04-08 13:47:31 +02:00
|
|
|
//
|
|
|
|
// SettingNotification+CoreDataClass.swift
|
|
|
|
// CoreDataStack
|
|
|
|
//
|
|
|
|
// Created by ihugo on 2021/4/9.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import CoreData
|
|
|
|
|
|
|
|
public final class Subscription: NSManagedObject {
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
@NSManaged public var id: String?
|
|
|
|
@NSManaged public var endpoint: String?
|
|
|
|
@NSManaged public var policyRaw: String
|
|
|
|
@NSManaged public var serverKey: String?
|
|
|
|
@NSManaged public var userToken: String?
|
2021-04-08 13:47:31 +02:00
|
|
|
|
|
|
|
@NSManaged public private(set) var createdAt: Date
|
|
|
|
@NSManaged public private(set) var updatedAt: Date
|
2021-04-26 10:57:50 +02:00
|
|
|
@NSManaged public private(set) var activedAt: Date
|
|
|
|
|
|
|
|
// MARK: one-to-one relationships
|
|
|
|
@NSManaged public var alert: SubscriptionAlerts
|
2021-04-08 13:47:31 +02:00
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
// MARK: many-to-one relationships
|
2021-04-08 13:47:31 +02:00
|
|
|
@NSManaged public var setting: Setting?
|
|
|
|
}
|
|
|
|
|
|
|
|
public extension Subscription {
|
|
|
|
override func awakeFromInsert() {
|
|
|
|
super.awakeFromInsert()
|
2021-04-26 10:57:50 +02:00
|
|
|
let now = Date()
|
|
|
|
setPrimitiveValue(now, forKey: #keyPath(Subscription.createdAt))
|
|
|
|
setPrimitiveValue(now, forKey: #keyPath(Subscription.updatedAt))
|
|
|
|
setPrimitiveValue(now, forKey: #keyPath(Subscription.activedAt))
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(activedAt: Date) {
|
|
|
|
self.activedAt = activedAt
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func didUpdate(at networkDate: Date) {
|
|
|
|
self.updatedAt = networkDate
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
static func insert(
|
|
|
|
into context: NSManagedObjectContext,
|
2021-04-26 10:57:50 +02:00
|
|
|
property: Property,
|
|
|
|
setting: Setting
|
2021-04-08 13:47:31 +02:00
|
|
|
) -> Subscription {
|
2021-04-26 10:57:50 +02:00
|
|
|
let subscription: Subscription = context.insertObject()
|
|
|
|
subscription.policyRaw = property.policyRaw
|
|
|
|
subscription.setting = setting
|
|
|
|
return subscription
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public extension Subscription {
|
|
|
|
struct Property {
|
2021-04-26 10:57:50 +02:00
|
|
|
public let policyRaw: String
|
2021-04-08 13:47:31 +02:00
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
public init(policyRaw: String) {
|
|
|
|
self.policyRaw = policyRaw
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Subscription: Managed {
|
|
|
|
public static var defaultSortDescriptors: [NSSortDescriptor] {
|
|
|
|
return [NSSortDescriptor(keyPath: \Subscription.createdAt, ascending: false)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Subscription {
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
public static func predicate(policyRaw: String) -> NSPredicate {
|
|
|
|
return NSPredicate(format: "%K == %@", #keyPath(Subscription.policyRaw), policyRaw)
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-27 12:05:29 +02:00
|
|
|
public static func predicate(userToken: String) -> NSPredicate {
|
|
|
|
return NSPredicate(format: "%K == %@", #keyPath(Subscription.userToken), userToken)
|
|
|
|
}
|
|
|
|
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|