-
-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathStatusSection.swift
More file actions
249 lines (220 loc) · 10.9 KB
/
StatusSection.swift
File metadata and controls
249 lines (220 loc) · 10.9 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// TimelineSection.swift
// Mastodon
//
// Created by sxiaojian on 2021/1/27.
//
import Combine
import CoreData
import CoreDataStack
import UIKit
import AVKit
import AlamofireImage
import MastodonMeta
import MastodonSDK
import NaturalLanguage
import MastodonCore
import MastodonUI
enum StatusSection: Equatable, Hashable {
case main
}
extension StatusSection {
struct Configuration {
let authenticationBox: MastodonAuthenticationBox
weak var statusTableViewCellDelegate: StatusTableViewCellDelegate?
weak var timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate?
let filterContext: Mastodon.Entity.FilterContext?
}
static func diffableDataSource(
tableView: UITableView,
configuration: Configuration
) -> UITableViewDiffableDataSource<StatusSection, StatusItem> {
tableView.register(StatusTableViewCell.self, forCellReuseIdentifier: String(describing: StatusTableViewCell.self))
tableView.register(TimelineMiddleLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineMiddleLoaderTableViewCell.self))
tableView.register(StatusThreadRootTableViewCell.self, forCellReuseIdentifier: String(describing: StatusThreadRootTableViewCell.self))
tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))
return UITableViewDiffableDataSource(tableView: tableView) { tableView, indexPath, item -> UITableViewCell? in
switch item {
case .feed(let feed):
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: StatusTableViewCell.self), for: indexPath) as? StatusTableViewCell else { fatalError("WTF?! Wrong cell.") }
let displayItem = StatusTableViewCell.StatusTableViewCellViewModel.DisplayItem.feed(feed)
let contentConcealModel = StatusView.ContentConcealViewModel(status: feed.status, filterBox: StatusFilterService.shared.activeFilterBox, filterContext: configuration.filterContext)
configure(
tableView: tableView,
cell: cell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel(displayItem: displayItem, contentConcealModel: contentConcealModel),
configuration: configuration
)
return cell
case .feedLoader(let feed):
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineMiddleLoaderTableViewCell.self), for: indexPath) as? TimelineMiddleLoaderTableViewCell else { fatalError("WTF?! Wrong cell.") }
configure(
cell: cell,
feed: feed,
configuration: configuration
)
return cell
case .status(let status):
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: StatusTableViewCell.self), for: indexPath) as? StatusTableViewCell else { fatalError("WTF?! Wrong cell.") }
let displayItem = StatusTableViewCell.StatusTableViewCellViewModel.DisplayItem.status(status)
let contentConcealModel = StatusView.ContentConcealViewModel(status: status, filterBox: StatusFilterService.shared.activeFilterBox, filterContext: configuration.filterContext)
configure(
tableView: tableView,
cell: cell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel(displayItem: displayItem, contentConcealModel: contentConcealModel),
configuration: configuration
)
return cell
case .thread(let thread):
let cell = dequeueConfiguredReusableCell(
tableView: tableView,
indexPath: indexPath,
configuration: ThreadCellRegistrationConfiguration(
thread: thread,
configuration: configuration
)
)
return cell
case .topLoader:
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self), for: indexPath) as? TimelineBottomLoaderTableViewCell else { fatalError("WTF?! Wrong cell.") }
cell.activityIndicatorView.startAnimating()
return cell
case .bottomLoader:
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self), for: indexPath) as? TimelineBottomLoaderTableViewCell else { fatalError("WTF?! Wrong cell.") }
cell.activityIndicatorView.startAnimating()
return cell
}
}
} // end func
}
extension StatusSection {
struct ThreadCellRegistrationConfiguration {
let thread: StatusItem.Thread
let configuration: Configuration
}
static func dequeueConfiguredReusableCell(
tableView: UITableView,
indexPath: IndexPath,
configuration: ThreadCellRegistrationConfiguration
) -> UITableViewCell {
switch configuration.thread {
case .root(let threadContext):
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: StatusThreadRootTableViewCell.self), for: indexPath) as! StatusThreadRootTableViewCell
let contentConcealModel = StatusView.ContentConcealViewModel(status: threadContext.status, filterBox: StatusFilterService.shared.activeFilterBox, filterContext: .thread)
StatusSection.configure(
tableView: tableView,
cell: cell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel(displayItem: .status(threadContext.status), contentConcealModel: contentConcealModel),
configuration: configuration.configuration
)
return cell
case .reply(let threadContext),
.leaf(let threadContext):
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: StatusTableViewCell.self), for: indexPath) as! StatusTableViewCell
let displayItem = StatusTableViewCell.StatusTableViewCellViewModel.DisplayItem.status(threadContext.status)
let contentConcealModel = StatusView.ContentConcealViewModel(status: threadContext.status, filterBox: StatusFilterService.shared.activeFilterBox, filterContext: configuration.configuration.filterContext)
assert(configuration.configuration.filterContext == .thread)
StatusSection.configure(
tableView: tableView, cell: cell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel(displayItem: displayItem, contentConcealModel: contentConcealModel),
configuration: configuration.configuration
)
return cell
}
}
}
extension StatusSection {
public static func setupStatusPollDataSource(
authenticationBox: MastodonAuthenticationBox,
statusView: StatusView
) {
statusView.pollTableViewDiffableDataSource = UITableViewDiffableDataSource<PollSection, PollItem>(tableView: statusView.pollTableView) { tableView, indexPath, item in
switch item {
case .history:
return nil
case .option(let record):
// Fix cell reuse animation issue
let cell: PollOptionTableViewCell = {
let _cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PollOptionTableViewCell.self) + "@\(indexPath.row)#\(indexPath.section)") as? PollOptionTableViewCell
_cell?.prepareForReuse()
return _cell ?? PollOptionTableViewCell()
}()
cell.pollOptionView.viewModel.authenticationBox = authenticationBox
cell.pollOptionView.configure(pollOption: record)
return cell
}
}
var _snapshot = NSDiffableDataSourceSnapshot<PollSection, PollItem>()
_snapshot.appendSections([.main])
statusView.pollTableViewDiffableDataSource?.applySnapshotUsingReloadData(_snapshot)
}
}
extension StatusSection {
public static func setupStatusPollHistoryDataSource(
context: AppContext,
authenticationBox: MastodonAuthenticationBox,
statusView: StatusView
) {
statusView.pollTableViewDiffableDataSource = UITableViewDiffableDataSource<PollSection, PollItem>(tableView: statusView.pollTableView) { tableView, indexPath, item in
switch item {
case .option:
return nil
case let .history(option):
// Fix cell reuse animation issue
let cell: PollOptionTableViewCell = {
let _cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PollOptionTableViewCell.self) + "@\(indexPath.row)#\(indexPath.section)") as? PollOptionTableViewCell
_cell?.prepareForReuse()
return _cell ?? PollOptionTableViewCell()
}()
cell.pollOptionView.configure(historyPollOption: option)
return cell
}
}
}
}
extension StatusSection {
static func configure(
tableView: UITableView,
cell: StatusTableViewCell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel,
configuration: Configuration
) {
setupStatusPollDataSource(
authenticationBox: configuration.authenticationBox,
statusView: cell.statusView
)
cell.statusView.viewModel.authenticationBox = configuration.authenticationBox
cell.configure(
tableView: tableView,
viewModel: viewModel,
delegate: configuration.statusTableViewCellDelegate
)
}
static func configure(
tableView: UITableView,
cell: StatusThreadRootTableViewCell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel,
configuration: Configuration
) {
setupStatusPollDataSource(
authenticationBox: configuration.authenticationBox,
statusView: cell.statusView
)
cell.statusView.viewModel.authenticationBox = configuration.authenticationBox
cell.configure(
tableView: tableView,
viewModel: viewModel,
delegate: configuration.statusTableViewCellDelegate
)
}
static func configure(
cell: TimelineMiddleLoaderTableViewCell,
feed: MastodonFeed,
configuration: Configuration
) {
cell.configure(
feed: feed,
delegate: configuration.timelineMiddleLoaderTableViewCellDelegate
)
}
}