2021-03-11 08:41:27 +01:00
|
|
|
//
|
|
|
|
// ComposeViewModel+Diffable.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-11.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2021-03-25 08:56:17 +01:00
|
|
|
import Combine
|
2021-03-12 12:25:28 +01:00
|
|
|
import TwitterTextEditor
|
2021-03-25 08:56:17 +01:00
|
|
|
import MastodonSDK
|
2021-03-11 08:41:27 +01:00
|
|
|
|
|
|
|
extension ComposeViewModel {
|
|
|
|
|
2021-03-12 07:18:07 +01:00
|
|
|
func setupDiffableDataSource(
|
2021-03-22 10:48:35 +01:00
|
|
|
for collectionView: UICollectionView,
|
2021-03-12 12:25:28 +01:00
|
|
|
dependency: NeedsDependency,
|
2021-03-25 08:56:17 +01:00
|
|
|
customEmojiPickerInputViewModel: CustomEmojiPickerInputViewModel,
|
2021-03-18 08:16:35 +01:00
|
|
|
textEditorViewTextAttributesDelegate: TextEditorViewTextAttributesDelegate,
|
2021-03-23 11:47:21 +01:00
|
|
|
composeStatusAttachmentTableViewCellDelegate: ComposeStatusAttachmentCollectionViewCellDelegate,
|
|
|
|
composeStatusPollOptionCollectionViewCellDelegate: ComposeStatusPollOptionCollectionViewCellDelegate,
|
2021-03-24 07:49:27 +01:00
|
|
|
composeStatusNewPollOptionCollectionViewCellDelegate: ComposeStatusPollOptionAppendEntryCollectionViewCellDelegate,
|
|
|
|
composeStatusPollExpiresOptionCollectionViewCellDelegate: ComposeStatusPollExpiresOptionCollectionViewCellDelegate
|
2021-03-12 07:18:07 +01:00
|
|
|
) {
|
2021-03-22 10:48:35 +01:00
|
|
|
let diffableDataSource = ComposeStatusSection.collectionViewDiffableDataSource(
|
|
|
|
for: collectionView,
|
2021-03-12 07:18:07 +01:00
|
|
|
dependency: dependency,
|
|
|
|
managedObjectContext: context.managedObjectContext,
|
2021-03-12 12:25:28 +01:00
|
|
|
composeKind: composeKind,
|
2021-03-25 08:56:17 +01:00
|
|
|
customEmojiPickerInputViewModel: customEmojiPickerInputViewModel,
|
2021-03-18 08:16:35 +01:00
|
|
|
textEditorViewTextAttributesDelegate: textEditorViewTextAttributesDelegate,
|
2021-03-23 11:47:21 +01:00
|
|
|
composeStatusAttachmentTableViewCellDelegate: composeStatusAttachmentTableViewCellDelegate,
|
|
|
|
composeStatusPollOptionCollectionViewCellDelegate: composeStatusPollOptionCollectionViewCellDelegate,
|
2021-03-24 07:49:27 +01:00
|
|
|
composeStatusNewPollOptionCollectionViewCellDelegate: composeStatusNewPollOptionCollectionViewCellDelegate,
|
|
|
|
composeStatusPollExpiresOptionCollectionViewCellDelegate: composeStatusPollExpiresOptionCollectionViewCellDelegate
|
2021-03-12 07:18:07 +01:00
|
|
|
)
|
2021-03-22 11:40:32 +01:00
|
|
|
|
2021-03-24 08:46:40 +01:00
|
|
|
diffableDataSource.reorderingHandlers.canReorderItem = { item in
|
|
|
|
switch item {
|
|
|
|
case .pollOption: return true
|
|
|
|
default: return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update reordered data source
|
|
|
|
diffableDataSource.reorderingHandlers.didReorder = { [weak self] transaction in
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
|
|
|
let items = transaction.finalSnapshot.itemIdentifiers
|
|
|
|
var pollOptionAttributes: [ComposeStatusItem.ComposePollOptionAttribute] = []
|
|
|
|
for item in items {
|
|
|
|
guard case let .pollOption(attribute) = item else { continue }
|
|
|
|
pollOptionAttributes.append(attribute)
|
|
|
|
}
|
|
|
|
self.pollOptionAttributes.value = pollOptionAttributes
|
|
|
|
}
|
|
|
|
|
2021-03-22 10:48:35 +01:00
|
|
|
self.diffableDataSource = diffableDataSource
|
2021-03-11 08:41:27 +01:00
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<ComposeStatusSection, ComposeStatusItem>()
|
2021-03-23 11:47:21 +01:00
|
|
|
snapshot.appendSections([.repliedTo, .status, .attachment, .poll])
|
2021-03-11 08:41:27 +01:00
|
|
|
switch composeKind {
|
2021-03-16 04:23:19 +01:00
|
|
|
case .reply(let statusObjectID):
|
|
|
|
snapshot.appendItems([.replyTo(statusObjectID: statusObjectID)], toSection: .repliedTo)
|
|
|
|
snapshot.appendItems([.input(replyToStatusObjectID: statusObjectID, attribute: composeStatusAttribute)], toSection: .repliedTo)
|
2021-03-15 06:42:46 +01:00
|
|
|
case .post:
|
2021-03-16 04:23:19 +01:00
|
|
|
snapshot.appendItems([.input(replyToStatusObjectID: nil, attribute: composeStatusAttribute)], toSection: .status)
|
2021-03-11 08:41:27 +01:00
|
|
|
}
|
|
|
|
diffableDataSource.apply(snapshot, animatingDifferences: false)
|
|
|
|
}
|
|
|
|
|
2021-03-25 08:56:17 +01:00
|
|
|
func setupCustomEmojiPickerDiffableDataSource(
|
|
|
|
for collectionView: UICollectionView,
|
|
|
|
dependency: NeedsDependency
|
|
|
|
) {
|
|
|
|
let diffableDataSource = CustomEmojiPickerSection.collectionViewDiffableDataSource(
|
|
|
|
for: collectionView,
|
|
|
|
dependency: dependency
|
|
|
|
)
|
|
|
|
self.customEmojiPickerDiffableDataSource = diffableDataSource
|
|
|
|
|
|
|
|
customEmojiViewModel
|
|
|
|
.sink { [weak self, weak diffableDataSource] customEmojiViewModel in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard let diffableDataSource = diffableDataSource else { return }
|
|
|
|
guard let customEmojiViewModel = customEmojiViewModel else {
|
|
|
|
self.customEmojiViewModelSubscription = nil
|
|
|
|
let snapshot = NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem>()
|
|
|
|
diffableDataSource.apply(snapshot)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.customEmojiViewModelSubscription = customEmojiViewModel.emojis
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self, weak diffableDataSource] emojis in
|
|
|
|
guard let _ = self else { return }
|
|
|
|
guard let diffableDataSource = diffableDataSource else { return }
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem>()
|
|
|
|
let customEmojiSection = CustomEmojiPickerSection.emoji(name: customEmojiViewModel.domain.uppercased())
|
|
|
|
snapshot.appendSections([customEmojiSection])
|
|
|
|
let items: [CustomEmojiPickerItem] = {
|
|
|
|
var items = [CustomEmojiPickerItem]()
|
|
|
|
for emoji in emojis where emoji.visibleInPicker {
|
|
|
|
let attribute = CustomEmojiPickerItem.CustomEmojiAttribute(emoji: emoji)
|
|
|
|
let item = CustomEmojiPickerItem.emoji(attribute: attribute)
|
|
|
|
items.append(item)
|
|
|
|
}
|
|
|
|
return items
|
|
|
|
}()
|
|
|
|
snapshot.appendItems(items, toSection: customEmojiSection)
|
|
|
|
diffableDataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
2021-03-11 08:41:27 +01:00
|
|
|
}
|