forked from dashpay/dashwallet-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateUsernameViewController.swift
More file actions
358 lines (322 loc) · 14.7 KB
/
CreateUsernameViewController.swift
File metadata and controls
358 lines (322 loc) · 14.7 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//
// 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
private let invitationURL: URL?
private let assetLockTx: DSTransaction?
@objc var completionHandler: ((Bool) -> ())?
@objc
init(dashPayModel: DWDashPayProtocol, invitationURL: URL?, assetLockTx: DSTransaction?, definedUsername: String?) {
self.dashPayModel = dashPayModel
self.invitationURL = invitationURL
self.assetLockTx = assetLockTx
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(assetLockTx: self.assetLockTx, 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 {
let assetLockTx: DSTransaction?
@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 showContestedInfo: 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(.h5Bold)
.padding(.top, 12)
Text(NSLocalizedString("Please note that you will not be able to change it in future", comment: "Usernames"))
.foregroundColor(.primaryText)
.font(.body2)
TextInput(label: "Username", text: $viewModel.username)
.padding(.top, 20)
.focused($isTextInputFocused)
if !viewModel.uiState.contestedAllowed {
HStack(spacing: 0) {
Text(NSLocalizedString("The username must meet ", comment: "Usernames"))
Text(NSLocalizedString("one", comment: "Usernames"))
.fontWeight(.bold)
Text(NSLocalizedString(" of these criteria", comment: "Usernames"))
}
.foregroundColor(.primaryText)
.font(.body2)
.padding(.top, 12)
} else {
Text(NSLocalizedString("The username must meet these criteria", comment: "Usernames"))
.foregroundColor(.primaryText)
.font(.body2)
.padding(.top, 12)
}
if viewModel.uiState.lengthRule != .hidden {
ValidationCheck(
validationResult: viewModel.uiState.lengthRule,
text: !viewModel.uiState.contestedAllowed ?
NSLocalizedString("Between 20 and 23 characters", comment: "Usernames") :
NSLocalizedString("Between 3 and 23 characters", comment: "Usernames")
).padding(.top, 20)
}
if viewModel.uiState.allowedCharactersRule != .hidden {
ValidationCheck(
validationResult: viewModel.uiState.allowedCharactersRule,
text: !viewModel.uiState.contestedAllowed ?
NSLocalizedString("Contains numbers 2-9", comment: "Usernames") :
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()
if viewModel.uiState.hasInvite && !viewModel.uiState.isInvitationMixed {
HStack(alignment: .top) {
Image("invite.unmixed")
.resizable()
.scaledToFit()
.frame(width: 18, height: 18)
Text(NSLocalizedString("The invitation was created with un-mixed funds", comment: "Invites"))
.foregroundColor(.primaryText)
.font(.body2)
}
.padding(.bottom, 16)
}
if !viewModel.uiState.contestedAllowed {
HStack(alignment: .top) {
Image("invite.noncontested")
.resizable()
.scaledToFit()
.frame(width: 18, height: 18)
Text(viewModel.uiState.hasInvite ? NSLocalizedString("You can only create a non-contested username using this invitation", comment: "Invites") :
NSLocalizedString("You can only create a non-contested username at this time", comment: "Invites"))
.foregroundColor(.primaryText)
.font(.body2)
Spacer()
Button {
showContestedInfo = true
} label: {
Image(systemName: "info.circle.fill")
.resizable()
.scaledToFit()
.padding(8)
.foregroundColor(.gray300)
}
.frame(width: 30, height: 30)
}
.frame(minHeight: 36)
.padding(.bottom, 20)
}
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
if let tx = assetLockTx {
viewModel.checkAssetLockTx(tx)
}
}
.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(
amount: Int64(viewModel.uiState.requiredDash),
title: NSLocalizedString("Confirm", comment: ""),
onCancel: {
confirmUsernameRequest = false
},
onConfirm: {
confirmUsernameRequest = false
Task {
inProgress = true
let result = await viewModel.submitUsernameRequest(withProve: self.prove)
inProgress = false
if result {
finish()
}
}
},
username: viewModel.username,
detailsText: NSLocalizedString("Please note that the username can NOT be changed once it is registered.", comment: "Usernames"),
requiresAcceptance: true
)
if #available(iOS 16.0, *) {
dialog.presentationDetents([.height(340)])
} else {
dialog
}
}
.sheet(isPresented: $showContestedInfo) {
let dialog = BottomSheet(showBackButton: Binding<Bool>.constant(false)) {
TextIntro(
inProgress: .constant(false)
) {
FeatureTopText(
title: NSLocalizedString("What are contested and non-contested usernames?", comment: "Usernames"),
text: NSLocalizedString("There are two types of usernames: contested and non-contested.\n\nNon-contested usernames have at least one number (2-9) or are longer than 20 characters and will be automatically approved.\n\nIf you want to create a contested username which is shorter and without numbers, you need to register with DashPay without an invitation, pay the required fee and wait for approval upon completion of the voting period.", comment: "Usernames"),
alignment: .leading
)
}
}
if #available(iOS 16.0, *) {
dialog.presentationDetents([.height(400)])
} 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")
}
}
}