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

93 lines
2.3 KiB
Swift
Raw Normal View History

2021-01-27 07:50:13 +01:00
//
// HomeTimelineIndex.swift
// CoreDataStack
//
// Created by MainasuK Cirno on 2021/1/27.
//
import Foundation
import CoreData
2021-01-28 09:10:30 +01:00
final public class HomeTimelineIndex: NSManagedObject {
2021-01-27 07:50:13 +01:00
public typealias ID = String
@NSManaged public private(set) var identifier: ID
@NSManaged public private(set) var domain: String
@NSManaged public private(set) var userID: String
@NSManaged public private(set) var hasMore: Bool // default NO
2021-01-27 07:50:13 +01:00
@NSManaged public private(set) var createdAt: Date
@NSManaged public private(set) var deletedAt: Date?
2021-01-27 07:50:13 +01:00
// many-to-one relationship
2021-04-01 08:39:15 +02:00
@NSManaged public private(set) var status: Status
2021-01-27 07:50:13 +01:00
}
extension HomeTimelineIndex {
@discardableResult
public static func insert(
into context: NSManagedObjectContext,
property: Property,
2021-04-01 08:39:15 +02:00
status: Status
2021-01-27 07:50:13 +01:00
) -> HomeTimelineIndex {
let index: HomeTimelineIndex = context.insertObject()
index.identifier = property.identifier
index.domain = property.domain
2021-02-07 07:42:50 +01:00
index.userID = property.userID
2021-04-01 08:39:15 +02:00
index.createdAt = status.createdAt
2021-01-27 07:50:13 +01:00
2021-04-01 08:39:15 +02:00
index.status = status
2021-01-27 07:50:13 +01:00
return index
}
public func update(hasMore: Bool) {
if self.hasMore != hasMore {
self.hasMore = hasMore
}
}
// internal method for status call
func softDelete() {
deletedAt = Date()
}
2021-01-27 07:50:13 +01:00
}
extension HomeTimelineIndex {
public struct Property {
public let identifier: String
public let domain: String
2021-02-07 07:42:50 +01:00
public let userID: String
public init(domain: String,userID: String) {
2021-01-27 07:50:13 +01:00
self.identifier = UUID().uuidString + "@" + domain
self.domain = domain
2021-02-07 07:42:50 +01:00
self.userID = userID
2021-01-27 07:50:13 +01:00
}
}
}
extension HomeTimelineIndex: Managed {
public static var defaultSortDescriptors: [NSSortDescriptor] {
return [NSSortDescriptor(keyPath: \HomeTimelineIndex.createdAt, ascending: false)]
}
}
2021-02-07 07:42:50 +01:00
extension HomeTimelineIndex {
public static func predicate(userID: String) -> NSPredicate {
return NSPredicate(format: "%K == %@", #keyPath(HomeTimelineIndex.userID), userID)
}
public static func notDeleted() -> NSPredicate {
return NSPredicate(format: "%K == nil", #keyPath(HomeTimelineIndex.deletedAt))
}
}