mastodon-app-ufficiale-ipho.../CoreDataStack/Entity/SubscriptionAlerts.swift

131 lines
3.5 KiB
Swift
Raw Normal View History

2021-04-08 13:47:31 +02:00
//
// PushSubscriptionAlerts+CoreDataClass.swift
// CoreDataStack
//
// Created by ihugo on 2021/4/9.
//
//
import Foundation
import CoreData
public final class SubscriptionAlerts: NSManagedObject {
2021-04-12 15:42:43 +02:00
@NSManaged public var follow: NSNumber?
@NSManaged public var favourite: NSNumber?
@NSManaged public var reblog: NSNumber?
@NSManaged public var mention: NSNumber?
@NSManaged public var poll: NSNumber?
2021-04-08 13:47:31 +02:00
@NSManaged public private(set) var createdAt: Date
@NSManaged public private(set) var updatedAt: Date
// MARK: - relationships
@NSManaged public var pushSubscription: Subscription?
}
public extension SubscriptionAlerts {
override func awakeFromInsert() {
super.awakeFromInsert()
setPrimitiveValue(Date(), forKey: #keyPath(SubscriptionAlerts.createdAt))
}
func didUpdate(at networkDate: Date) {
self.updatedAt = networkDate
}
@discardableResult
static func insert(
into context: NSManagedObjectContext,
property: Property
) -> SubscriptionAlerts {
let alerts: SubscriptionAlerts = context.insertObject()
alerts.favourite = property.favourite
alerts.follow = property.follow
alerts.mention = property.mention
alerts.poll = property.poll
alerts.reblog = property.reblog
return alerts
}
2021-04-12 15:42:43 +02:00
func update(favourite: NSNumber?) {
2021-04-08 13:47:31 +02:00
guard self.favourite != favourite else { return }
self.favourite = favourite
didUpdate(at: Date())
}
2021-04-12 15:42:43 +02:00
func update(follow: NSNumber?) {
2021-04-08 13:47:31 +02:00
guard self.follow != follow else { return }
self.follow = follow
didUpdate(at: Date())
}
2021-04-12 15:42:43 +02:00
func update(mention: NSNumber?) {
2021-04-08 13:47:31 +02:00
guard self.mention != mention else { return }
self.mention = mention
didUpdate(at: Date())
}
2021-04-12 15:42:43 +02:00
func update(poll: NSNumber?) {
2021-04-08 13:47:31 +02:00
guard self.poll != poll else { return }
self.poll = poll
didUpdate(at: Date())
}
2021-04-12 15:42:43 +02:00
func update(reblog: NSNumber?) {
2021-04-08 13:47:31 +02:00
guard self.reblog != reblog else { return }
self.reblog = reblog
didUpdate(at: Date())
}
}
public extension SubscriptionAlerts {
struct Property {
2021-04-12 15:42:43 +02:00
public let favourite: NSNumber?
public let follow: NSNumber?
public let mention: NSNumber?
public let poll: NSNumber?
public let reblog: NSNumber?
2021-04-08 13:47:31 +02:00
2021-04-12 15:42:43 +02:00
public init(favourite: NSNumber?, follow: NSNumber?, mention: NSNumber?, poll: NSNumber?, reblog: NSNumber?) {
self.favourite = favourite
self.follow = follow
self.mention = mention
self.poll = poll
self.reblog = reblog
2021-04-08 13:47:31 +02:00
}
}
func updateIfNeed(property: Property) {
if self.follow != property.follow {
self.follow = property.follow
}
if self.favourite != property.favourite {
self.favourite = property.favourite
}
if self.reblog != property.reblog {
self.reblog = property.reblog
}
if self.mention != property.mention {
self.mention = property.mention
}
if self.poll != property.poll {
self.poll = property.poll
}
}
}
extension SubscriptionAlerts: Managed {
public static var defaultSortDescriptors: [NSSortDescriptor] {
return [NSSortDescriptor(keyPath: \SubscriptionAlerts.createdAt, ascending: false)]
}
}