154 lines
6.7 KiB
Swift
154 lines
6.7 KiB
Swift
import SwiftUI
|
||
|
||
|
||
|
||
struct ChangePasswordView: View {
|
||
@State private var oldPassword = ""
|
||
@State private var newPassword = ""
|
||
@State private var confirmPassword = ""
|
||
@State private var isOldPasswordVisible = false
|
||
@State private var isNewPasswordVisible = false
|
||
@State private var isConfirmPasswordVisible = false
|
||
|
||
private var isOldPasswordValid: Bool {
|
||
return oldPassword.count >= 8 && oldPassword.count <= 128
|
||
}
|
||
|
||
private var isOldPasswordSame: Bool {
|
||
return oldPassword == newPassword
|
||
}
|
||
|
||
private var isNewPasswordValid: Bool {
|
||
return newPassword.count >= 8 && newPassword.count <= 128
|
||
}
|
||
|
||
private var isPasswordConfirmValid: Bool {
|
||
return newPassword == confirmPassword
|
||
}
|
||
|
||
var body: some View {
|
||
Form {
|
||
Section {
|
||
|
||
HStack {
|
||
if isOldPasswordVisible {
|
||
TextField(NSLocalizedString("Старый пароль", comment: "Старый пароль"), text: $oldPassword)
|
||
.autocapitalization(.none)
|
||
.disableAutocorrection(true)
|
||
.textContentType(.password)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
} else {
|
||
SecureField(NSLocalizedString("Старый пароль", comment: "Старый пароль"), text: $oldPassword)
|
||
.autocapitalization(.none)
|
||
.disableAutocorrection(true)
|
||
.textContentType(.password)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
}
|
||
|
||
Button(action: {
|
||
isOldPasswordVisible.toggle()
|
||
}) {
|
||
Image(systemName: isOldPasswordVisible ? "eye.slash" : "eye")
|
||
.foregroundColor(.gray)
|
||
}
|
||
.buttonStyle(PlainButtonStyle())
|
||
.padding(.horizontal, 4)
|
||
|
||
if !oldPassword.isEmpty {
|
||
Image(systemName: isOldPasswordValid ? "checkmark.circle" : "xmark.circle")
|
||
.foregroundColor(isOldPasswordValid ? .green : .red)
|
||
}
|
||
}
|
||
|
||
HStack {
|
||
if isNewPasswordVisible {
|
||
TextField(NSLocalizedString("Новый пароль", comment: "Новый пароль"), text: $newPassword)
|
||
.autocapitalization(.none)
|
||
.disableAutocorrection(true)
|
||
.textContentType(.newPassword)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
} else {
|
||
SecureField(NSLocalizedString("Новый пароль", comment: "Новый пароль"), text: $newPassword)
|
||
.autocapitalization(.none)
|
||
.disableAutocorrection(true)
|
||
.textContentType(.newPassword)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
}
|
||
|
||
Button(action: {
|
||
isNewPasswordVisible.toggle()
|
||
}) {
|
||
Image(systemName: isNewPasswordVisible ? "eye.slash" : "eye")
|
||
.foregroundColor(.gray)
|
||
}
|
||
.buttonStyle(PlainButtonStyle())
|
||
.padding(.horizontal, 4)
|
||
|
||
if !newPassword.isEmpty {
|
||
let isAllValid = isNewPasswordValid && !isOldPasswordSame
|
||
|
||
Image(systemName: isAllValid ? "checkmark.circle" : "xmark.circle")
|
||
.foregroundColor(isAllValid ? .green : .red)
|
||
}
|
||
}
|
||
|
||
HStack {
|
||
if isConfirmPasswordVisible {
|
||
TextField(NSLocalizedString("Подтверждение пароля", comment: "Подтверждение пароля"), text: $confirmPassword)
|
||
.autocapitalization(.none)
|
||
.disableAutocorrection(true)
|
||
.textContentType(.password)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
} else {
|
||
SecureField(NSLocalizedString("Подтверждение пароля", comment: "Подтверждение пароля"), text: $confirmPassword)
|
||
.autocapitalization(.none)
|
||
.disableAutocorrection(true)
|
||
.textContentType(.password)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
}
|
||
|
||
Button(action: {
|
||
isConfirmPasswordVisible.toggle()
|
||
}) {
|
||
Image(systemName: isConfirmPasswordVisible ? "eye.slash" : "eye")
|
||
.foregroundColor(.gray)
|
||
}
|
||
.buttonStyle(PlainButtonStyle())
|
||
.padding(.horizontal, 4)
|
||
|
||
if !confirmPassword.isEmpty {
|
||
Image(systemName: isPasswordConfirmValid ? "checkmark.circle" : "xmark.circle")
|
||
.foregroundColor(isPasswordConfirmValid ? .green : .red)
|
||
}
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
var isButtonEnabled: Bool {
|
||
isPasswordConfirmValid && !isOldPasswordSame && isNewPasswordValid && isOldPasswordValid
|
||
}
|
||
|
||
Button(action: {
|
||
// Действие для сохранения профиля
|
||
print("oldPassword: \(oldPassword)")
|
||
print("newPassword: \(newPassword)")
|
||
print("confirmPassword: \(confirmPassword)")
|
||
}) {
|
||
Text(NSLocalizedString("Применить", comment: ""))
|
||
.foregroundColor(.white)
|
||
.padding()
|
||
.frame(maxWidth: .infinity)
|
||
.background(isButtonEnabled ? Color.blue : Color.gray)
|
||
.cornerRadius(8)
|
||
}
|
||
.disabled(!isButtonEnabled)
|
||
.buttonStyle(PlainButtonStyle())
|
||
.listRowInsets(EdgeInsets())
|
||
.listRowBackground(Color.clear)
|
||
}
|
||
.navigationTitle(NSLocalizedString("Изменение пароля", comment: ""))
|
||
}
|
||
}
|