-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathCreateUsernameViewController.swift
More file actions
257 lines (231 loc) · 9.92 KB
/
CreateUsernameViewController.swift
File metadata and controls
257 lines (231 loc) · 9.92 KB
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
256
257
//
// Created by Andrei Ashikhmin
// Copyright © 2024 Dash Core Group. All rights reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import UIKit
import SwiftUI
class CreateUsernameViewController: UIViewController {
private let dashPayModel: DWDashPayProtocol
@objc var completionHandler: ((Bool) -> ())?
@objc
init(dashPayModel: DWDashPayProtocol, invitationURL: URL?, definedUsername: String?) {
// TODO: invites
self.dashPayModel = dashPayModel
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.dw_secondaryBackground()
let content = CreateUsernameView(dashPayModel: dashPayModel) { dialog in
self.showModalDialog(dialog: dialog)
} dismissDialog: {
self.dismiss(animated: true)
} finish: {
self.navigationController?.popViewController(animated: true)
self.completionHandler?(true)
}
let swiftUIController = UIHostingController(rootView: content)
swiftUIController.view.backgroundColor = UIColor.dw_secondaryBackground()
self.dw_embedChild(swiftUIController)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.applyOpaqueAppearance(with: UIColor.dw_secondaryBackground(), shadowColor: .clear)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
struct CreateUsernameView: View {
@State var dashPayModel: DWDashPayProtocol
@StateObject private var viewModel = CreateUsernameViewModel()
@FocusState private var isTextInputFocused: Bool
@State private var showVotingInfo: Bool = false
@State private var showVerifyIdentity: Bool = false
@State private var confirmUsernameRequest: Bool = false
@State private var inProgress: Bool = false
@State private var prove: URL? = nil
var showVerifyConfirmation: (any View) -> Void
var dismissDialog: () -> Void
var finish: () -> Void
var body: some View {
VStack(alignment: .leading, spacing: 0) {
Text(NSLocalizedString("Create your username", comment: "Usernames"))
.foregroundColor(.primaryText)
.font(.title1)
.padding(.top, 12)
Text(NSLocalizedString("Please note that you will not be able to change it in future", comment: "Usernames"))
.foregroundColor(.primaryText)
.font(.system(size: 14))
TextInput(label: "Username", text: $viewModel.username)
.padding(.top, 20)
.focused($isTextInputFocused)
if viewModel.uiState.lengthRule != .hidden {
ValidationCheck(
validationResult: viewModel.uiState.lengthRule,
text: NSLocalizedString("Between 3 and 23 characters", comment: "Usernames")
).padding(.top, 20)
}
if viewModel.uiState.allowedCharactersRule != .hidden {
ValidationCheck(
validationResult: viewModel.uiState.allowedCharactersRule,
text: NSLocalizedString("Letter, numbers and hyphens only", comment: "Usernames")
).padding(.top, 20)
}
if viewModel.uiState.costRule != .hidden {
ValidationCheck(
validationResult: viewModel.uiState.costRule,
text: String.localizedStringWithFormat(NSLocalizedString("You need to have more %@ Dash to create this username", comment: "Usernames"), viewModel.uiState.requiredDash.dashAmount.formattedDashAmountWithoutCurrencySymbol)
).padding(.top, 20)
}
if viewModel.uiState.usernameBlockedRule != .hidden {
ValidationCheck(
validationResult: viewModel.uiState.usernameBlockedRule,
text: getMessageForBlockedRule()
).padding(.top, 20)
}
if viewModel.uiState.usernameBlockedRule == .warning {
DashButton(
text: NSLocalizedString("What is username voting?", comment: "Usernames"),
leadingIcon: .system("plus"),
style: .plain,
size: .small,
stretch: false
) {
showVotingInfo = true
}
.overrideForegroundColor(.dashBlue)
.padding(.leading, 12)
.padding(.top, 4)
}
Spacer()
DashButton(
text: NSLocalizedString("Continue", comment: ""),
isEnabled: viewModel.uiState.canContinue,
isLoading: inProgress
) {
if viewModel.uiState.usernameBlockedRule == .valid {
Task {
inProgress = true
let result = await viewModel.submitUsernameRequest(withProve: nil)
inProgress = false
if result {
finish()
}
}
} else {
showVerifyConfirmation(getVerifyConfirmation())
}
}
}
.padding(.horizontal, 20)
.padding(.bottom, 20)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
.onAppear {
isTextInputFocused = true
}
.sheet(isPresented: $showVotingInfo) {
let dialog = BottomSheet(showBackButton: Binding<Bool>.constant(false)) {
VotingInfoScreen {
showVotingInfo = false
}
}
if #available(iOS 16.0, *) {
dialog.presentationDetents([.height(600)])
} else {
dialog
}
}
.sheet(isPresented: $showVerifyIdentity, onDismiss: {
guard let url = self.prove else { return }
if viewModel.currentUsernameRequest == nil {
confirmUsernameRequest = true
} else {
viewModel.updateRequest(with: url)
}
}) {
let dialog = BottomSheet(showBackButton: Binding<Bool>.constant(false)) {
VerifyIdentityScreen(
viewModel: viewModel,
onConfirmed: { url in
self.prove = url
showVerifyIdentity = false
}
)
}
if #available(iOS 16.0, *) {
dialog.presentationDetents([.height(650)])
} else {
dialog
}
}
.sheet(isPresented: $confirmUsernameRequest) {
let dialog = ConfirmSpendDialog(username: viewModel.username, amount: Int64(viewModel.uiState.requiredDash)) {
confirmUsernameRequest = false
} onConfirm: {
confirmUsernameRequest = false
Task {
inProgress = true
let result = await viewModel.submitUsernameRequest(withProve: self.prove)
inProgress = false
if result {
finish()
}
}
}
if #available(iOS 16.0, *) {
dialog.presentationDetents([.height(340)])
} else {
dialog
}
}
}
@ViewBuilder
private func getVerifyConfirmation() -> some View {
ModalDialog(
heading: NSLocalizedString("Verify your identity to enhance your chances of getting your requested username", comment: "Usernames"),
textBlock1: NSLocalizedString("If somebody else requests the same username as you, we will let the network decide whom to give this username", comment: "Usernames"),
positiveButtonText: NSLocalizedString("Verify", comment: ""),
positiveButtonAction: {
dismissDialog()
self.prove = nil
showVerifyIdentity = true
},
negativeButtonText: NSLocalizedString("Skip", comment: ""),
negativeButtonAction: {
dismissDialog()
confirmUsernameRequest = true
},
buttonsOrientation: .horizontal
)
}
private func getMessageForBlockedRule() -> String {
switch viewModel.uiState.usernameBlockedRule {
case .invalid:
return NSLocalizedString("This username is blocked by the Dash Network", comment: "Usernames")
case .invalidCritical:
return NSLocalizedString("This username is already created by someone else", comment: "Usernames")
case .warning:
return NSLocalizedString("The Dash network will vote on this username. We will notify you of the results on March 14, 2024.", comment: "Usernames") // TODO: date
case .loading:
return ""
default:
return NSLocalizedString("Username is available", comment: "Usernames")
}
}
}