-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathNSStatisticsViewController.swift
164 lines (127 loc) · 6.12 KB
/
NSStatisticsViewController.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
/*
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/
import UIKit
class NSStatisticsViewController: NSTitleViewScrollViewController {
private let loadingView: NSLoadingView = {
let button = NSUnderlinedButton()
button.title = "loading_view_reload".ub_localized
return .init(reloadButton: button, errorImage: UIImage(named: "ic-info-outline"), small: true)
}()
private let appUsageStatisticsModule = NSAppUsageStatisticsModuleView()
private let covidCodesStatisticsModule = NSCovidCodesStatisticsModuleView()
private let covidStatisticsModule = NSCovidStatisticsModuleView()
private let shareModule = NSStatisticsShareModule()
private let loader = StatisticsLoader()
override init() {
super.init()
titleView = NSStatisticsHeaderView()
title = "bottom_nav_tab_stats".ub_localized
navigationItem.title = "app_name".ub_localized
tabBarItem.image = UIImage(named: "ic-stats")
tabBarItem.title = "bottom_nav_tab_stats".ub_localized
view.backgroundColor = .ns_backgroundSecondary
}
override func viewDidLoad() {
super.viewDidLoad()
setupLayout()
covidCodesStatisticsModule.infoButtonCallback = { [weak self] in
guard let strongSelf = self else { return }
let popup = NSStatisticInfoPopupViewController(type: .covidcodes)
popup.modalPresentationStyle = .overFullScreen
strongSelf.present(popup, animated: true)
}
covidStatisticsModule.infoButtonCallback = { [weak self] in
guard let strongSelf = self else { return }
let popup = NSStatisticInfoPopupViewController(type: .cases)
popup.modalPresentationStyle = .overFullScreen
strongSelf.present(popup, animated: true)
}
shareModule.shareButtonTouched = { [weak self] in
self?.share()
}
covidCodesStatisticsModule.setData(statisticData: nil)
covidStatisticsModule.setData(statisticData: nil)
appUsageStatisticsModule.setData(statisticData: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadData()
if UIAccessibility.isVoiceOverRunning {
stackScrollView.scrollView.setContentOffset(.zero, animated: false)
}
}
private func loadData() {
appUsageStatisticsModule.startLoading()
loader.get { [weak self] result in
guard let self = self else { return }
switch result {
case let .success(response):
self.appUsageStatisticsModule.stopLoading()
UIView.animate(withDuration: 0.3, delay: 0, options: [.beginFromCurrentState], animations: {
self.appUsageStatisticsModule.setData(statisticData: response)
self.covidCodesStatisticsModule.setData(statisticData: response)
self.covidStatisticsModule.setData(statisticData: response)
}, completion: { _ in
self.covidStatisticsModule.setData(statisticData: response)
})
case let .failure(error):
self.appUsageStatisticsModule.stopLoading(error: error) { [weak self] in
self?.loadData()
}
self.covidCodesStatisticsModule.setData(statisticData: nil)
self.covidStatisticsModule.setData(statisticData: nil)
}
}
}
private func share() {
let items: [Any] = ["share_app_message".ub_localized, URL(string: "share_app_url".ub_localized)!]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
present(ac, animated: true)
}
private func setupLayout() {
// navigation bar
let image = UIImage(named: "ic-info-outline")
navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, landscapeImagePhone: image, style: .plain, target: self, action: #selector(infoButtonPressed))
navigationItem.rightBarButtonItem?.tintColor = .ns_blue
navigationItem.rightBarButtonItem?.accessibilityLabel = "accessibility_info_button_app".ub_localized
stackScrollView.addArrangedView(appUsageStatisticsModule)
stackScrollView.addSpacerView(NSPadding.medium)
stackScrollView.addArrangedView(covidCodesStatisticsModule)
stackScrollView.addSpacerView(NSPadding.medium)
stackScrollView.addArrangedView(covidStatisticsModule)
stackScrollView.addSpacerView(NSPadding.medium)
let button = NSExternalLinkButton(style: .normal(color: .ns_blue), size: .small)
button.title = "stats_more_statistics_button".ub_localized
button.touchUpCallback = { [weak self] in
guard let self = self else { return }
self.moreStatisticsTouched()
}
let wrapper = UIView()
wrapper.addSubview(button)
button.snp.makeConstraints { make in
make.top.bottom.equalToSuperview()
make.leading.equalToSuperview().inset(NSPadding.medium)
make.trailing.lessThanOrEqualToSuperview().inset(NSPadding.medium)
}
stackScrollView.addArrangedView(wrapper)
stackScrollView.addSpacerView(NSPadding.large)
stackScrollView.addSpacerView(1.0, color: UIColor.setColorsForTheme(lightColor: .ns_dividerColor, darkColor: UIColor(ub_hexString: "#1e1e23")!))
stackScrollView.addSpacerView(NSPadding.large)
stackScrollView.addArrangedView(shareModule)
}
private func moreStatisticsTouched() {
if let url = URL(string: "stats_more_statistics_url".ub_localized) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
@objc private func infoButtonPressed() {
present(NSNavigationController(rootViewController: NSAboutViewController()), animated: true)
}
}