Skip to content

Commit

Permalink
feat: 현재 비밀번호 텍스트필드 구현 #16
Browse files Browse the repository at this point in the history
  • Loading branch information
ubeeni committed Sep 4, 2024
1 parent 7694ca1 commit fe9a9e7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 11 deletions.
5 changes: 5 additions & 0 deletions DOTCHI/DOTCHI/Global/Bases/BaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class BaseViewController: UIViewController, UIGestureRecognizerDelegate {
return data
}

/// 키체인에서 저장된 비밀번호를 가져오는 메서드
func getPasswordFromKeychain() -> String? {
return keychainManager.get(KeychainKeys.password.rawValue)
}

/// 신고 사유 선택 action sheet
// func reportActionSheet(userId: Int) -> UIAlertController {
// let reportActionSheet: UIAlertController = UIAlertController(
Expand Down
105 changes: 94 additions & 11 deletions DOTCHI/DOTCHI/Sources/Screens/My/ChangePasswordViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ class ChangePasswordViewController: BaseViewController, UITextFieldDelegate {
private let newPasswordLabel = UILabel()
private let newPasswordTextField = UITextField()
private let newPasswordWarningLabel = UILabel()
private let newPasswordInfoImageView = UIImageView()
private let confirmPasswordTextField = UITextField()
private let confirmPasswordWarningLabel = UILabel()
private let newPasswordInfoImageView = UIImageView()
private let confirmPasswordInfoImageView = UIImageView()
private let currentPasswordLabel = UILabel()
private let currentPasswordTextField = UITextField()
private let currentPasswordWarningLabel = UILabel()
private let currentPasswordInfoImageView = UIImageView()

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -30,6 +33,7 @@ class ChangePasswordViewController: BaseViewController, UITextFieldDelegate {

newPasswordTextField.delegate = self
confirmPasswordTextField.delegate = self
currentPasswordTextField.delegate = self
}

override func viewWillAppear(_ animated: Bool) {
Expand All @@ -45,7 +49,9 @@ class ChangePasswordViewController: BaseViewController, UITextFieldDelegate {
newPasswordWarningLabel,
confirmPasswordTextField,
confirmPasswordWarningLabel,
currentPasswordLabel
currentPasswordLabel,
currentPasswordTextField,
currentPasswordWarningLabel
])

navigationView.centerTitleLabel.text = "비밀번호 변경"
Expand Down Expand Up @@ -108,11 +114,6 @@ class ChangePasswordViewController: BaseViewController, UITextFieldDelegate {
confirmPasswordTextField.attributedPlaceholder = NSAttributedString(string: "새 비밀번호 확인(최소 10자 이상)", attributes: attributes)
confirmPasswordTextField.addTarget(self, action: #selector(passwordTextFieldDidChange(_:)), for: .editingChanged)

confirmPasswordWarningLabel.text = "비밀번호가 일치하지 않습니다"
confirmPasswordWarningLabel.textColor = .dotchiOrange
confirmPasswordWarningLabel.font = .sSub
confirmPasswordWarningLabel.isHidden = true

let confirmPasswordContainerView = UIView()
confirmPasswordContainerView.addSubview(confirmPasswordInfoImageView)

Expand All @@ -130,9 +131,48 @@ class ChangePasswordViewController: BaseViewController, UITextFieldDelegate {
confirmPasswordTextField.rightView = confirmPasswordContainerView
confirmPasswordTextField.rightViewMode = .always

confirmPasswordWarningLabel.text = "비밀번호가 일치하지 않습니다"
confirmPasswordWarningLabel.textColor = .dotchiOrange
confirmPasswordWarningLabel.font = .sSub
confirmPasswordWarningLabel.isHidden = true

currentPasswordLabel.text = "현재 비밀번호"
currentPasswordLabel.textColor = .dotchiWhite
currentPasswordLabel.font = .sub

currentPasswordTextField.isSecureTextEntry = true
currentPasswordTextField.textColor = .dotchiWhite
currentPasswordTextField.font = .sub
currentPasswordTextField.backgroundColor = .dotchiMgray
currentPasswordTextField.tintColor = UITextField().tintColor
currentPasswordTextField.layer.cornerRadius = 8
currentPasswordTextField.layer.borderWidth = 1
currentPasswordTextField.setLeftPadding(12)

currentPasswordTextField.attributedPlaceholder = NSAttributedString(string: "현재 비밀번호", attributes: attributes)
currentPasswordTextField.addTarget(self, action: #selector(passwordTextFieldDidChange(_:)), for: .editingChanged)

let currentPasswordContainerView = UIView()
currentPasswordContainerView.addSubview(currentPasswordInfoImageView)

currentPasswordContainerView.snp.makeConstraints { make in
make.height.equalTo(48)
make.width.equalTo(39)
}

currentPasswordInfoImageView.snp.makeConstraints { make in
make.height.width.equalTo(20)
make.centerY.equalToSuperview()
make.trailing.equalToSuperview().offset(-10)
}

currentPasswordTextField.rightView = currentPasswordContainerView
currentPasswordTextField.rightViewMode = .always

currentPasswordWarningLabel.text = "비밀번호가 일치하지 않습니다"
currentPasswordWarningLabel.textColor = .dotchiOrange
currentPasswordWarningLabel.font = .sSub
currentPasswordWarningLabel.isHidden = true
}

// MARK: - Button Actions
Expand All @@ -143,6 +183,8 @@ class ChangePasswordViewController: BaseViewController, UITextFieldDelegate {
handleConfirmPasswordTextFieldChange(confirmPasswordTextField.text ?? "")
} else if textField == confirmPasswordTextField {
handleConfirmPasswordTextFieldChange(confirmPasswordTextField.text ?? "")
} else if textField == currentPasswordTextField {
handleCurrentPasswordTextFieldChange(currentPasswordTextField.text ?? "")
}
}

Expand Down Expand Up @@ -185,6 +227,35 @@ class ChangePasswordViewController: BaseViewController, UITextFieldDelegate {
updateWarningLabelConstraints(isWarningVisible: isWarningVisible, forConfirmPassword: true)
}

private func handleCurrentPasswordTextFieldChange(_ newText: String) {
currentPasswordTextField.layer.borderColor = verifyCurrentPassword() ? UIColor.clear.cgColor : UIColor.dotchiOrange.cgColor
currentPasswordWarningLabel.isHidden = verifyCurrentPassword()

if verifyCurrentPassword() {
currentPasswordInfoImageView.image = UIImage(systemName: "checkmark.circle")
currentPasswordInfoImageView.tintColor = .dotchiGreen
} else {
currentPasswordInfoImageView.image = UIImage(systemName: "info.circle")
currentPasswordInfoImageView.tintColor = .dotchiOrange
}

currentPasswordInfoImageView.isHidden = false
}

private func verifyCurrentPassword() -> Bool {
guard let savedPassword = getPasswordFromKeychain() else {
currentPasswordWarningLabel.text = "비밀번호를 확인할 수 없습니다"
currentPasswordWarningLabel.isHidden = false
return false
}

let isValid = savedPassword == currentPasswordTextField.text
currentPasswordWarningLabel.isHidden = isValid
currentPasswordWarningLabel.text = isValid ? "" : "비밀번호가 일치하지 않습니다"

return isValid
}

private func updateWarningLabelConstraints(isWarningVisible: Bool, forConfirmPassword: Bool = false) {
UIView.animate(withDuration: 0.3) {
if forConfirmPassword {
Expand All @@ -198,20 +269,21 @@ class ChangePasswordViewController: BaseViewController, UITextFieldDelegate {
self.confirmPasswordWarningLabel.snp.remakeConstraints { make in
make.top.equalTo(self.confirmPasswordTextField.snp.bottom).offset(8)
make.leading.trailing.equalToSuperview().inset(28)
make.height.equalTo(isWarningVisible ? 20 : 0)
}

self.confirmPasswordWarningLabel.isHidden = !isWarningVisible
} else {
self.newPasswordTextField.snp.remakeConstraints { make in
let offset = isWarningVisible ? 16 : 10
make.top.equalTo(self.newPasswordLabel.snp.bottom).offset(16)
make.leading.trailing.equalToSuperview().inset(28)
make.height.equalTo(48)
}

self.newPasswordWarningLabel.snp.remakeConstraints { make in
make.top.equalTo(self.newPasswordTextField.snp.bottom).offset(8)
make.top.equalTo(self.newPasswordTextField.snp.bottom).offset(isWarningVisible ? 8 : 0)
make.leading.trailing.equalToSuperview().inset(28)
make.height.equalTo(isWarningVisible ? 20 : 0)
}

self.confirmPasswordTextField.snp.remakeConstraints { make in
Expand Down Expand Up @@ -250,7 +322,7 @@ class ChangePasswordViewController: BaseViewController, UITextFieldDelegate {
}

newPasswordWarningLabel.snp.makeConstraints { make in
make.top.equalTo(newPasswordTextField.snp.bottom).offset(-2)
make.top.equalTo(newPasswordTextField.snp.bottom).offset(0)
make.leading.trailing.equalToSuperview().inset(28)
make.height.equalTo(0)
}
Expand All @@ -267,7 +339,18 @@ class ChangePasswordViewController: BaseViewController, UITextFieldDelegate {
}

currentPasswordLabel.snp.makeConstraints { make in
make.top.equalTo(confirmPasswordWarningLabel).offset(33)
make.top.equalTo(confirmPasswordWarningLabel.snp.bottom).offset(33)
make.leading.trailing.equalToSuperview().inset(28)
}

currentPasswordTextField.snp.makeConstraints { make in
make.top.equalTo(currentPasswordLabel.snp.bottom).offset(16)
make.leading.trailing.equalToSuperview().inset(28)
make.height.equalTo(48)
}

currentPasswordWarningLabel.snp.makeConstraints { make in
make.top.equalTo(currentPasswordTextField.snp.bottom).offset(8)
make.leading.trailing.equalToSuperview().inset(28)
}
}
Expand Down

0 comments on commit fe9a9e7

Please sign in to comment.