Skip to content

Commit

Permalink
[Add] 인증번호 서버 연결(#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReeseSR committed May 4, 2022
1 parent 08d42b6 commit d617462
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,163 @@ import UIKit

class ResetCodeViewController: UIViewController {

// MARK: - Properties
var email: String? // 이메일

// MARK: - @IBOutlet Properties

@IBOutlet weak var codeTextFieldView: UIView!
@IBOutlet weak var codeTextField: UITextField!
@IBOutlet weak var warningLabel: UILabel!
@IBOutlet weak var completeButton: UIButton!

// MARK: - View Life Cycle
override func viewDidLoad() {
print("이거 실행되긴 하니?")
super.viewDidLoad()

initNavigationBar()
setDelegation()
configureUI()

// Do any additional setup after loading the view.
}

// MARK: - Functions
private func addTarget() {
codeTextField.addTarget(self, action: #selector(self.activateCodeTextField), for: .editingDidBegin)
codeTextField.addTarget(self, action: #selector(self.inactivateCodeTextField), for: .editingDidEnd)
codeTextField.addTarget(self, action: #selector(self.activateCompleteButton), for: .editingChanged)
}

// 텍스트 필드 확성화
@objc private func activateCodeTextField() {
codeTextFieldView.makeRoundedWithBorder(radius: 12, color: UIColor.systemGray2.cgColor)
}

// 텍스트 필드 비활성화
@objc private func inactivateCodeTextField() {
codeTextFieldView.makeRoundedWithBorder(radius: 12, color: UIColor.systemGray6.cgColor)
}

// 버튼 활성화
@objc private func activateCompleteButton() {
completeButton.isEnabled = codeTextField.hasText
completeButton.alpha = 1.0
}

private func initNavigationBar() {
self.navigationController?.initWithBackButton()
}

private func setDelegation() {
self.codeTextField.delegate = self
}

private func configureUI() {
codeTextFieldView.makeRoundedWithBorder(radius: 12, color: UIColor.systemGray6.cgColor)
completeButton.makeRounded(radius: 12)
makeCompleteButtonDisable()
}

private func pushToNewPasswordViewController() {
self.navigationController?.pushViewController(NewPasswordViewController.loadFromNib(), animated: true)
}

private func makeCompleteButtonEnable() {
completeButton.isEnabled = true
completeButton.alpha = 1.0
}

/*
// MARK: - Navigation
private func makeCompleteButtonDisable() {
completeButton.isEnabled = false
completeButton.alpha = 0.3
}

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
func validateCode(code: String) -> Bool {
// Email 정규식
let codeRegEx = "^[a-z0-9+]{6}$"
let codeTest = NSPredicate(format: "SELF MATCHES %@", codeRegEx)
return codeTest.evaluate(with: code)
}
*/

private func checkCodeFormat(userInput: String) {
if validateCode(code: userInput) {
makeCompleteButtonEnable()
} else {
makeCompleteButtonDisable()
}
}

@IBAction func touchCompleteButton(_ sender: Any) {
print("touched Complete Button")
sendCode()
}

private func pushToNewPasswordViewController(email: String) {
let newPasswordViewController = NewPasswordViewController.loadFromNib()
self.navigationController?.pushViewController(newPasswordViewController, animated: true)

}

}

// MARK: - UITextFieldDelegate
extension ResetCodeViewController: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
print("textFieldDidendEditing")
guard let text = textField.text else {
return
}
checkCodeFormat(userInput: text)
}

func textFieldDidBeginEditing(_ textField: UITextField) {
print("저 실행되었습니다.")
warningLabel.isHidden = true
codeTextFieldView.makeRoundedWithBorder(radius: 12, color: UIColor.systemGray2.cgColor)
completeButton.isEnabled = true
}

// Return 눌렀을 시 키보드 내리기
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}

extension ResetCodeViewController {
func sendCode() {
print("저 실행됩니다요")
guard let code = codeTextField.text else {
return
}

PasswordAPI.shared.checkCode(completion: { response in
switch response {
case .success(let data):
if let data = data as? String {
self.showToast(message: data)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.dismiss(animated: true)
}
self.pushToNewPasswordViewController()
case .requestErr(let message):
if let message = message as? String {
print(message)
self.showToast(message: message)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.dismiss(animated: true)
}
print("requestErr", message)
case .pathErr:
print("pathErr")
case .serverErr:
print("serverErr")
case .networkFail:
print("networkFail")
}
}, email: email ?? "", authNum: code)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="19529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17126"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19519"/>
<capability name="Named colors" minToolsVersion="9.0"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
Expand All @@ -11,7 +12,11 @@
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="ResetCodeViewController" customModule="Hyangyu" customModuleProvider="target">
<connections>
<outlet property="codeTextField" destination="D2j-kN-mPg" id="lSR-fp-5p2"/>
<outlet property="codeTextFieldView" destination="b9a-KS-zg3" id="AMg-wt-AEO"/>
<outlet property="completeButton" destination="LcM-3r-zR8" id="kwh-Yx-Qoi"/>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
<outlet property="warningLabel" destination="arn-Xz-wwJ" id="0Re-aC-pI6"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
Expand All @@ -28,21 +33,21 @@
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="aRQ-q9-sOY">
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="b9a-KS-zg3">
<rect key="frame" x="0.0" y="22" width="374" height="54"/>
<subviews>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="인증번호를 입력해주세요" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="jGb-xJ-s4V">
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="인증번호를 입력하세요" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="D2j-kN-mPg">
<rect key="frame" x="15" y="15" width="344" height="24"/>
<fontDescription key="fontDescription" name="AppleSDGothicNeo-Regular" family="Apple SD Gothic Neo" pointSize="16"/>
<textInputTraits key="textInputTraits" textContentType="email"/>
</textField>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="jGb-xJ-s4V" firstAttribute="top" secondItem="aRQ-q9-sOY" secondAttribute="top" constant="15" id="LXH-ps-GdF"/>
<constraint firstAttribute="trailing" secondItem="jGb-xJ-s4V" secondAttribute="trailing" constant="15" id="Q3s-eJ-04T"/>
<constraint firstItem="jGb-xJ-s4V" firstAttribute="leading" secondItem="aRQ-q9-sOY" secondAttribute="leading" constant="15" id="bm1-oh-pPc"/>
<constraint firstAttribute="bottom" secondItem="jGb-xJ-s4V" secondAttribute="bottom" constant="15" id="xF0-4h-Yl0"/>
<constraint firstItem="D2j-kN-mPg" firstAttribute="top" secondItem="b9a-KS-zg3" secondAttribute="top" constant="15" id="8nk-Mj-dGV"/>
<constraint firstItem="D2j-kN-mPg" firstAttribute="leading" secondItem="b9a-KS-zg3" secondAttribute="leading" constant="15" id="FtJ-Fh-LoN"/>
<constraint firstAttribute="trailing" secondItem="D2j-kN-mPg" secondAttribute="trailing" constant="15" id="NvO-DN-q92"/>
<constraint firstAttribute="bottom" secondItem="D2j-kN-mPg" secondAttribute="bottom" constant="15" id="Vc7-jK-KhW"/>
</constraints>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="인증번호가 맞지 않습니다" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="arn-Xz-wwJ">
Expand All @@ -69,6 +74,9 @@
<state key="normal" title="인증번호 확인">
<color key="titleColor" name="lightYellow"/>
</state>
<connections>
<action selector="touchCompleteButton:" destination="-1" eventType="touchUpInside" id="RcF-4p-5zg"/>
</connections>
</button>
</subviews>
<viewLayoutGuide key="safeArea" id="fnl-2z-Ty3"/>
Expand Down

0 comments on commit d617462

Please sign in to comment.