2021-04-07 13:49:33 +02:00
|
|
|
//
|
|
|
|
// SearchHistory.swift
|
|
|
|
// CoreDataStack
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/4/7.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import CoreData
|
|
|
|
|
|
|
|
public final class SearchHistory: NSManagedObject {
|
|
|
|
public typealias ID = UUID
|
|
|
|
@NSManaged public private(set) var identifier: ID
|
2021-07-22 06:56:20 +02:00
|
|
|
@NSManaged public private(set) var domain: String
|
|
|
|
@NSManaged public private(set) var userID: MastodonUser.ID
|
2021-04-07 13:49:33 +02:00
|
|
|
@NSManaged public private(set) var createAt: Date
|
2021-04-08 06:22:05 +02:00
|
|
|
@NSManaged public private(set) var updatedAt: Date
|
2021-07-15 14:28:36 +02:00
|
|
|
|
|
|
|
// one-to-one relationship
|
2021-04-07 13:49:33 +02:00
|
|
|
@NSManaged public private(set) var account: MastodonUser?
|
2021-04-07 15:01:32 +02:00
|
|
|
@NSManaged public private(set) var hashtag: Tag?
|
2021-07-15 14:28:36 +02:00
|
|
|
@NSManaged public private(set) var status: Status?
|
2021-04-07 13:49:33 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchHistory {
|
|
|
|
public override func awakeFromInsert() {
|
|
|
|
super.awakeFromInsert()
|
|
|
|
setPrimitiveValue(UUID(), forKey: #keyPath(SearchHistory.identifier))
|
2021-04-08 06:22:05 +02:00
|
|
|
setPrimitiveValue(Date(), forKey: #keyPath(SearchHistory.createAt))
|
|
|
|
setPrimitiveValue(Date(), forKey: #keyPath(SearchHistory.updatedAt))
|
|
|
|
}
|
|
|
|
|
|
|
|
public override func willSave() {
|
|
|
|
super.willSave()
|
|
|
|
setPrimitiveValue(Date(), forKey: #keyPath(SearchHistory.updatedAt))
|
2021-04-07 13:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
public static func insert(
|
|
|
|
into context: NSManagedObjectContext,
|
2021-07-22 06:56:20 +02:00
|
|
|
property: Property,
|
2021-04-07 13:49:33 +02:00
|
|
|
account: MastodonUser
|
|
|
|
) -> SearchHistory {
|
|
|
|
let searchHistory: SearchHistory = context.insertObject()
|
2021-07-22 06:56:20 +02:00
|
|
|
searchHistory.domain = property.domain
|
|
|
|
searchHistory.userID = property.userID
|
2021-04-07 13:49:33 +02:00
|
|
|
searchHistory.account = account
|
|
|
|
return searchHistory
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
public static func insert(
|
|
|
|
into context: NSManagedObjectContext,
|
2021-07-22 06:56:20 +02:00
|
|
|
property: Property,
|
2021-04-07 15:01:32 +02:00
|
|
|
hashtag: Tag
|
2021-04-07 13:49:33 +02:00
|
|
|
) -> SearchHistory {
|
|
|
|
let searchHistory: SearchHistory = context.insertObject()
|
2021-07-22 06:56:20 +02:00
|
|
|
searchHistory.domain = property.domain
|
|
|
|
searchHistory.userID = property.userID
|
2021-04-07 15:01:32 +02:00
|
|
|
searchHistory.hashtag = hashtag
|
2021-04-07 13:49:33 +02:00
|
|
|
return searchHistory
|
|
|
|
}
|
2021-07-15 14:28:36 +02:00
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
public static func insert(
|
|
|
|
into context: NSManagedObjectContext,
|
2021-07-22 06:56:20 +02:00
|
|
|
property: Property,
|
2021-07-15 14:28:36 +02:00
|
|
|
status: Status
|
|
|
|
) -> SearchHistory {
|
|
|
|
let searchHistory: SearchHistory = context.insertObject()
|
2021-07-22 06:56:20 +02:00
|
|
|
searchHistory.domain = property.domain
|
|
|
|
searchHistory.userID = property.userID
|
2021-07-15 14:28:36 +02:00
|
|
|
searchHistory.status = status
|
|
|
|
return searchHistory
|
|
|
|
}
|
2021-04-07 13:49:33 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 06:56:20 +02:00
|
|
|
extension SearchHistory {
|
|
|
|
public func update(updatedAt: Date) {
|
2021-04-08 06:22:05 +02:00
|
|
|
setValue(updatedAt, forKey: #keyPath(SearchHistory.updatedAt))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 06:56:20 +02:00
|
|
|
extension SearchHistory {
|
|
|
|
public struct Property {
|
|
|
|
public let domain: String
|
|
|
|
public let userID: MastodonUser.ID
|
|
|
|
|
|
|
|
public init(domain: String, userID: MastodonUser.ID) {
|
|
|
|
self.domain = domain
|
|
|
|
self.userID = userID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 13:49:33 +02:00
|
|
|
extension SearchHistory: Managed {
|
|
|
|
public static var defaultSortDescriptors: [NSSortDescriptor] {
|
2021-04-08 06:22:05 +02:00
|
|
|
return [NSSortDescriptor(keyPath: \SearchHistory.updatedAt, ascending: false)]
|
2021-04-07 13:49:33 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 08:13:49 +02:00
|
|
|
|
|
|
|
extension SearchHistory {
|
|
|
|
static func predicate(domain: String) -> NSPredicate {
|
|
|
|
return NSPredicate(format: "%K == %@", #keyPath(SearchHistory.domain), domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func predicate(userID: String) -> NSPredicate {
|
|
|
|
return NSPredicate(format: "%K == %@", #keyPath(SearchHistory.userID), userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
public static func predicate(domain: String, userID: String) -> NSPredicate {
|
|
|
|
return NSCompoundPredicate(andPredicateWithSubpredicates: [
|
|
|
|
predicate(domain: domain),
|
|
|
|
predicate(userID: userID)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|