-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathChartStackSection.swift
255 lines (211 loc) · 10.4 KB
/
ChartStackSection.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
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
250
251
252
253
254
255
//
// ChartStackSection.swift
// GraphTest
//
// Created by Andrei Salavei on 4/13/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import UIKit
import GraphCore
import Display
private enum Constants {
static let chartViewHeightFraction: CGFloat = 0.55
}
private class LeftAlignedIconButton: UIButton {
override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
var titleRect = super.titleRect(forContentRect: contentRect)
let imageSize = currentImage?.size ?? .zero
titleRect.origin.x = imageSize.width
return titleRect
}
override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
var imageRect = super.imageRect(forContentRect: contentRect)
imageRect.origin.x = 0.0
return imageRect
}
}
class ChartStackSection: UIView, ChartThemeContainer {
var chartView: ChartView
var rangeView: RangeChartView
var visibilityView: ChartVisibilityView
var sectionContainerView: UIView
var titleLabel: UILabel!
var backButton: UIButton!
var controller: BaseChartController?
var theme: ChartTheme?
var strings: ChartStrings?
var displayRange: Bool = true
let hapticFeedback = HapticFeedback()
init() {
sectionContainerView = UIView()
chartView = ChartView()
rangeView = RangeChartView()
visibilityView = ChartVisibilityView()
titleLabel = UILabel()
backButton = LeftAlignedIconButton()
super.init(frame: CGRect())
self.addSubview(sectionContainerView)
sectionContainerView.addSubview(chartView)
sectionContainerView.addSubview(rangeView)
sectionContainerView.addSubview(visibilityView)
sectionContainerView.addSubview(titleLabel)
sectionContainerView.addSubview(backButton)
titleLabel.font = UIFont.systemFont(ofSize: 14, weight: .bold)
titleLabel.textAlignment = .center
visibilityView.clipsToBounds = true
backButton.isExclusiveTouch = true
backButton.addTarget(self, action: #selector(self.didTapBackButton), for: .touchUpInside)
backButton.setTitle("Zoom Out", for: .normal)
backButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
backButton.setTitleColor(UIColor(rgb: 0x007aff), for: .normal)
backButton.setImage(UIImage(bundleImageName: "Chart/arrow_left"), for: .normal)
backButton.imageEdgeInsets = UIEdgeInsets(top: 0.0, left: 6.0, bottom: 0.0, right: 3.0)
backButton.imageView?.tintColor = UIColor(rgb: 0x007aff)
backButton.adjustsImageWhenHighlighted = false
backButton.setVisible(false, animated: false)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
titleLabel.font = UIFont.systemFont(ofSize: 14, weight: .bold)
visibilityView.clipsToBounds = true
backButton.isExclusiveTouch = true
backButton.setVisible(false, animated: false)
}
public func resetDetailsView() {
controller?.cancelChartInteraction()
}
func apply(theme: ChartTheme, strings: ChartStrings, animated: Bool) {
self.theme = theme
self.strings = strings
self.backButton.setTitle(strings.zoomOut, for: .normal)
UIView.perform(animated: animated && self.isVisibleInWindow) {
self.sectionContainerView.backgroundColor = theme.chartBackgroundColor
self.rangeView.backgroundColor = theme.chartBackgroundColor
self.visibilityView.backgroundColor = theme.chartBackgroundColor
self.backButton.tintColor = theme.actionButtonColor
self.backButton.setTitleColor(theme.actionButtonColor, for: .normal)
self.backButton.imageView?.tintColor = theme.actionButtonColor
}
if rangeView.isVisibleInWindow || chartView.isVisibleInWindow {
chartView.loadDetailsViewIfNeeded()
chartView.apply(theme: theme, strings: strings, animated: animated && chartView.isVisibleInWindow)
controller?.apply(theme: theme, strings: strings, animated: animated)
rangeView.apply(theme: theme, strings: strings, animated: animated && rangeView.isVisibleInWindow)
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + TimeInterval.random(in: 0...0.1)) {
self.chartView.loadDetailsViewIfNeeded()
self.controller?.apply(theme: theme, strings: strings, animated: false)
self.chartView.apply(theme: theme, strings: strings, animated: false)
self.rangeView.apply(theme: theme, strings: strings, animated: false)
}
}
self.titleLabel.setTextColor(theme.chartTitleColor, animated: animated && titleLabel.isVisibleInWindow)
}
@objc private func didTapBackButton() {
self.controller?.didTapZoomOut()
}
func setBackButtonVisible(_ visible: Bool, animated: Bool) {
backButton.setVisible(visible, animated: animated)
layoutIfNeeded(animated: animated)
}
func updateToolViews(animated: Bool) {
guard let controller = self.controller else {
return
}
rangeView.setRange(controller.currentChartHorizontalRangeFraction, animated: animated)
rangeView.setRangePaging(enabled: controller.isChartRangePagingEnabled,
minimumSize: controller.minimumSelectedChartRange)
visibilityView.setVisible(controller.drawChartVisibity, animated: animated)
if controller.drawChartVisibity {
visibilityView.isExpanded = true
visibilityView.items = controller.actualChartsCollection.chartValues.map { value in
return ChartVisibilityItem(title: value.name, color: value.color)
}
visibilityView.setItemsSelection(controller.actualChartVisibility)
visibilityView.setNeedsLayout()
visibilityView.layoutIfNeeded()
} else {
visibilityView.isExpanded = false
}
superview?.superview?.layoutIfNeeded(animated: animated)
}
override func layoutSubviews() {
super.layoutSubviews()
let bounds = self.bounds
self.titleLabel.frame = CGRect(origin: CGPoint(x: backButton.alpha > 0.0 ? 36.0 : 0.0, y: 5.0), size: CGSize(width: bounds.width, height: 28.0))
self.sectionContainerView.frame = CGRect(origin: CGPoint(), size: CGSize(width: bounds.width, height: 750.0))
self.chartView.frame = CGRect(origin: CGPoint(), size: CGSize(width: bounds.width, height: 310.0))
self.rangeView.isHidden = !self.displayRange
self.rangeView.frame = CGRect(origin: CGPoint(x: 0.0, y: 310.0), size: CGSize(width: bounds.width, height: 42.0))
self.visibilityView.frame = CGRect(origin: CGPoint(x: 0.0, y: self.displayRange ? 368.0 : 326.0), size: CGSize(width: bounds.width, height: 350.0))
self.backButton.frame = CGRect(x: 8.0, y: 0.0, width: 96.0, height: 38.0)
self.chartView.setNeedsDisplay()
}
func setup(controller: BaseChartController, displayRange: Bool = true, zoomToEnding: Bool = true) {
self.controller = controller
self.displayRange = displayRange
if let theme = self.theme, let strings = self.strings {
controller.apply(theme: theme, strings: strings, animated: false)
}
self.chartView.renderers = controller.mainChartRenderers
self.chartView.userDidSelectCoordinateClosure = { [unowned self] point in
self.controller?.chartInteractionDidBegin(point: point)
}
self.chartView.userDidDeselectCoordinateClosure = { [unowned self] in
self.controller?.chartInteractionDidEnd()
}
controller.cartViewBounds = { [unowned self] in
return self.chartView.bounds
}
controller.chartFrame = { [unowned self] in
return self.chartView.chartFrame
}
controller.setDetailsViewModel = { [unowned self] viewModel, animated, feedback in
self.chartView.setDetailsViewModel(viewModel: viewModel, animated: animated)
if feedback {
self.hapticFeedback.tap()
}
}
controller.setDetailsChartVisibleClosure = { [unowned self] visible, animated in
self.chartView.setDetailsChartVisible(visible, animated: animated)
}
controller.setDetailsViewPositionClosure = { [unowned self] position in
self.chartView.detailsViewPosition = position
}
controller.setChartTitleClosure = { [unowned self] title, animated in
self.titleLabel.setText(title, animated: animated)
}
controller.setBackButtonVisibilityClosure = { [unowned self] visible, animated in
self.setNeedsLayout()
self.setBackButtonVisible(visible, animated: animated)
}
controller.refreshChartToolsClosure = { [unowned self] animated in
self.updateToolViews(animated: animated)
}
self.rangeView.chartView.renderers = controller.navigationRenderers
self.rangeView.rangeDidChangeClosure = { range in
controller.updateChartRange(range)
}
self.rangeView.touchedOutsideClosure = {
controller.cancelChartInteraction()
}
controller.chartRangeUpdatedClosure = { [unowned self] (range, animated) in
self.rangeView.setRange(range, animated: animated)
}
controller.chartRangePagingClosure = { [unowned self] (isEnabled, pageSize) in
self.rangeView.setRangePaging(enabled: isEnabled, minimumSize: pageSize)
}
self.visibilityView.selectionCallbackClosure = { [unowned self] visibility in
self.controller?.updateChartsVisibility(visibility: visibility, animated: true)
}
controller.initializeChart()
updateToolViews(animated: false)
let range: ClosedRange<CGFloat> = displayRange && zoomToEnding ? 0.8 ... 1.0 : 0.0 ... 1.0
rangeView.setRange(range, animated: false)
controller.updateChartRange(range, animated: false)
self.setNeedsLayout()
}
}