-
-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathSearchResultSection.swift
More file actions
111 lines (99 loc) · 4.91 KB
/
SearchResultSection.swift
File metadata and controls
111 lines (99 loc) · 4.91 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
//
// SearchResultSection.swift
// Mastodon
//
// Created by sxiaojian on 2021/4/6.
//
import Foundation
import MastodonSDK
import UIKit
import CoreData
import CoreDataStack
import MastodonAsset
import MastodonCore
import MastodonLocalization
import MastodonUI
import Combine
enum SearchResultSection: Hashable {
case main
}
extension SearchResultSection {
struct Configuration {
let authenticationBox: MastodonAuthenticationBox
weak var statusViewTableViewCellDelegate: StatusTableViewCellDelegate?
weak var userTableViewCellDelegate: UserTableViewCellDelegate?
}
static func tableViewDiffableDataSource(
tableView: UITableView,
authenticationBox: MastodonAuthenticationBox,
configuration: Configuration
) -> UITableViewDiffableDataSource<SearchResultSection, SearchResultItem> {
tableView.register(UserTableViewCell.self, forCellReuseIdentifier: String(describing: UserTableViewCell.self))
tableView.register(StatusTableViewCell.self, forCellReuseIdentifier: String(describing: StatusTableViewCell.self))
tableView.register(HashtagTableViewCell.self, forCellReuseIdentifier: String(describing: HashtagTableViewCell.self))
tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))
return UITableViewDiffableDataSource(tableView: tableView) { tableView, indexPath, item -> UITableViewCell? in
switch item {
case .account(let account, let relationship):
guard let cell = tableView.dequeueReusableCell(withIdentifier: UserTableViewCell.reuseIdentifier, for: indexPath) as? UserTableViewCell else { fatalError("WTF?! Wrong cell.") }
guard let me = authenticationBox.cachedAccount else { return cell }
cell.userView.setButtonState(.loading)
cell.configure(
me: me,
tableView: tableView,
account: account,
relationship: relationship,
delegate: configuration.userTableViewCellDelegate
)
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: nil) // no filters in search results
configure(
tableView: tableView,
cell: cell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel(displayItem: displayItem, contentConcealModel: contentConcealModel),
configuration: configuration
)
return cell
case .hashtag(let tag):
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: HashtagTableViewCell.self)) as? HashtagTableViewCell else { fatalError("WTF?! Wrong cell.") }
cell.primaryLabel.configure(content: PlaintextMetaContent(string: "#" + tag.name))
return cell
case .bottomLoader(let attribute):
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self)) as! TimelineBottomLoaderTableViewCell
if attribute.isNoResult {
cell.stopAnimating()
cell.loadMoreLabel.text = L10n.Scene.Search.Searching.EmptyState.noResults
cell.loadMoreLabel.textColor = Asset.Colors.Label.secondary.color
cell.loadMoreLabel.isHidden = false
} else {
cell.startAnimating()
cell.loadMoreLabel.isHidden = true
}
return cell
}
} // end UITableViewDiffableDataSource
} // end func
}
extension SearchResultSection {
static func configure(
tableView: UITableView,
cell: StatusTableViewCell,
viewModel: StatusTableViewCell.StatusTableViewCellViewModel,
configuration: Configuration
) {
StatusSection.setupStatusPollDataSource(
authenticationBox: configuration.authenticationBox,
statusView: cell.statusView
)
cell.statusView.viewModel.authenticationBox = configuration.authenticationBox
cell.configure(
tableView: tableView,
viewModel: viewModel,
delegate: configuration.statusViewTableViewCellDelegate
)
}
}