Newer
Older
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
import UIKit
import Popup
import Models
import Shared
import Combine
import DependencyInjection
final class BackupConfigController: UIViewController {
@Dependency private var coordinator: BackupCoordinating
lazy private var screenView = BackupConfigView()
private let viewModel: BackupConfigViewModel
private var cancellables = Set<AnyCancellable>()
private var popupCancellables = Set<AnyCancellable>()
private var wifiOnly = false
private var manualBackups = false
private var serviceName: String = ""
override func loadView() {
view = screenView
}
init(_ viewModel: BackupConfigViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) { nil }
override func viewDidLoad() {
super.viewDidLoad()
setupBindings()
}
private func setupBindings() {
viewModel.actionState()
.receive(on: DispatchQueue.main)
.sink { [unowned self] in screenView.actionView.setState($0) }
.store(in: &cancellables)
viewModel.connectedServices()
.receive(on: DispatchQueue.main)
.sink { [unowned self] in decorate(connectedServices: $0) }
.store(in: &cancellables)
viewModel.enabledService()
.receive(on: DispatchQueue.main)
.sink { [unowned self] in decorate(enabledService: $0) }
.store(in: &cancellables)
viewModel.automatic()
.receive(on: DispatchQueue.main)
.sink { [unowned self] in
screenView.frequencyDetailView.subtitleLabel.text = $0 ? "Automatic" : "Manual"
manualBackups = !$0
}.store(in: &cancellables)
viewModel.wifiOnly()
.receive(on: DispatchQueue.main)
.sink { [unowned self] in
screenView.infrastructureDetailView.subtitleLabel.text = $0 ? "Wi-Fi Only" : "Wi-Fi and Cellular"
wifiOnly = $0
}.store(in: &cancellables)
viewModel.lastBackup()
.receive(on: DispatchQueue.main)
.sink { [unowned self] in
guard let backup = $0 else {
screenView.latestBackupDetailView.subtitleLabel.text = "Never"
return
}
screenView.latestBackupDetailView.subtitleLabel.text = backup.date.backupStyle()
}.store(in: &cancellables)
screenView.actionView.backupNowButton
.publisher(for: .touchUpInside)
.sink { [unowned self] in viewModel.didTapBackupNow() }
.store(in: &cancellables)
screenView.frequencyDetailView
.publisher(for: .touchUpInside)
.sink { [unowned self] in presentFrequencyPopup(manual: manualBackups) }
.store(in: &cancellables)
screenView.infrastructureDetailView
.publisher(for: .touchUpInside)
.sink { [unowned self] in presentInfrastructurePopup(wifiOnly: wifiOnly) }
.store(in: &cancellables)
screenView.googleDriveButton
.publisher(for: .touchUpInside)
.sink { [unowned self] in viewModel.didTapService(.drive, self) }
.store(in: &cancellables)
screenView.googleDriveButton.switcherView
.publisher(for: .valueChanged)
.sink { [unowned self] in viewModel.didToggleService(self, .drive, screenView.googleDriveButton.switcherView.isOn) }
.store(in: &cancellables)
screenView.dropboxButton.switcherView
.publisher(for: .valueChanged)
.sink { [unowned self] in viewModel.didToggleService(self, .dropbox, screenView.dropboxButton.switcherView.isOn) }
.store(in: &cancellables)
screenView.iCloudButton.switcherView
.publisher(for: .valueChanged)
.sink { [unowned self] in viewModel.didToggleService(self, .icloud, screenView.iCloudButton.switcherView.isOn) }
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
.store(in: &cancellables)
screenView.dropboxButton
.publisher(for: .touchUpInside)
.sink { [unowned self] in viewModel.didTapService(.dropbox, self) }
.store(in: &cancellables)
screenView.iCloudButton
.publisher(for: .touchUpInside)
.sink { [unowned self] in viewModel.didTapService(.icloud, self) }
.store(in: &cancellables)
}
private func decorate(enabledService: CloudService?) {
var button: BackupSwitcherButton?
switch enabledService {
case .none:
break
case .icloud:
serviceName = "iCloud"
button = screenView.iCloudButton
case .dropbox:
serviceName = "Dropbox"
button = screenView.dropboxButton
case .drive:
serviceName = "Google Drive"
button = screenView.googleDriveButton
}
screenView.enabledSubtitleLabel.text
= Localized.Backup.Config.disclaimer(serviceName)
screenView.frequencyDetailView.titleLabel.text
= Localized.Backup.Config.frequency(serviceName).uppercased()
guard let button = button else {
screenView.iCloudButton.isHidden = false
screenView.dropboxButton.isHidden = false
screenView.googleDriveButton.isHidden = false
screenView.iCloudButton.switcherView.isOn = false
screenView.dropboxButton.switcherView.isOn = false
screenView.googleDriveButton.switcherView.isOn = false
screenView.frequencyDetailView.isHidden = true
screenView.enabledSubtitleView.isHidden = true
screenView.latestBackupDetailView.isHidden = true
screenView.infrastructureDetailView.isHidden = true
return
}
screenView.frequencyDetailView.isHidden = false
screenView.enabledSubtitleView.isHidden = false
screenView.latestBackupDetailView.isHidden = false
screenView.infrastructureDetailView.isHidden = false
[screenView.iCloudButton, screenView.dropboxButton, screenView.googleDriveButton]
.forEach {
$0.isHidden = $0 != button
$0.switcherView.isOn = $0 == button
}
}
private func decorate(connectedServices: Set<CloudService>) {
if connectedServices.contains(.icloud) {
screenView.iCloudButton.showSwitcher(enabled: false)
} else {
screenView.iCloudButton.showChevron()
}
if connectedServices.contains(.dropbox) {
screenView.dropboxButton.showSwitcher(enabled: false)
} else {
screenView.dropboxButton.showChevron()
}
if connectedServices.contains(.drive) {
screenView.googleDriveButton.showSwitcher(enabled: false)
} else {
screenView.googleDriveButton.showChevron()
}
}
private func presentInfrastructurePopup(wifiOnly: Bool) {
let cancelButton = CapsuleButton()
cancelButton.setStyle(.seeThrough)
cancelButton.setTitle(Localized.ChatList.Dashboard.cancel, for: .normal)
let wifiOnlyButton = PopupRadioButton(title: "Wi-Fi Only", isSelected: wifiOnly)
let wifiAndCellularButton = PopupRadioButton(title: "Wi-Fi and Cellular", isSelected: !wifiOnly)
let popup = BottomPopup(with: [
PopupLabel(
font: Fonts.Mulish.extraBold.font(size: 28.0),
text: Localized.Backup.Config.infrastructure,
color: Asset.neutralActive.color,
alignment: .left,
spacingAfter: 30
),
wifiOnlyButton,
wifiAndCellularButton,
PopupEmptyView(height: 20.0),
PopupStackView(spacing: 20.0, views: [cancelButton])
])
wifiOnlyButton.action
.sink { [unowned self] in
viewModel.didChooseWifiOnly(true)
popup.dismiss(animated: true) { [weak self] in
self?.popupCancellables.removeAll()
}
}.store(in: &popupCancellables)
wifiAndCellularButton.action
.sink { [unowned self] in
viewModel.didChooseWifiOnly(false)
popup.dismiss(animated: true) { [weak self] in
self?.popupCancellables.removeAll()
}
}.store(in: &popupCancellables)
cancelButton.publisher(for: .touchUpInside)
.receive(on: DispatchQueue.main)
.sink {
popup.dismiss(animated: true) { [weak self] in
self?.popupCancellables.removeAll()
}
}.store(in: &popupCancellables)
coordinator.toPopup(popup, from: self)
}
private func presentFrequencyPopup(manual: Bool) {
let cancelButton = CapsuleButton()
cancelButton.setStyle(.seeThrough)
cancelButton.setTitle(Localized.ChatList.Dashboard.cancel, for: .normal)
let manualButton = PopupRadioButton(title: "Manual", isSelected: manual)
let automaticButton = PopupRadioButton(title: "Automatic", isSelected: !manual)
let popup = BottomPopup(with: [
PopupLabel(
font: Fonts.Mulish.extraBold.font(size: 28.0),
text: Localized.Backup.Config.frequency(serviceName),
color: Asset.neutralActive.color,
alignment: .left,
spacingAfter: 30
),
manualButton,
automaticButton,
PopupEmptyView(height: 20.0),
PopupStackView(spacing: 20.0, views: [cancelButton])
])
manualButton.action
.sink { [unowned self] in
viewModel.didChooseAutomatic(false)
popup.dismiss(animated: true) { [weak self] in
self?.popupCancellables.removeAll()
}
}.store(in: &popupCancellables)
automaticButton.action
.sink { [unowned self] in
viewModel.didChooseAutomatic(true)
popup.dismiss(animated: true) { [weak self] in
self?.popupCancellables.removeAll()
}
}.store(in: &popupCancellables)
cancelButton.publisher(for: .touchUpInside)
.receive(on: DispatchQueue.main)
.sink {
popup.dismiss(animated: true) { [weak self] in
self?.popupCancellables.removeAll()
}
}.store(in: &popupCancellables)
coordinator.toPopup(popup, from: self)
}
}