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
|
2021-02-04 07:45:44 +01:00
|
|
|
@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
|
2021-02-04 07:45:44 +01:00
|
|
|
@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
|
|
|
|
}
|
|
|
|
|
2021-02-04 07:45:44 +01:00
|
|
|
public func update(hasMore: Bool) {
|
|
|
|
if self.hasMore != hasMore {
|
|
|
|
self.hasMore = hasMore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 14:06:36 +02:00
|
|
|
// internal method for status call
|
2021-02-04 07:45:44 +01:00
|
|
|
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
|
|
|
|
|
2021-07-26 10:31:57 +02:00
|
|
|
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 {
|
|
|
|
|
2021-07-26 10:31:57 +02:00
|
|
|
static func predicate(domain: String) -> NSPredicate {
|
|
|
|
return NSPredicate(format: "%K == %@", #keyPath(HomeTimelineIndex.domain), domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func predicate(userID: MastodonUser.ID) -> NSPredicate {
|
2021-02-07 07:42:50 +01:00
|
|
|
return NSPredicate(format: "%K == %@", #keyPath(HomeTimelineIndex.userID), userID)
|
|
|
|
}
|
2021-07-26 10:31:57 +02:00
|
|
|
|
|
|
|
public static func predicate(domain: String, userID: MastodonUser.ID) -> NSPredicate {
|
|
|
|
return NSCompoundPredicate(andPredicateWithSubpredicates: [
|
|
|
|
predicate(domain: domain),
|
|
|
|
predicate(userID: userID)
|
|
|
|
])
|
|
|
|
}
|
2021-02-07 07:42:50 +01:00
|
|
|
|
|
|
|
public static func notDeleted() -> NSPredicate {
|
|
|
|
return NSPredicate(format: "%K == nil", #keyPath(HomeTimelineIndex.deletedAt))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|