mastodon-ios/CoreDataStack/Entity/Subscription.swift

88 lines
2.4 KiB
Swift
Raw Normal View History

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 {
@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
@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
// 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()
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,
property: Property,
setting: Setting
2021-04-08 13:47:31 +02:00
) -> Subscription {
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 {
public let policyRaw: String
2021-04-08 13:47:31 +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 {
public static func predicate(policyRaw: String) -> NSPredicate {
return NSPredicate(format: "%K == %@", #keyPath(Subscription.policyRaw), policyRaw)
2021-04-08 13:47:31 +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
}