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

67 lines
1.9 KiB
Swift
Raw Normal View History

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
@NSManaged public private(set) var createAt: Date
@NSManaged public private(set) var updatedAt: Date
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-04-07 13:49:33 +02:00
}
extension SearchHistory {
public override func awakeFromInsert() {
super.awakeFromInsert()
setPrimitiveValue(UUID(), forKey: #keyPath(SearchHistory.identifier))
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,
account: MastodonUser
) -> SearchHistory {
let searchHistory: SearchHistory = context.insertObject()
searchHistory.account = account
return searchHistory
}
@discardableResult
public static func insert(
into context: NSManagedObjectContext,
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-04-07 15:01:32 +02:00
searchHistory.hashtag = hashtag
2021-04-07 13:49:33 +02:00
return searchHistory
}
}
public extension SearchHistory {
func update(updatedAt: Date) {
setValue(updatedAt, forKey: #keyPath(SearchHistory.updatedAt))
}
}
2021-04-07 13:49:33 +02:00
extension SearchHistory: Managed {
public static var defaultSortDescriptors: [NSSortDescriptor] {
return [NSSortDescriptor(keyPath: \SearchHistory.updatedAt, ascending: false)]
2021-04-07 13:49:33 +02:00
}
}