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

83 lines
2.5 KiB
Swift
Raw Normal View History

2021-03-11 08:41:27 +01:00
//
// ComposeStatusItem.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-3-11.
//
import Foundation
import Combine
2021-03-11 08:41:27 +01:00
import CoreData
2021-03-23 11:47:21 +01:00
/// Note: update Equatable when change case
2021-03-11 08:41:27 +01:00
enum ComposeStatusItem {
2021-03-16 04:23:19 +01:00
case replyTo(statusObjectID: NSManagedObjectID)
case input(replyToStatusObjectID: NSManagedObjectID?, attribute: ComposeStatusAttribute)
case attachment(attachmentService: MastodonAttachmentService)
2021-03-23 11:47:21 +01:00
case poll(attribute: ComposePollAttribute)
case newPoll
2021-03-11 08:41:27 +01:00
}
2021-03-23 11:47:21 +01:00
extension ComposeStatusItem: Equatable { }
2021-03-11 08:41:27 +01:00
extension ComposeStatusItem: Hashable { }
extension ComposeStatusItem {
2021-03-16 04:23:19 +01:00
final class ComposeStatusAttribute: Equatable, Hashable {
private let id = UUID()
let avatarURL = CurrentValueSubject<URL?, Never>(nil)
let displayName = CurrentValueSubject<String?, Never>(nil)
let username = CurrentValueSubject<String?, Never>(nil)
let composeContent = CurrentValueSubject<String?, Never>(nil)
2021-03-16 04:23:19 +01:00
static func == (lhs: ComposeStatusAttribute, rhs: ComposeStatusAttribute) -> Bool {
return lhs.avatarURL.value == rhs.avatarURL.value &&
lhs.displayName.value == rhs.displayName.value &&
lhs.username.value == rhs.username.value &&
lhs.composeContent.value == rhs.composeContent.value
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
}
2021-03-23 11:47:21 +01:00
protocol ComposeStatusItemDelegate: class {
func composePollAttribute(_ attribute: ComposeStatusItem.ComposePollAttribute, pollOptionDidChange: String?)
}
2021-03-23 11:47:21 +01:00
extension ComposeStatusItem {
final class ComposePollAttribute: Equatable, Hashable {
private let id = UUID()
var disposeBag = Set<AnyCancellable>()
weak var delegate: ComposeStatusItemDelegate?
2021-03-23 11:47:21 +01:00
let option = CurrentValueSubject<String, Never>("")
init() {
option
.sink { [weak self] option in
guard let self = self else { return }
self.delegate?.composePollAttribute(self, pollOptionDidChange: option)
}
.store(in: &disposeBag)
}
deinit {
disposeBag.removeAll()
}
2021-03-23 11:47:21 +01:00
static func == (lhs: ComposePollAttribute, rhs: ComposePollAttribute) -> Bool {
return lhs.id == rhs.id &&
lhs.option.value == rhs.option.value
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
}