mastodon-ios/CoreDataStack/Entity/HomeTimelineIndex.swift

80 lines
1.9 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-01-28 09:10:30 +01:00
@NSManaged public private(set) var toot: Toot
2021-01-27 07:50:13 +01:00
}
extension HomeTimelineIndex {
@discardableResult
public static func insert(
into context: NSManagedObjectContext,
property: Property,
2021-01-28 09:10:30 +01:00
toot: Toot
2021-01-27 07:50:13 +01:00
) -> HomeTimelineIndex {
let index: HomeTimelineIndex = context.insertObject()
index.identifier = property.identifier
index.domain = property.domain
index.userID = toot.author.id
2021-01-28 09:10:30 +01:00
index.createdAt = toot.createdAt
2021-01-27 07:50:13 +01:00
2021-01-28 09:10:30 +01:00
index.toot = toot
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 Toot 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
public init(domain: String) {
self.identifier = UUID().uuidString + "@" + domain
self.domain = domain
}
}
}
extension HomeTimelineIndex: Managed {
public static var defaultSortDescriptors: [NSSortDescriptor] {
return [NSSortDescriptor(keyPath: \HomeTimelineIndex.createdAt, ascending: false)]
}
}