metatext/Views/SwiftUI/RegistrationView.swift

80 lines
3 KiB
Swift
Raw Normal View History

2020-09-11 09:55:06 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import SwiftUI
import ViewModels
struct RegistrationView: View {
@StateObject var viewModel: RegistrationViewModel
2020-09-12 02:50:42 +00:00
@State private var presentURL: URL?
2020-09-11 09:55:06 +00:00
var body: some View {
Form {
2020-09-12 02:50:42 +00:00
HStack {
TextField("registration.username", text: $viewModel.registration.username)
2020-09-12 07:36:59 +00:00
.textContentType(.username)
2020-09-11 09:55:06 +00:00
.autocapitalization(.none)
.disableAutocorrection(true)
2020-09-12 07:36:59 +00:00
Text("@".appending(viewModel.instance.uri))
2020-09-12 02:50:42 +00:00
.foregroundColor(.secondary)
}
TextField("registration.email", text: $viewModel.registration.email)
2020-09-12 07:36:59 +00:00
.textContentType(.emailAddress)
2020-09-12 02:50:42 +00:00
.autocapitalization(.none)
.disableAutocorrection(true)
.keyboardType(.emailAddress)
SecureField("registration.password", text: $viewModel.registration.password)
2020-09-12 07:36:59 +00:00
.textContentType(.password)
2020-09-12 02:50:42 +00: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 09:55:06 +00:00
}
}
2020-09-12 07:36:59 +00:00
Button("registration.server-rules") { presentURL = viewModel.serverRulesURL }
Button("registration.terms-of-service") { presentURL = viewModel.termsOfServiceURL }
2020-09-12 02:50:42 +00: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 09:55:06 +00:00
}
}
2020-09-12 02:50:42 +00:00
.frame(maxWidth: .infinity, alignment: .center)
2020-09-11 09:55:06 +00:00
}
.alertItem($viewModel.alertItem)
2021-01-27 20:31:32 +00:00
.sheet(item: $presentURL) { SafariView(url: $0).edgesIgnoringSafeArea(.all) }
2020-09-11 09:55:06 +00: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