2021-03-11 08:41:27 +01:00
|
|
|
//
|
|
|
|
// ComposeStatusSection.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-11.
|
|
|
|
//
|
|
|
|
|
2021-03-12 07:18:07 +01:00
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
2021-03-11 08:41:27 +01:00
|
|
|
|
|
|
|
enum ComposeStatusSection: Equatable, Hashable {
|
|
|
|
case repliedTo
|
|
|
|
case status
|
|
|
|
}
|
2021-03-12 07:18:07 +01:00
|
|
|
|
|
|
|
extension ComposeStatusSection {
|
|
|
|
enum ComposeKind {
|
|
|
|
case toot
|
|
|
|
case replyToot(tootObjectID: NSManagedObjectID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ComposeStatusSection {
|
|
|
|
static func tableViewDiffableDataSource(
|
|
|
|
for tableView: UITableView,
|
|
|
|
dependency: NeedsDependency,
|
|
|
|
managedObjectContext: NSManagedObjectContext,
|
|
|
|
composeKind: ComposeKind
|
|
|
|
) -> UITableViewDiffableDataSource<ComposeStatusSection, ComposeStatusItem> {
|
|
|
|
UITableViewDiffableDataSource<ComposeStatusSection, ComposeStatusItem>(tableView: tableView) { tableView, indexPath, item -> UITableViewCell? in
|
|
|
|
switch item {
|
|
|
|
case .replyTo(let tootObjectID):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ComposeRepliedToTootContentTableViewCell.self), for: indexPath) as! ComposeRepliedToTootContentTableViewCell
|
|
|
|
// TODO:
|
|
|
|
return cell
|
|
|
|
case .toot(let replyToTootObjectID, let attribute):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ComposeTootContentTableViewCell.self), for: indexPath) as! ComposeTootContentTableViewCell
|
|
|
|
managedObjectContext.perform {
|
|
|
|
guard let replyToTootObjectID = replyToTootObjectID,
|
|
|
|
let replyTo = managedObjectContext.object(with: replyToTootObjectID) as? Toot else {
|
|
|
|
cell.statusView.headerContainerStackView.isHidden = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cell.statusView.headerContainerStackView.isHidden = false
|
|
|
|
cell.statusView.headerInfoLabel.text = "[TODO] \(replyTo.author.displayName)"
|
|
|
|
}
|
2021-03-12 08:57:58 +01:00
|
|
|
ComposeStatusSection.configure(cell: cell, attribute: attribute)
|
2021-03-12 07:18:07 +01:00
|
|
|
// self size input cell
|
|
|
|
cell.composeContent
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { text in
|
|
|
|
tableView.beginUpdates()
|
|
|
|
tableView.endUpdates()
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ComposeStatusSection {
|
2021-03-12 08:57:58 +01:00
|
|
|
static func configure(
|
2021-03-12 07:18:07 +01:00
|
|
|
cell: ComposeTootContentTableViewCell,
|
|
|
|
attribute: ComposeStatusItem.ComposeTootAttribute
|
|
|
|
) {
|
2021-03-12 08:57:58 +01:00
|
|
|
// set avatar
|
2021-03-12 07:18:07 +01:00
|
|
|
attribute.avatarURL
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { avatarURL in
|
|
|
|
cell.statusView.configure(with: AvatarConfigurableViewConfiguration(avatarImageURL: avatarURL))
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-12 08:57:58 +01:00
|
|
|
// set display name and username
|
2021-03-12 07:18:07 +01:00
|
|
|
Publishers.CombineLatest(
|
|
|
|
attribute.displayName.eraseToAnyPublisher(),
|
|
|
|
attribute.username.eraseToAnyPublisher()
|
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { displayName, username in
|
|
|
|
cell.statusView.nameLabel.text = displayName
|
|
|
|
cell.statusView.usernameLabel.text = username
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-12 08:57:58 +01:00
|
|
|
|
|
|
|
// bind compose content
|
|
|
|
cell.composeContent
|
|
|
|
.map { $0 as String? }
|
|
|
|
.assign(to: \.value, on: attribute.composeContent)
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-12 07:18:07 +01:00
|
|
|
}
|
|
|
|
}
|