-
-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathDiscoverySection.swift
More file actions
103 lines (91 loc) · 4.38 KB
/
DiscoverySection.swift
File metadata and controls
103 lines (91 loc) · 4.38 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
//
// DiscoverySection.swift
// Mastodon
//
// Created by MainasuK on 2022-4-13.
//
import UIKit
import MastodonCore
import MastodonUI
import MastodonSDK
enum DiscoverySection: CaseIterable {
// case posts
case hashtags
case news
case forYou
}
extension DiscoverySection {
class Configuration {
let authenticationBox: MastodonAuthenticationBox
weak var profileCardTableViewCellDelegate: ProfileCardTableViewCellDelegate?
let familiarFollowers: Published<[Mastodon.Entity.FamiliarFollowers]>.Publisher?
public init(
authenticationBox: MastodonAuthenticationBox,
profileCardTableViewCellDelegate: ProfileCardTableViewCellDelegate? = nil,
familiarFollowers: Published<[Mastodon.Entity.FamiliarFollowers]>.Publisher? = nil
) {
self.authenticationBox = authenticationBox
self.profileCardTableViewCellDelegate = profileCardTableViewCellDelegate
self.familiarFollowers = familiarFollowers
}
}
static func diffableDataSource(
tableView: UITableView,
configuration: Configuration
) -> UITableViewDiffableDataSource<DiscoverySection, DiscoveryItem> {
tableView.register(TrendTableViewCell.self, forCellReuseIdentifier: String(describing: TrendTableViewCell.self))
tableView.register(NewsTableViewCell.self, forCellReuseIdentifier: String(describing: NewsTableViewCell.self))
tableView.register(ProfileCardTableViewCell.self, forCellReuseIdentifier: String(describing: ProfileCardTableViewCell.self))
tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))
return UITableViewDiffableDataSource(tableView: tableView) {
tableView,
indexPath,
item in
switch item {
case .hashtag(let tag):
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TrendTableViewCell.self), for: indexPath) as? TrendTableViewCell else {
assertionFailure("unexpected cell dequeued")
return nil
}
cell.trendView.configure(tag: tag)
return cell
case .link(let link):
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: NewsTableViewCell.self), for: indexPath) as? NewsTableViewCell else {
assertionFailure("unexpected cell dequeued")
return nil
}
cell.newsView.configure(link: link)
return cell
case .account(let account, relationship: let relationship):
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ProfileCardTableViewCell.self), for: indexPath) as?
ProfileCardTableViewCell else {
assertionFailure("unexpected cell dequeued")
return nil
}
cell.configure(
tableView: tableView,
account: account,
relationship: relationship,
profileCardTableViewCellDelegate: configuration.profileCardTableViewCellDelegate
)
// bind familiarFollowers
if let familiarFollowers = configuration.familiarFollowers {
familiarFollowers
.map { array in array.first(where: { $0.id == account.id }) }
.assign(to: \.familiarFollowers, on: cell.profileCardView.viewModel)
.store(in: &cell.disposeBag)
} else {
cell.profileCardView.viewModel.familiarFollowers = nil
}
return cell
case .bottomLoader:
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self), for: indexPath) as? TimelineBottomLoaderTableViewCell else {
assertionFailure("unexpected cell dequeued")
return nil
}
cell.activityIndicatorView.startAnimating()
return cell
}
}
}
}