2022-10-10 13:14:52 +02:00
|
|
|
//
|
|
|
|
// ComposeContentViewModel+DataSource.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 22/10/10.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import MastodonCore
|
2022-11-17 01:18:49 +01:00
|
|
|
import MastodonSDK
|
2022-10-10 13:14:52 +02:00
|
|
|
import CoreDataStack
|
2022-10-11 12:31:40 +02:00
|
|
|
import UIHostingConfigurationBackport
|
2022-10-10 13:14:52 +02:00
|
|
|
|
|
|
|
extension ComposeContentViewModel {
|
|
|
|
|
|
|
|
func setupDataSource(
|
|
|
|
tableView: UITableView
|
|
|
|
) {
|
|
|
|
tableView.dataSource = self
|
|
|
|
|
|
|
|
setupTableViewCell(tableView: tableView)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ComposeContentViewModel {
|
|
|
|
enum Section: CaseIterable {
|
|
|
|
case replyTo
|
|
|
|
case status
|
|
|
|
}
|
|
|
|
|
2022-10-11 12:31:40 +02:00
|
|
|
private func setupTableViewCell(tableView: UITableView) {
|
|
|
|
composeContentTableViewCell.contentConfiguration = UIHostingConfigurationBackport {
|
|
|
|
ComposeContentView(viewModel: self)
|
|
|
|
}
|
|
|
|
|
|
|
|
$contentCellFrame
|
|
|
|
.map { $0.height }
|
|
|
|
.removeDuplicates()
|
|
|
|
.sink { [weak self] height in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard !tableView.visibleCells.isEmpty else { return }
|
|
|
|
UIView.performWithoutAnimation {
|
|
|
|
tableView.beginUpdates()
|
|
|
|
self.composeContentTableViewCell.frame.size.height = height
|
|
|
|
tableView.endUpdates()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2022-12-03 19:25:07 +01:00
|
|
|
if case .reply(let status) = destination {
|
2022-10-10 13:14:52 +02:00
|
|
|
let cell = composeReplyToTableViewCell
|
|
|
|
// bind frame publisher
|
2022-10-11 12:31:40 +02:00
|
|
|
cell.$framePublisher
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.assign(to: \.replyToCellFrame, on: self)
|
|
|
|
.store(in: &cell.disposeBag)
|
2022-10-10 13:14:52 +02:00
|
|
|
|
|
|
|
// set initial width
|
|
|
|
cell.statusView.frame.size.width = tableView.frame.width
|
|
|
|
|
|
|
|
// configure status
|
|
|
|
context.managedObjectContext.performAndWait {
|
|
|
|
guard let replyTo = status.object(in: context.managedObjectContext) else { return }
|
|
|
|
cell.statusView.configure(status: replyTo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 12:42:50 +01:00
|
|
|
// MARK: - UITableViewDataSource
|
2022-10-10 13:14:52 +02:00
|
|
|
extension ComposeContentViewModel: UITableViewDataSource {
|
|
|
|
public func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
|
return Section.allCases.count
|
|
|
|
}
|
|
|
|
|
|
|
|
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
switch Section.allCases[section] {
|
|
|
|
case .replyTo:
|
2022-12-03 19:25:07 +01:00
|
|
|
switch destination {
|
2022-10-10 13:14:52 +02:00
|
|
|
case .reply: return 1
|
|
|
|
default: return 0
|
|
|
|
}
|
|
|
|
case .status: return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
switch Section.allCases[indexPath.section] {
|
|
|
|
case .replyTo:
|
|
|
|
return composeReplyToTableViewCell
|
|
|
|
case .status:
|
2022-10-11 12:31:40 +02:00
|
|
|
return composeContentTableViewCell
|
2022-10-10 13:14:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-13 12:42:50 +01:00
|
|
|
|
|
|
|
extension ComposeContentViewModel {
|
|
|
|
|
|
|
|
func setupCustomEmojiPickerDiffableDataSource(
|
|
|
|
collectionView: UICollectionView
|
|
|
|
) {
|
|
|
|
let diffableDataSource = CustomEmojiPickerSection.collectionViewDiffableDataSource(
|
|
|
|
collectionView: collectionView,
|
|
|
|
context: context
|
|
|
|
)
|
|
|
|
self.customEmojiPickerDiffableDataSource = diffableDataSource
|
2022-11-17 01:18:49 +01:00
|
|
|
|
2022-11-13 12:42:50 +01:00
|
|
|
let domain = authContext.mastodonAuthenticationBox.domain.uppercased()
|
|
|
|
customEmojiViewModel?.emojis
|
2022-11-17 01:18:49 +01:00
|
|
|
// Don't block the main queue
|
|
|
|
.receive(on: DispatchQueue.global(qos: .userInteractive))
|
|
|
|
// Sort emojis
|
|
|
|
.map({ (emojis) -> [Mastodon.Entity.Emoji] in
|
|
|
|
return emojis.sorted { a, b in
|
|
|
|
a.shortcode.lowercased() < b.shortcode.lowercased()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Collate emojis into categories
|
|
|
|
.map({ (emojis) -> (noCategory: [Mastodon.Entity.Emoji], categorised: [String:[Mastodon.Entity.Emoji]]) in
|
|
|
|
let emojiMap: (noCategory: [Mastodon.Entity.Emoji], categorised: [String:[Mastodon.Entity.Emoji]]) = {
|
|
|
|
var noCategory = [Mastodon.Entity.Emoji]()
|
|
|
|
var categorised = [String:[Mastodon.Entity.Emoji]]()
|
|
|
|
|
|
|
|
for emoji in emojis where emoji.visibleInPicker {
|
|
|
|
if let category = emoji.category {
|
|
|
|
var categoryArray = categorised[category] ?? [Mastodon.Entity.Emoji]()
|
|
|
|
categoryArray.append(emoji)
|
|
|
|
categorised[category] = categoryArray
|
|
|
|
} else {
|
|
|
|
noCategory.append(emoji)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
noCategory,
|
|
|
|
categorised
|
|
|
|
)
|
|
|
|
}()
|
2022-11-13 12:42:50 +01:00
|
|
|
|
2022-11-17 01:18:49 +01:00
|
|
|
return emojiMap
|
|
|
|
})
|
|
|
|
// Build snapshot from emoji map
|
|
|
|
.map({ (emojiMap) -> NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem> in
|
|
|
|
|
2022-11-13 12:42:50 +01:00
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem>()
|
|
|
|
let customEmojiSection = CustomEmojiPickerSection.emoji(name: domain)
|
|
|
|
snapshot.appendSections([customEmojiSection])
|
2022-11-17 01:18:49 +01:00
|
|
|
snapshot.appendItems(emojiMap.noCategory.map({ emoji in
|
|
|
|
CustomEmojiPickerItem.emoji(attribute: CustomEmojiPickerItem.CustomEmojiAttribute(emoji: emoji))
|
|
|
|
}), toSection: customEmojiSection)
|
|
|
|
emojiMap.categorised.keys.sorted().forEach { category in
|
|
|
|
let section = CustomEmojiPickerSection.emoji(name: category)
|
|
|
|
snapshot.appendSections([section])
|
|
|
|
if let items = emojiMap.categorised[category] {
|
|
|
|
snapshot.appendItems(items.map({ emoji in
|
|
|
|
CustomEmojiPickerItem.emoji(attribute: CustomEmojiPickerItem.CustomEmojiAttribute(emoji: emoji))
|
|
|
|
}), toSection: section)
|
2022-11-13 12:42:50 +01:00
|
|
|
}
|
2022-11-17 01:18:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return snapshot
|
|
|
|
})
|
|
|
|
// Apply snapshot
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self, weak diffableDataSource] snapshot in
|
|
|
|
guard let _ = self else { return }
|
|
|
|
guard let diffableDataSource = diffableDataSource else { return }
|
|
|
|
|
2022-11-13 12:42:50 +01:00
|
|
|
diffableDataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|