fix register close
This commit is contained in:
parent
01756710a3
commit
53b90dc646
@ -124,7 +124,7 @@ struct LoginView: View {
|
|||||||
}
|
}
|
||||||
.padding(.top, 10)
|
.padding(.top, 10)
|
||||||
.sheet(isPresented: $isShowingRegistration) {
|
.sheet(isPresented: $isShowingRegistration) {
|
||||||
RegistrationView(viewModel: viewModel)
|
RegistrationView(viewModel: viewModel, isPresented: $isShowingRegistration)
|
||||||
}
|
}
|
||||||
|
|
||||||
Spacer()
|
Spacer()
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import SwiftUI
|
|||||||
|
|
||||||
struct RegistrationView: View {
|
struct RegistrationView: View {
|
||||||
@ObservedObject var viewModel: LoginViewModel
|
@ObservedObject var viewModel: LoginViewModel
|
||||||
|
@Binding var isPresented: Bool
|
||||||
@Environment(\.presentationMode) private var presentationMode
|
@Environment(\.presentationMode) private var presentationMode
|
||||||
|
|
||||||
@State private var username: String = ""
|
@State private var username: String = ""
|
||||||
@ -40,149 +41,138 @@ struct RegistrationView: View {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationView {
|
NavigationView {
|
||||||
|
|
||||||
ScrollView {
|
ScrollView {
|
||||||
ZStack {
|
ZStack(alignment: .top) {
|
||||||
Color.clear
|
Color.clear
|
||||||
.contentShape(Rectangle())
|
.contentShape(Rectangle())
|
||||||
.onTapGesture {
|
.onTapGesture { hideKeyboard() }
|
||||||
hideKeyboard()
|
|
||||||
}
|
|
||||||
|
|
||||||
VStack(alignment: .leading, spacing: 16) {
|
VStack(alignment: .leading, spacing: 16) {
|
||||||
|
Group {
|
||||||
Group {
|
HStack {
|
||||||
|
TextField(NSLocalizedString("Логин", comment: "Логин"), text: $username)
|
||||||
HStack {
|
.autocapitalization(.none)
|
||||||
TextField(NSLocalizedString("Логин", comment: "Логин"), text: $username)
|
.disableAutocorrection(true)
|
||||||
.autocapitalization(.none)
|
Spacer()
|
||||||
.disableAutocorrection(true)
|
if !username.isEmpty {
|
||||||
Spacer()
|
Image(systemName: isUsernameValid ? "checkmark.circle" : "xmark.circle")
|
||||||
if !username.isEmpty {
|
.foregroundColor(isUsernameValid ? .green : .red)
|
||||||
Image(systemName: isUsernameValid ? "checkmark.circle" : "xmark.circle")
|
}
|
||||||
.foregroundColor(isUsernameValid ? .green : .red)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
.padding()
|
|
||||||
.background(Color(.secondarySystemBackground))
|
|
||||||
.cornerRadius(8)
|
|
||||||
.autocapitalization(.none)
|
|
||||||
.disableAutocorrection(true)
|
|
||||||
.onChange(of: username) { newValue in
|
|
||||||
if newValue.count > 32 {
|
|
||||||
username = String(newValue.prefix(32))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isUsernameValid && !username.isEmpty {
|
|
||||||
Text(NSLocalizedString("Логин должен быть от 3 до 32 символов (английские буквы, цифры, _)", comment: "Логин должен быть от 3 до 32 символов (английские буквы, цифры, _)"))
|
|
||||||
.foregroundColor(.red)
|
|
||||||
.font(.caption)
|
|
||||||
}
|
|
||||||
|
|
||||||
HStack {
|
|
||||||
SecureField(NSLocalizedString("Пароль", comment: "Пароль"), text: $password)
|
|
||||||
.autocapitalization(.none)
|
|
||||||
Spacer()
|
|
||||||
if !password.isEmpty {
|
|
||||||
Image(systemName: isPasswordValid ? "checkmark.circle" : "xmark.circle")
|
|
||||||
.foregroundColor(isPasswordValid ? .green : .red)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.padding()
|
|
||||||
.background(Color(.secondarySystemBackground))
|
|
||||||
.cornerRadius(8)
|
|
||||||
.autocapitalization(.none)
|
|
||||||
.onChange(of: password) { newValue in
|
|
||||||
if newValue.count > 32 {
|
|
||||||
password = String(newValue.prefix(32))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isPasswordValid && !password.isEmpty {
|
|
||||||
Text(NSLocalizedString("Пароль должен быть от 8 до 128 символов", comment: "Пароль должен быть от 6 до 32 символов"))
|
|
||||||
.foregroundColor(.red)
|
|
||||||
.font(.caption)
|
|
||||||
}
|
|
||||||
|
|
||||||
HStack {
|
|
||||||
SecureField(NSLocalizedString("Подтверждение пароля", comment: "Подтверждение пароля"), text: $confirmPassword)
|
|
||||||
.autocapitalization(.none)
|
|
||||||
Spacer()
|
|
||||||
if !confirmPassword.isEmpty {
|
|
||||||
Image(systemName: isConfirmPasswordValid ? "checkmark.circle" : "xmark.circle")
|
|
||||||
.foregroundColor(isConfirmPasswordValid ? .green : .red)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.padding()
|
|
||||||
.background(Color(.secondarySystemBackground))
|
|
||||||
.cornerRadius(8)
|
|
||||||
.autocapitalization(.none)
|
|
||||||
.onChange(of: confirmPassword) { newValue in
|
|
||||||
if newValue.count > 32 {
|
|
||||||
confirmPassword = String(newValue.prefix(32))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isConfirmPasswordValid && !confirmPassword.isEmpty {
|
|
||||||
Text(NSLocalizedString("Пароли не совпадают", comment: "Пароли не совпадают"))
|
|
||||||
.foregroundColor(.red)
|
|
||||||
.font(.caption)
|
|
||||||
}
|
|
||||||
|
|
||||||
TextField(NSLocalizedString("Инвайт-код (необязательно)", comment: "Инвайт-код"), text: $inviteCode)
|
|
||||||
.padding()
|
.padding()
|
||||||
.background(Color(.secondarySystemBackground))
|
.background(Color(.secondarySystemBackground))
|
||||||
.cornerRadius(8)
|
.cornerRadius(8)
|
||||||
.autocapitalization(.none)
|
.autocapitalization(.none)
|
||||||
.disableAutocorrection(true)
|
.disableAutocorrection(true)
|
||||||
}
|
.onChange(of: username) { newValue in
|
||||||
|
if newValue.count > 32 {
|
||||||
|
username = String(newValue.prefix(32))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Button(action: registerUser) {
|
if !isUsernameValid && !username.isEmpty {
|
||||||
if isLoading {
|
Text(NSLocalizedString("Логин должен быть от 3 до 32 символов (английские буквы, цифры, _)", comment: "Логин должен быть от 3 до 32 символов (английские буквы, цифры, _)"))
|
||||||
ProgressView()
|
.foregroundColor(.red)
|
||||||
|
.font(.caption)
|
||||||
|
}
|
||||||
|
|
||||||
|
HStack {
|
||||||
|
SecureField(NSLocalizedString("Пароль", comment: "Пароль"), text: $password)
|
||||||
|
.autocapitalization(.none)
|
||||||
|
Spacer()
|
||||||
|
if !password.isEmpty {
|
||||||
|
Image(systemName: isPasswordValid ? "checkmark.circle" : "xmark.circle")
|
||||||
|
.foregroundColor(isPasswordValid ? .green : .red)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.padding()
|
||||||
|
.background(Color(.secondarySystemBackground))
|
||||||
|
.cornerRadius(8)
|
||||||
|
.autocapitalization(.none)
|
||||||
|
.onChange(of: password) { newValue in
|
||||||
|
if newValue.count > 32 {
|
||||||
|
password = String(newValue.prefix(32))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isPasswordValid && !password.isEmpty {
|
||||||
|
Text(NSLocalizedString("Пароль должен быть от 8 до 128 символов", comment: "Пароль должен быть от 6 до 32 символов"))
|
||||||
|
.foregroundColor(.red)
|
||||||
|
.font(.caption)
|
||||||
|
}
|
||||||
|
|
||||||
|
HStack {
|
||||||
|
SecureField(NSLocalizedString("Подтверждение пароля", comment: "Подтверждение пароля"), text: $confirmPassword)
|
||||||
|
.autocapitalization(.none)
|
||||||
|
Spacer()
|
||||||
|
if !confirmPassword.isEmpty {
|
||||||
|
Image(systemName: isConfirmPasswordValid ? "checkmark.circle" : "xmark.circle")
|
||||||
|
.foregroundColor(isConfirmPasswordValid ? .green : .red)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.padding()
|
||||||
|
.background(Color(.secondarySystemBackground))
|
||||||
|
.cornerRadius(8)
|
||||||
|
.autocapitalization(.none)
|
||||||
|
.onChange(of: confirmPassword) { newValue in
|
||||||
|
if newValue.count > 32 {
|
||||||
|
confirmPassword = String(newValue.prefix(32))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isConfirmPasswordValid && !confirmPassword.isEmpty {
|
||||||
|
Text(NSLocalizedString("Пароли не совпадают", comment: "Пароли не совпадают"))
|
||||||
|
.foregroundColor(.red)
|
||||||
|
.font(.caption)
|
||||||
|
}
|
||||||
|
|
||||||
|
TextField(NSLocalizedString("Инвайт-код (необязательно)", comment: "Инвайт-код"), text: $inviteCode)
|
||||||
.padding()
|
.padding()
|
||||||
.frame(maxWidth: .infinity)
|
.background(Color(.secondarySystemBackground))
|
||||||
.background(Color.gray.opacity(0.6))
|
|
||||||
.cornerRadius(8)
|
|
||||||
} else {
|
|
||||||
Text(NSLocalizedString("Зарегистрироваться", comment: "Зарегистрироваться"))
|
|
||||||
.foregroundColor(.white)
|
|
||||||
.padding()
|
|
||||||
.frame(maxWidth: .infinity)
|
|
||||||
.background(isFormValid ? Color.blue : Color.gray.opacity(0.6))
|
|
||||||
.cornerRadius(8)
|
.cornerRadius(8)
|
||||||
|
.autocapitalization(.none)
|
||||||
|
.disableAutocorrection(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Button(action: registerUser) {
|
||||||
|
if isLoading {
|
||||||
|
ProgressView()
|
||||||
|
.padding()
|
||||||
|
.frame(maxWidth: .infinity)
|
||||||
|
.background(Color.gray.opacity(0.6))
|
||||||
|
.cornerRadius(8)
|
||||||
|
} else {
|
||||||
|
Text(NSLocalizedString("Зарегистрироваться", comment: "Зарегистрироваться"))
|
||||||
|
.foregroundColor(.white)
|
||||||
|
.padding()
|
||||||
|
.frame(maxWidth: .infinity)
|
||||||
|
.background(isFormValid ? Color.blue : Color.gray.opacity(0.6))
|
||||||
|
.cornerRadius(8)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.disabled(!isFormValid)
|
||||||
|
.padding(.bottom)
|
||||||
}
|
}
|
||||||
.disabled(!isFormValid)
|
.padding()
|
||||||
.padding(.bottom)
|
|
||||||
}
|
|
||||||
.padding()
|
|
||||||
}
|
|
||||||
.navigationBarItems(trailing:
|
|
||||||
Button(action: {
|
|
||||||
presentationMode.wrappedValue.dismiss()
|
|
||||||
}) {
|
|
||||||
Text(NSLocalizedString("Закрыть", comment: "Закрыть"))
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.navigationTitle(Text(NSLocalizedString("Регистрация", comment: "Регистрация")))
|
|
||||||
.alert(isPresented: $showError) {
|
|
||||||
Alert(
|
|
||||||
title: Text(NSLocalizedString("Ошибка регистрация", comment: "Ошибка")),
|
|
||||||
message: Text(errorMessage),
|
|
||||||
dismissButton: .default(Text(NSLocalizedString("OK", comment: "")))
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onTapGesture {
|
.navigationTitle(Text(NSLocalizedString("Регистрация", comment: "Регистрация")))
|
||||||
hideKeyboard()
|
.toolbar {
|
||||||
|
ToolbarItem(placement: .navigationBarTrailing) {
|
||||||
|
Button(action: dismissSheet) {
|
||||||
|
Text(NSLocalizedString("Закрыть", comment: "Закрыть"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.alert(isPresented: $showError) {
|
||||||
|
Alert(
|
||||||
|
title: Text(NSLocalizedString("Ошибка регистрация", comment: "Ошибка")),
|
||||||
|
message: Text(errorMessage),
|
||||||
|
dismissButton: .default(Text(NSLocalizedString("OK", comment: "")))
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
.onTapGesture {
|
|
||||||
hideKeyboard()
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private func registerUser() {
|
private func registerUser() {
|
||||||
isLoading = true
|
isLoading = true
|
||||||
@ -190,7 +180,7 @@ struct RegistrationView: View {
|
|||||||
viewModel.registerUser(username: username, password: password, invite: inviteCode.isEmpty ? nil : inviteCode) { success, message in
|
viewModel.registerUser(username: username, password: password, invite: inviteCode.isEmpty ? nil : inviteCode) { success, message in
|
||||||
isLoading = false
|
isLoading = false
|
||||||
if success {
|
if success {
|
||||||
presentationMode.wrappedValue.dismiss()
|
dismissSheet()
|
||||||
} else {
|
} else {
|
||||||
errorMessage = message ?? NSLocalizedString("Неизвестная ошибка.", comment: "")
|
errorMessage = message ?? NSLocalizedString("Неизвестная ошибка.", comment: "")
|
||||||
showError = true
|
showError = true
|
||||||
@ -198,6 +188,12 @@ struct RegistrationView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func dismissSheet() {
|
||||||
|
hideKeyboard()
|
||||||
|
isPresented = false
|
||||||
|
presentationMode.wrappedValue.dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
private func hideKeyboard() {
|
private func hideKeyboard() {
|
||||||
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
||||||
}
|
}
|
||||||
@ -208,6 +204,6 @@ struct RegistrationView_Previews: PreviewProvider {
|
|||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
let viewModel = LoginViewModel()
|
let viewModel = LoginViewModel()
|
||||||
viewModel.isLoading = false // чтобы убрать спиннер
|
viewModel.isLoading = false // чтобы убрать спиннер
|
||||||
return RegistrationView(viewModel: viewModel)
|
return RegistrationView(viewModel: viewModel, isPresented: .constant(true))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user