metatext-app-ios-iphone-ipad/Views/RegistrationView.swift

80 lines
3.0 KiB
Swift
Raw Normal View History

2020-09-11 11:55:06 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import SwiftUI
import ViewModels
struct RegistrationView: View {
@StateObject var viewModel: RegistrationViewModel
2020-09-12 04:50:42 +02:00
@State private var presentURL: URL?
2020-09-11 11:55:06 +02:00
var body: some View {
Form {
2020-09-12 04:50:42 +02:00
HStack {
TextField("registration.username", text: $viewModel.registration.username)
2020-09-12 09:36:59 +02:00
.textContentType(.username)
2020-09-11 11:55:06 +02:00
.autocapitalization(.none)
.disableAutocorrection(true)
2020-09-12 09:36:59 +02:00
Text("@".appending(viewModel.instance.uri))
2020-09-12 04:50:42 +02:00
.foregroundColor(.secondary)
}
TextField("registration.email", text: $viewModel.registration.email)
2020-09-12 09:36:59 +02:00
.textContentType(.emailAddress)
2020-09-12 04:50:42 +02:00
.autocapitalization(.none)
.disableAutocorrection(true)
.keyboardType(.emailAddress)
SecureField("registration.password", text: $viewModel.registration.password)
2020-09-12 09:36:59 +02:00
.textContentType(.password)
2020-09-12 04:50:42 +02:00
SecureField("registration.password-confirmation", text: $viewModel.passwordConfirmation)
if viewModel.instance.approvalRequired {
VStack(alignment: .leading) {
Text("registration.reason-\(viewModel.instance.uri)")
TextEditor(text: $viewModel.registration.reason)
2020-09-11 11:55:06 +02:00
}
}
2020-09-12 09:36:59 +02:00
Button("registration.server-rules") { presentURL = viewModel.serverRulesURL }
Button("registration.terms-of-service") { presentURL = viewModel.termsOfServiceURL }
2020-09-12 04:50:42 +02:00
Toggle("registration.agree-to-server-rules-and-terms-of-service",
isOn: $viewModel.registration.agreement)
Group {
if viewModel.registering {
ProgressView()
} else {
Button(viewModel.instance.approvalRequired
? "add-identity.request-invite"
: "add-identity.join",
action: viewModel.registerTapped)
.disabled(viewModel.registerDisabled)
2020-09-11 11:55:06 +02:00
}
}
2020-09-12 04:50:42 +02:00
.frame(maxWidth: .infinity, alignment: .center)
2020-09-11 11:55:06 +02:00
}
.alertItem($viewModel.alertItem)
2020-09-12 04:50:42 +02:00
.sheet(item: $presentURL) { SafariView(url: $0) }
2020-09-11 11:55:06 +02:00
}
}
extension RegistrationError: LocalizedError {
public var errorDescription: String? {
switch self {
case .passwordConfirmationMismatch:
return NSLocalizedString(
"registration.password-confirmation-mismatch",
comment: "")
}
}
}
#if DEBUG
import PreviewViewModels
struct RegistrationView_Previews: PreviewProvider {
static var previews: some View {
RegistrationView(viewModel: RootViewModel.preview
.addIdentityViewModel()
.registrationViewModel(instance: .preview,
url: .previewInstanceURL))
}
}
#endif