mastodon-ios/Mastodon/Diffiable/Item/Item.swift

94 lines
2.9 KiB
Swift
Raw Normal View History

2021-01-28 09:10:30 +01:00
//
// Item.swift
// Mastodon
//
// Created by sxiaojian on 2021/1/27.
//
import CoreData
import CoreDataStack
2021-02-03 06:01:50 +01:00
import Foundation
import MastodonSDK
2021-01-28 09:10:30 +01:00
/// Note: update Equatable when change case
enum Item {
// timeline
2021-03-05 06:41:48 +01:00
case homeTimelineIndex(objectID: NSManagedObjectID, attribute: StatusAttribute)
2021-02-07 07:42:50 +01:00
2021-01-28 09:10:30 +01:00
// normal list
2021-03-05 06:41:48 +01:00
case toot(objectID: NSManagedObjectID, attribute: StatusAttribute)
2021-02-03 06:01:50 +01:00
2021-02-04 08:09:58 +01:00
// loader
2021-02-07 07:42:50 +01:00
case homeMiddleLoader(upperTimelineIndexAnchorObjectID: NSManagedObjectID)
case publicMiddleLoader(tootID: String)
2021-02-03 06:01:50 +01:00
case bottomLoader
2021-01-28 09:10:30 +01:00
}
protocol StatusContentWarningAttribute {
var isStatusTextSensitive: Bool { get set }
var isStatusSensitive: Bool { get set }
}
2021-02-07 07:42:50 +01:00
extension Item {
class StatusAttribute: Equatable, Hashable, StatusContentWarningAttribute {
var isStatusTextSensitive: Bool
var isStatusSensitive: Bool
2021-02-07 07:42:50 +01:00
public init(
isStatusTextSensitive: Bool,
isStatusSensitive: Bool
) {
self.isStatusTextSensitive = isStatusTextSensitive
self.isStatusSensitive = isStatusSensitive
}
2021-03-05 06:41:48 +01:00
static func == (lhs: Item.StatusAttribute, rhs: Item.StatusAttribute) -> Bool {
return lhs.isStatusTextSensitive == rhs.isStatusTextSensitive &&
lhs.isStatusSensitive == rhs.isStatusSensitive
2021-02-07 07:42:50 +01:00
}
func hash(into hasher: inout Hasher) {
hasher.combine(isStatusTextSensitive)
hasher.combine(isStatusSensitive)
2021-02-07 07:42:50 +01:00
}
}
}
2021-01-28 09:10:30 +01:00
extension Item: Equatable {
static func == (lhs: Item, rhs: Item) -> Bool {
switch (lhs, rhs) {
2021-02-07 07:42:50 +01:00
case (.homeTimelineIndex(let objectIDLeft, _), .homeTimelineIndex(let objectIDRight, _)):
return objectIDLeft == objectIDRight
case (.toot(let objectIDLeft, _), .toot(let objectIDRight, _)):
2021-01-28 09:10:30 +01:00
return objectIDLeft == objectIDRight
2021-02-04 04:12:17 +01:00
case (.bottomLoader, .bottomLoader):
return true
2021-02-07 07:42:50 +01:00
case (.publicMiddleLoader(let upperLeft), .publicMiddleLoader(let upperRight)):
return upperLeft == upperRight
case (.homeMiddleLoader(let upperLeft), .homeMiddleLoader(let upperRight)):
2021-02-04 08:09:58 +01:00
return upperLeft == upperRight
2021-02-03 06:01:50 +01:00
default:
return false
2021-01-28 09:10:30 +01:00
}
}
}
extension Item: Hashable {
func hash(into hasher: inout Hasher) {
switch self {
2021-02-07 07:42:50 +01:00
case .homeTimelineIndex(let objectID, _):
hasher.combine(objectID)
case .toot(let objectID, _):
2021-01-28 09:10:30 +01:00
hasher.combine(objectID)
2021-02-07 07:42:50 +01:00
case .publicMiddleLoader(let upper):
hasher.combine(String(describing: Item.publicMiddleLoader.self))
hasher.combine(upper)
case .homeMiddleLoader(upperTimelineIndexAnchorObjectID: let upper):
hasher.combine(String(describing: Item.homeMiddleLoader.self))
2021-02-04 08:09:58 +01:00
hasher.combine(upper)
2021-02-03 06:01:50 +01:00
case .bottomLoader:
hasher.combine(String(describing: Item.bottomLoader.self))
2021-01-28 09:10:30 +01:00
}
}
}