-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
SettingsAccountViewController.swift
214 lines (168 loc) · 6.48 KB
/
SettingsAccountViewController.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
import KsApi
import Library
import Prelude
import ReactiveSwift
import SwiftUI
import UIKit
final class SettingsAccountViewController: UIViewController, MessageBannerViewControllerPresenting {
@IBOutlet private var tableView: UITableView!
private let dataSource = SettingsAccountDataSource()
internal var messageBannerViewController: MessageBannerViewController?
fileprivate let viewModel: SettingsAccountViewModelType = SettingsAccountViewModel(
SettingsAccountViewController.viewController(for:currency:)
)
internal static func instantiate() -> SettingsAccountViewController {
return Storyboard.Settings.instantiate(SettingsAccountViewController.self)
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self.dataSource
self.tableView.delegate = self
self.tableView.estimatedSectionFooterHeight = SettingsGroupedFooterView.defaultHeight
self.messageBannerViewController = self.configureMessageBannerViewController(on: self)
self.tableView.register(nib: .SettingsTableViewCell)
self.tableView.register(nib: .SettingsAccountWarningCell)
self.tableView.registerHeaderFooter(nib: .SettingsHeaderView)
self.tableView.registerHeaderFooterClass(SettingsGroupedFooterView.self)
self.viewModel.inputs.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.viewModel.inputs.viewWillAppear()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.tableView.ksr_sizeHeaderFooterViewsToFit()
}
override func bindViewModel() {
self.viewModel.outputs.reloadData
.observeForUI()
.observeValues { [weak self] currency, email,
shouldHideEmailWarning, shouldHideEmailPasswordSection, isAppleConnectedAccount in
self?.dataSource.configureRows(
currency: currency,
shouldHideEmailWarning: shouldHideEmailWarning,
shouldHideEmailPasswordSection: shouldHideEmailPasswordSection,
isAppleConnected: isAppleConnectedAccount
)
if let email = email, isAppleConnectedAccount {
self?.showAppleHeader(with: email)
}
self?.tableView.reloadData()
}
self.viewModel.outputs.fetchAccountFieldsError
.observeForUI()
.observeValues { [weak self] in
self?.dataSource.configureRows(
currency: nil,
shouldHideEmailWarning: true,
shouldHideEmailPasswordSection: false,
isAppleConnected: false
)
self?.tableView.reloadData()
self?.showGeneralError()
}
self.viewModel.outputs.transitionToViewController
.observeForControllerAction()
.observeValues { [weak self] viewController in
self?.navigationController?.pushViewController(viewController, animated: true)
}
}
override func bindStyles() {
super.bindStyles()
_ = self
|> settingsViewControllerStyle
|> UIViewController.lens.title %~ { _ in Strings.Account() }
_ = self.tableView
|> settingsTableViewStyle
|> settingsTableViewSeparatorStyle
}
// MARK: - Functions
private func showGeneralError() {
self.messageBannerViewController?.showBanner(
with: .error,
message: Strings.Something_went_wrong_please_try_again()
)
}
private func showAppleHeader(with appleId: String) {
let container = UIView(frame: .zero)
self.tableView.tableHeaderView = container
let header = SettingsAccountHeaderView(frame: .zero)
header.configure(with: appleId)
_ = (header, container)
|> ksr_addSubviewToParent()
|> ksr_constrainViewToEdgesInParent()
_ = header.widthAnchor.constraint(equalTo: self.tableView.widthAnchor)
|> \.priority .~ .defaultHigh
|> \.isActive .~ true
}
}
extension SettingsAccountViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
guard let cellType = dataSource.cellTypeForIndexPath(indexPath: indexPath) else {
return
}
self.viewModel.inputs.didSelectRow(cellType: cellType)
}
func tableView(_: UITableView, heightForHeaderInSection _: Int) -> CGFloat {
return SettingsSectionType.sectionHeaderHeight
}
func tableView(_: UITableView, heightForFooterInSection section: Int) -> CGFloat {
guard let data = self.viewModel.outputs.shouldShowCreatePasswordFooter() else {
return 0.1
}
let (shouldShow, _) = data
guard section == SettingsAccountSectionType.createPassword.rawValue, shouldShow else {
return 0.1
}
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, viewForHeaderInSection _: Int) -> UIView? {
return tableView.dequeueReusableHeaderFooterView(withIdentifier: Nib.SettingsHeaderView.rawValue)
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
guard let data = self.viewModel.outputs.shouldShowCreatePasswordFooter() else {
return nil
}
let (shouldShowCreatePasswordFooter, anEmail) = data
guard let email = anEmail, shouldShowCreatePasswordFooter,
section == SettingsAccountSectionType.createPassword.rawValue else {
return nil
}
let footerView = tableView.dequeueReusableHeaderFooterView(
withClass: SettingsGroupedFooterView.self
) as? SettingsGroupedFooterView
let text = Strings.Youre_connected_via_Facebook_email_Create_a_password_for_this_account(email: email)
footerView?.label.text = text
return footerView
}
}
extension SettingsAccountViewController {
static func viewController(for cellType: SettingsAccountCellType, currency: Currency) -> UIViewController? {
switch cellType {
case .createPassword:
return CreatePasswordViewController.instantiate()
case .changeEmail:
/**
if #available(iOS 15, *) {
let changeEmailView = ChangeEmailView()
return UIHostingController(rootView: changeEmailView)
} else {
return ChangeEmailViewController.instantiate()
}
*/
return ChangeEmailViewController.instantiate()
case .changePassword:
return ChangePasswordViewController.instantiate()
case .paymentMethods:
return PaymentMethodsViewController.instantiate()
case .privacy:
return SettingsPrivacyViewController.instantiate()
case .currency:
let vc = SelectCurrencyViewController.instantiate()
vc.configure(with: currency)
return vc
}
}
}