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

112 lines
3.6 KiB
Swift
Raw Normal View History

2021-04-12 10:31:53 +02:00
//
// MastodonNotification.swift
// CoreDataStack
//
// Created by sxiaojian on 2021/4/13.
//
import Foundation
import CoreData
public final class MastodonNotification: NSManagedObject {
public typealias ID = UUID
@NSManaged public private(set) var identifier: ID
@NSManaged public private(set) var id: String
@NSManaged public private(set) var createAt: Date
@NSManaged public private(set) var updatedAt: Date
2021-04-16 07:45:54 +02:00
@NSManaged public private(set) var typeRaw: String
2021-04-12 10:31:53 +02:00
@NSManaged public private(set) var account: MastodonUser
@NSManaged public private(set) var status: Status?
2021-04-16 07:45:54 +02:00
@NSManaged public private(set) var domain: String
@NSManaged public private(set) var userID: String
2021-04-12 10:31:53 +02:00
}
extension MastodonNotification {
public override func awakeFromInsert() {
super.awakeFromInsert()
setPrimitiveValue(UUID(), forKey: #keyPath(MastodonNotification.identifier))
}
}
public extension MastodonNotification {
@discardableResult
static func insert(
into context: NSManagedObjectContext,
domain: String,
2021-04-16 07:45:54 +02:00
userID: String,
networkDate: Date,
2021-04-12 10:31:53 +02:00
property: Property
) -> MastodonNotification {
let notification: MastodonNotification = context.insertObject()
notification.id = property.id
notification.createAt = property.createdAt
2021-04-16 07:45:54 +02:00
notification.updatedAt = networkDate
notification.typeRaw = property.typeRaw
2021-04-12 10:31:53 +02:00
notification.account = property.account
notification.status = property.status
notification.domain = domain
2021-04-16 07:45:54 +02:00
notification.userID = userID
2021-04-12 10:31:53 +02:00
return notification
}
}
public extension MastodonNotification {
struct Property {
public init(id: String,
2021-04-16 07:45:54 +02:00
typeRaw: String,
2021-04-12 10:31:53 +02:00
account: MastodonUser,
status: Status?,
2021-04-16 07:45:54 +02:00
createdAt: Date
) {
2021-04-12 10:31:53 +02:00
self.id = id
2021-04-16 07:45:54 +02:00
self.typeRaw = typeRaw
2021-04-12 10:31:53 +02:00
self.account = account
self.status = status
self.createdAt = createdAt
}
public let id: String
2021-04-16 07:45:54 +02:00
public let typeRaw: String
2021-04-12 10:31:53 +02:00
public let account: MastodonUser
public let status: Status?
public let createdAt: Date
}
}
extension MastodonNotification {
2021-04-16 07:45:54 +02:00
static func predicate(domain: String) -> NSPredicate {
2021-04-12 10:31:53 +02:00
return NSPredicate(format: "%K == %@", #keyPath(MastodonNotification.domain), domain)
}
2021-04-16 07:45:54 +02:00
static func predicate(userID: String) -> NSPredicate {
return NSPredicate(format: "%K == %@", #keyPath(MastodonNotification.userID), userID)
}
static func predicate(typeRaw: String) -> NSPredicate {
return NSPredicate(format: "%K == %@", #keyPath(MastodonNotification.typeRaw), typeRaw)
2021-04-12 10:31:53 +02:00
}
2021-04-16 07:45:54 +02:00
public static func predicate(domain: String, userID: String, typeRaw: String? = nil) -> NSPredicate {
if let typeRaw = typeRaw {
return NSCompoundPredicate(andPredicateWithSubpredicates: [
MastodonNotification.predicate(domain: domain),
MastodonNotification.predicate(typeRaw: typeRaw),
MastodonNotification.predicate(userID: userID),
])
} else {
return NSCompoundPredicate(andPredicateWithSubpredicates: [
MastodonNotification.predicate(domain: domain),
MastodonNotification.predicate(userID: userID)
])
}
2021-04-12 10:31:53 +02:00
}
2021-04-13 15:31:49 +02:00
2021-04-12 10:31:53 +02:00
}
extension MastodonNotification: Managed {
public static var defaultSortDescriptors: [NSSortDescriptor] {
return [NSSortDescriptor(keyPath: \MastodonNotification.createAt, ascending: false)]
2021-04-12 10:31:53 +02:00
}
}