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-24 07:49:27 +01:00
|
|
|
case pollOption(attribute: ComposePollOptionAttribute)
|
|
|
|
case pollOptionAppendEntry
|
|
|
|
case pollExpiresOption(attribute: ComposePollExpiresOptionAttribute)
|
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-25 11:17:05 +01:00
|
|
|
let isContentWarningComposing = CurrentValueSubject<Bool, Never>(false)
|
|
|
|
let contentWarningContent = CurrentValueSubject<String, Never>("")
|
|
|
|
|
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 &&
|
2021-03-25 11:17:05 +01:00
|
|
|
lhs.username.value == rhs.username.value &&
|
|
|
|
lhs.composeContent.value == rhs.composeContent.value &&
|
|
|
|
lhs.isContentWarningComposing.value == rhs.isContentWarningComposing.value &&
|
|
|
|
lhs.contentWarningContent.value == rhs.contentWarningContent.value
|
2021-03-12 07:18:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 11:47:21 +01:00
|
|
|
|
2021-05-08 05:03:34 +02:00
|
|
|
protocol ComposePollAttributeDelegate: AnyObject {
|
2021-03-24 07:49:27 +01:00
|
|
|
func composePollAttribute(_ attribute: ComposeStatusItem.ComposePollOptionAttribute, pollOptionDidChange: String?)
|
2021-03-23 12:33:12 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 11:47:21 +01:00
|
|
|
extension ComposeStatusItem {
|
2021-03-24 07:49:27 +01:00
|
|
|
final class ComposePollOptionAttribute: Equatable, Hashable {
|
2021-03-23 11:47:21 +01:00
|
|
|
private let id = UUID()
|
|
|
|
|
2021-03-23 12:33:12 +01:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-03-24 07:49:27 +01:00
|
|
|
weak var delegate: ComposePollAttributeDelegate?
|
2021-03-23 12:33:12 +01:00
|
|
|
|
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-24 07:49:27 +01:00
|
|
|
static func == (lhs: ComposePollOptionAttribute, rhs: ComposePollOptionAttribute) -> Bool {
|
2021-03-23 11:47:21 +01:00
|
|
|
return lhs.id == rhs.id &&
|
|
|
|
lhs.option.value == rhs.option.value
|
|
|
|
}
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 07:49:27 +01:00
|
|
|
|
|
|
|
extension ComposeStatusItem {
|
|
|
|
final class ComposePollExpiresOptionAttribute: Equatable, Hashable {
|
|
|
|
private let id = UUID()
|
|
|
|
|
|
|
|
let expiresOption = CurrentValueSubject<ExpiresOption, Never>(.thirtyMinutes)
|
|
|
|
|
|
|
|
|
|
|
|
static func == (lhs: ComposePollExpiresOptionAttribute, rhs: ComposePollExpiresOptionAttribute) -> Bool {
|
|
|
|
return lhs.id == rhs.id &&
|
|
|
|
lhs.expiresOption.value == rhs.expiresOption.value
|
|
|
|
}
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ExpiresOption: Equatable, Hashable, CaseIterable {
|
|
|
|
case thirtyMinutes
|
|
|
|
case oneHour
|
|
|
|
case sixHours
|
|
|
|
case oneDay
|
|
|
|
case threeDays
|
|
|
|
case sevenDays
|
|
|
|
|
|
|
|
var title: String {
|
|
|
|
switch self {
|
|
|
|
case .thirtyMinutes: return L10n.Scene.Compose.Poll.thirtyMinutes
|
|
|
|
case .oneHour: return L10n.Scene.Compose.Poll.oneHour
|
|
|
|
case .sixHours: return L10n.Scene.Compose.Poll.sixHours
|
|
|
|
case .oneDay: return L10n.Scene.Compose.Poll.oneDay
|
|
|
|
case .threeDays: return L10n.Scene.Compose.Poll.threeDays
|
|
|
|
case .sevenDays: return L10n.Scene.Compose.Poll.sevenDays
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var seconds: Int {
|
|
|
|
switch self {
|
|
|
|
case .thirtyMinutes: return 60 * 30
|
|
|
|
case .oneHour: return 60 * 60 * 1
|
|
|
|
case .sixHours: return 60 * 60 * 6
|
|
|
|
case .oneDay: return 60 * 60 * 24
|
|
|
|
case .threeDays: return 60 * 60 * 24 * 3
|
|
|
|
case .sevenDays: return 60 * 60 * 24 * 7
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|