2021-03-11 08:41:27 +01:00
|
|
|
//
|
|
|
|
// ComposeStatusItem.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-11.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2021-03-12 07:18:07 +01:00
|
|
|
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)
|
2021-03-17 11:09:38 +01:00
|
|
|
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 { }
|
2021-03-12 07:18:07 +01:00
|
|
|
|
|
|
|
extension ComposeStatusItem {
|
2021-03-16 04:23:19 +01:00
|
|
|
final class ComposeStatusAttribute: Equatable, Hashable {
|
2021-03-12 07:18:07 +01:00
|
|
|
private let id = UUID()
|
2021-03-23 12:33:12 +01:00
|
|
|
|
2021-03-12 07:18:07 +01:00
|
|
|
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 {
|
2021-03-12 07:18:07 +01:00
|
|
|
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
|
|
|
|
2021-03-23 12:33:12 +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()
|
|
|
|
|
2021-03-23 12:33:12 +01:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
weak var delegate: ComposeStatusItemDelegate?
|
|
|
|
|
2021-03-23 11:47:21 +01:00
|
|
|
let option = CurrentValueSubject<String, Never>("")
|
|
|
|
|
2021-03-23 12:33:12 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|