-
-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathComposeContentViewModel+DataSource.swift
More file actions
176 lines (152 loc) · 6.5 KB
/
ComposeContentViewModel+DataSource.swift
File metadata and controls
176 lines (152 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// ComposeContentViewModel+DataSource.swift
//
//
// Created by MainasuK on 22/10/10.
//
import UIKit
import MastodonCore
import MastodonSDK
import CoreDataStack
import UIHostingConfigurationBackport
extension ComposeContentViewModel {
func setupDataSource(
tableView: UITableView
) {
tableView.dataSource = self
setupTableViewCell(tableView: tableView)
}
}
extension ComposeContentViewModel {
enum Section: CaseIterable {
case replyTo
case status
}
private func setupTableViewCell(tableView: UITableView) {
composeContentTableViewCell.contentConfiguration = UIHostingConfigurationBackport {
ComposeContentView(viewModel: self)
}
$contentCellFrame
.map { $0.height }
.removeDuplicates()
.throttle(for: 0.5, scheduler: RunLoop.main, latest: true)
.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)
if case .reply(let status) = destination {
let cell = composeReplyToTableViewCell
// bind frame publisher
cell.$framePublisher
.receive(on: DispatchQueue.main)
.assign(to: \.replyToCellFrame, on: self)
.store(in: &cell.disposeBag)
// 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)
}
}
}
}
// MARK: - UITableViewDataSource
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:
switch destination {
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:
return composeContentTableViewCell
}
}
}
extension ComposeContentViewModel {
func setupCustomEmojiPickerDiffableDataSource(
collectionView: UICollectionView
) {
let diffableDataSource = CustomEmojiPickerSection.collectionViewDiffableDataSource(
collectionView: collectionView,
context: context
)
self.customEmojiPickerDiffableDataSource = diffableDataSource
let domain = authContext.mastodonAuthenticationBox.domain.uppercased()
customEmojiViewModel?.emojis
// 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
)
}()
return emojiMap
})
// Build snapshot from emoji map
.map({ (emojiMap) -> NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem> in
var snapshot = NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem>()
let customEmojiSection = CustomEmojiPickerSection.emoji(name: domain)
snapshot.appendSections([customEmojiSection])
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)
}
}
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 }
diffableDataSource.apply(snapshot)
}
.store(in: &disposeBag)
}
}