-
Notifications
You must be signed in to change notification settings - Fork 52
/
VisitsPreviewList.swift
127 lines (87 loc) · 3.85 KB
/
VisitsPreviewList.swift
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
// Kevin Li - 1:18 PM - 7/3/20
import SwiftUI
fileprivate class HostingCell: UITableViewCell {
static let identifier = "HostingCell"
private var hostingController: UIHostingController<AnyView>?
func configure(with view: AnyView) {
if let hostingController = hostingController {
hostingController.rootView = view
} else {
let controller = UIHostingController(rootView: view)
hostingController = controller
let rootView = controller.view!
rootView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(rootView)
NSLayoutConstraint.activate([
rootView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
rootView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
rootView.topAnchor.constraint(equalTo: contentView.topAnchor),
rootView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
}
layoutIfNeeded()
}
}
struct VisitsPreviewList: UIViewRepresentable {
@Environment(\.autoTimer) private var autoTimer: AutoTimer
typealias UIViewType = UITableView
let visitsProvider: VisitsProvider
let listScrollState: ListScrollState
func makeUIView(context: Context) -> UITableView {
let tableView = UITableView.visitsPreview(source: context.coordinator)
listScrollState.attach(to: tableView)
return tableView
}
func updateUIView(_ tableView: UITableView, context: Context) { }
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UITableViewDataSource {
private let parent: VisitsPreviewList
private var listScrollState: ListScrollState {
parent.listScrollState
}
private var visitsProvider: VisitsProvider {
parent.visitsProvider
}
init(_ parent: VisitsPreviewList) {
self.parent = parent
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
listScrollState.descendingDayComponents.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: HostingCell.identifier) as! HostingCell
let index = indexPath.row
let rootView = dayVisitsView(
dayComponent: visitsProvider.descendingDayComponents[index],
isFilled: (index % 2) == 0)
.id(index) // id is crucial as it resets state for a reused cell
.erased
cell.configure(with: rootView)
return cell
}
private func dayVisitsView(dayComponent: DateComponents, isFilled: Bool) -> DayVisitsView {
DayVisitsView(date: dayComponent.date,
visits: visitsProvider.visitsForDayComponents[dayComponent] ?? [],
isFilled: isFilled)
}
}
}
private extension UITableView {
static func visitsPreview(source: UITableViewDataSource) -> UITableView {
let tableView = UITableView()
tableView.showsVerticalScrollIndicator = false
tableView.allowsSelection = false
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.scrollsToTop = false
// Both are crucial towards making the scroll smoother and necessary for the tableview
// to scroll to a specific row properly
tableView.rowHeight = Constants.List.blockHeight
tableView.estimatedRowHeight = Constants.List.blockHeight
tableView.dataSource = source
tableView.register(HostingCell.self, forCellReuseIdentifier: HostingCell.identifier)
return tableView
}
}