Skip to content

Commit

Permalink
#40 Refactoring : 코드 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
doyeonk429 committed Aug 12, 2024
1 parent f702a6f commit 1503cb7
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 101 deletions.
11 changes: 6 additions & 5 deletions Drink-EG/Drink-EG/Resources/APIs/LoginAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ extension LoginAPI: TargetType {

// API 호출 시, header에 token 넣어서 전달
var headers: [String : String]? {
let jwtToken = "jwt_token_here"
return [
"Authorization": "Bearer \(jwtToken)",
"Content-type": "application/json"
]
switch self {
case .postLogin, .postRegister:
return [
"Content-type": "application/json"
]
}
}

var validationType: ValidationType {
Expand Down
186 changes: 90 additions & 96 deletions Drink-EG/Drink-EG/Sources/VCs/Login/JoinViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
//
// Created by 이현주 on 7/30/24.
//

import UIKit
import SnapKit
import Moya

class JoinViewController: UIViewController, UITextFieldDelegate {
class JoinViewController: UIViewController {
// MARK: - Properties
let provider = MoyaProvider<LoginAPI>()
public var userID : String?
public var userPW : String?
var joinDTO : JoinNLoginRequest?

public var userID: String?
public var userPW: String?
var joinDTO: JoinNLoginRequest?
let joinButton = UIButton(type: .system)
let loginButton = UIButton(type: .system)

Expand All @@ -25,46 +25,66 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
let pwTextField = UITextField()
let pwAgainTextField = UITextField()

private let alreadyLabel: UILabel = {
let alreadyLabel: UILabel = {
let l = UILabel()
l.text = "이미 계정이 있으신가요?"
l.textColor = UIColor(hue: 0, saturation: 0, brightness: 0.71, alpha: 1.0)
l.font = UIFont.boldSystemFont(ofSize: 14)

return l
}()

private let idLabel: UILabel = {
let idLabel: UILabel = {
let id = UILabel()
id.text = "아이디"
id.textColor = UIColor.white
id.font = UIFont.boldSystemFont(ofSize: 15)

return id
}()

private let pwLabel: UILabel = {
let pwLabel: UILabel = {
let pw = UILabel()
pw.text = "비밀번호"
pw.textColor = UIColor.white
pw.font = UIFont.boldSystemFont(ofSize: 15)

return pw
}()

private let pwAgainLabel: UILabel = {
let pwAgainLabel: UILabel = {
let pwA = UILabel()
pwA.text = "비밀번호 재입력"
pwA.textColor = UIColor.white
pwA.font = UIFont.boldSystemFont(ofSize: 15)

return pwA
}()

// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()

self.navigationController?.isNavigationBarHidden = false
self.setupNavigationBar()
self.setupUI()
}

// MARK: - Button Actions
@objc private func joinButtonTapped() {
assignUserData()
callJoinAPI { [weak self] isSuccess in
if isSuccess {
self?.goToLoginView()
} else {
print("회원가입 실패")
// 실패 시에 대한 추가 처리
}
}
}

@objc private func loginButtonTapped() {
goToLoginView()
}

// MARK: - Private Methods
private func setupNavigationBar() {
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named:"icon_back")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named:"icon_back")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
Expand All @@ -75,7 +95,7 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
titleLabel.font = UIFont.boldSystemFont(ofSize: 20)
titleLabel.textColor = .white
titleLabel.textAlignment = .center

let titleView = UIView()
titleView.addSubview(titleLabel)
titleLabel.snp.makeConstraints { make in
Expand All @@ -84,25 +104,27 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
}

self.navigationItem.titleView = titleView

view.backgroundColor = .black

idTextField.delegate = self
pwTextField.delegate = self
pwAgainTextField.delegate = self

setupUI()
}

private func setupUI() {
private func goToLoginView() {
let loginViewController = LoginViewController()
navigationController?.pushViewController(loginViewController, animated: true)
}
}

extension JoinViewController {
func setupUI() {
configureJoinButton()
configureLoginButton()
configureKakaoButton()
configureAppleButton()
configureIdTextField()
configurePwTextField()
configurePwAgainTextField()

setupLayout()
}

private func setupLayout() {
let loginStackView = UIStackView(arrangedSubviews: [alreadyLabel, loginButton])
loginStackView.axis = .horizontal
loginStackView.distribution = .fillProportionally
Expand All @@ -120,7 +142,7 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
buttonStackView.axis = .horizontal
buttonStackView.distribution = .fillEqually
buttonStackView.spacing = 13

view.addSubview(buttonStackView)
buttonStackView.snp.makeConstraints { make in
make.top.equalTo(view).offset(594)
Expand Down Expand Up @@ -171,7 +193,7 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
make.width.equalTo(327)
make.height.equalTo(60)
}

view.addSubview(joinButton)
joinButton.snp.makeConstraints { make in
make.top.equalTo(view).offset(484)
Expand All @@ -193,19 +215,6 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
joinButton.layer.borderWidth = 0
joinButton.addTarget(self, action: #selector(joinButtonTapped), for: .touchUpInside)
}

@objc private func joinButtonTapped() {
assignUserData()
callJoinAPI { [weak self] isSuccess in
if isSuccess {
self?.goToLoginView()
} else {
print("회원가입 실패")
// 실패 시에 대한 처리 (예: 에러 메시지 표시)
}
}
}

private func configureLoginButton() {
loginButton.setTitle("로그인", for: .normal)
loginButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
Expand All @@ -215,27 +224,14 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
loginButton.backgroundColor = .clear
loginButton.addTarget(self, action: #selector(loginButtonTapped), for: .touchUpInside)
}

@objc private func loginButtonTapped() {
goToLoginView()
}

private func goToLoginView() {
let loginViewController = LoginViewController()
navigationController?.pushViewController(loginViewController, animated: true)
}

private func configureKakaoButton() {
//카카오 이미지를 아이콘 포멧 이미지에 맞게 바꿈
kakaoButton.setImage(UIImage(named: "kakao")?.withRenderingMode(.alwaysOriginal), for: .normal)

kakaoButton.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0.26, alpha: 0.5)
kakaoButton.layer.cornerRadius = 16
kakaoButton.layer.borderWidth = 2
kakaoButton.layer.borderColor = UIColor.white.cgColor.copy(alpha: 0.1)

}

private func configureAppleButton() {
appleButton.setImage(UIImage(named: "apple")?.withRenderingMode(.alwaysOriginal), for: .normal)
appleButton.tintColor = .white
Expand All @@ -245,7 +241,6 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
appleButton.layer.borderWidth = 2
appleButton.layer.borderColor = UIColor.white.cgColor.copy(alpha: 0.1)
}

private func configureIdTextField() {
idTextField.tag = 1
idTextField.attributedPlaceholder = NSAttributedString(string: "아이디를 입력해 주세요", attributes: [NSAttributedString.Key.foregroundColor: UIColor(hex: "#999999")!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14, weight: .regular)])
Expand All @@ -259,7 +254,6 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
idTextField.returnKeyType = .done
idTextField.clearButtonMode = .always
}

private func configurePwTextField() {
pwTextField.tag = 2
pwTextField.attributedPlaceholder = NSAttributedString(string: "비밀번호를 입력해 주세요", attributes: [NSAttributedString.Key.foregroundColor: UIColor(hex: "#999999")!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14, weight: .regular)])
Expand All @@ -276,7 +270,6 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
pwTextField.textContentType = .newPassword
pwTextField.clearButtonMode = .always
}

private func configurePwAgainTextField() {
pwAgainTextField.tag = 3
pwAgainTextField.attributedPlaceholder = NSAttributedString(string: "비밀번호를 한번 더 입력해 주세요", attributes: [NSAttributedString.Key.foregroundColor: UIColor(hex: "#999999")!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14, weight: .regular)])
Expand All @@ -293,8 +286,36 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
pwAgainTextField.textContentType = .newPassword
pwAgainTextField.clearButtonMode = .always
}

// UITextFieldDelegate 메서드
}

extension JoinViewController {
func callJoinAPI(completion: @escaping (Bool) -> Void) {
guard let data = self.joinDTO else {
print("User Data가 없습니다.")
completion(false)
return
}

provider.request(.postRegister(data: data)) { result in
switch result {
case .success(let response):
do {
let data = try response.map(APIResponseString.self)
print("User Created: \(data)")
completion(data.isSuccess)
} catch {
print("Failed to map data: \(error)")
completion(false)
}
case .failure(let error):
print("Request failed: \(error)")
completion(false)
}
}
}
}

extension JoinViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.becomeFirstResponder()
// 텍스트 필드가 선택되었을 때 배경색 변경
Expand All @@ -308,7 +329,7 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
textField.setPwIcon(UIImage(named: "icon_lock_fill")!)
}
}

func textFieldDidEndEditing(_ textField: UITextField) {
// 텍스트 필드가 선택 해제되었을 때 배경색 원래대로
textField.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0.26, alpha: 0.5)
Expand All @@ -320,8 +341,6 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
else if textField.tag == 2 || textField.tag == 3 {
textField.setPwIcon(UIImage(named: "icon_lock")!)
}
// self.userID = self.idTextField.text
// self.userPW = self.pwTextField.text
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
Expand All @@ -334,49 +353,24 @@ class JoinViewController: UIViewController, UITextFieldDelegate {
self.pwAgainTextField.becomeFirstResponder()
} else if textField == self.pwAgainTextField {
if let rePW = self.pwAgainTextField.text {
// guard pw == rePW else {
// // toast message 띄우기
// }
// 토스트 메세지 띄우기
}
self.pwAgainTextField.resignFirstResponder()
}
return true
}

private func assignUserData() {
self.userID = self.idTextField.text
self.userPW = self.pwTextField.text
self.joinDTO = JoinNLoginRequest(username: self.userID ?? "", password: self.userPW ?? "")
}

// 배경 클릭시 키보드 내림 ==> view 에 터치가 들어오면 에디팅모드를 끝냄.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
self.view.endEditing(true) //firstresponder가 전부 사라짐
}

private func callJoinAPI(completion: @escaping (Bool) -> Void) {
if let data = self.joinDTO {
provider.request(.postRegister(data: data)) { result in
switch result {
case .success(let response):
do {
let data = try response.map(APIResponseString.self)
print("User Created: \(data)")
completion(data.isSuccess)
} catch {
print("Failed to map data: \(error)")
completion(false)
}
case .failure(let error):
print("Request failed: \(error)")
completion(false)
}
}
} else {
print("User Data가 없습니다.")
completion(false)
}
}
}

extension JoinViewController {
func assignUserData() {
self.userID = self.idTextField.text
self.userPW = self.pwTextField.text
self.joinDTO = JoinNLoginRequest(username: self.userID ?? "", password: self.userPW ?? "")
}
}

0 comments on commit 1503cb7

Please sign in to comment.