From 64adf8208a82b470e8a0753e45d364ea3ce60684 Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Thu, 31 Mar 2022 01:31:57 +0900 Subject: [PATCH 01/12] =?UTF-8?q?Chore:=20=ED=8C=8C=EC=9D=BC=20=EA=B2=BD?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD,=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=A3=BC=EC=84=9D=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SignUp/SignUp.xcodeproj/project.pbxproj | 10 +++++++++- SignUp/SignUp/Common/HttpRequestHandler.swift | 1 - .../AlertPresentable.swift | 0 3 files changed, 9 insertions(+), 2 deletions(-) rename SignUp/SignUp/Views/{DelegateProtocols => New Group}/AlertPresentable.swift (100%) diff --git a/SignUp/SignUp.xcodeproj/project.pbxproj b/SignUp/SignUp.xcodeproj/project.pbxproj index db089dc..d2321ee 100644 --- a/SignUp/SignUp.xcodeproj/project.pbxproj +++ b/SignUp/SignUp.xcodeproj/project.pbxproj @@ -70,7 +70,6 @@ isa = PBXGroup; children = ( B557E9C527F4401700448EC6 /* LoginViewDeleagte.swift */, - B557E9CD27F48A1E00448EC6 /* AlertPresentable.swift */, ); path = DelegateProtocols; sourceTree = ""; @@ -91,6 +90,14 @@ path = Protocols; sourceTree = ""; }; + B557E9D027F4BD4D00448EC6 /* New Group */ = { + isa = PBXGroup; + children = ( + B557E9CD27F48A1E00448EC6 /* AlertPresentable.swift */, + ); + path = "New Group"; + sourceTree = ""; + }; B571ECD327F1FA1100DF501F /* Controllers */ = { isa = PBXGroup; children = ( @@ -104,6 +111,7 @@ B571ECD627F355A700DF501F /* Views */ = { isa = PBXGroup; children = ( + B557E9D027F4BD4D00448EC6 /* New Group */, B557E9C427F4400400448EC6 /* DelegateProtocols */, B571ECD727F355C300DF501F /* LoginView.swift */, B571ECD927F363BE00DF501F /* SigningUpView.swift */, diff --git a/SignUp/SignUp/Common/HttpRequestHandler.swift b/SignUp/SignUp/Common/HttpRequestHandler.swift index 76a647f..ba1c32e 100644 --- a/SignUp/SignUp/Common/HttpRequestHandler.swift +++ b/SignUp/SignUp/Common/HttpRequestHandler.swift @@ -31,7 +31,6 @@ class HttpRequestHandler{ guard let data = data, let response = response as? HTTPURLResponse else { self.logger.error("Could not receive any response or data") - //네트워크 오류로 인한 로그인 실패 출력 return } diff --git a/SignUp/SignUp/Views/DelegateProtocols/AlertPresentable.swift b/SignUp/SignUp/Views/New Group/AlertPresentable.swift similarity index 100% rename from SignUp/SignUp/Views/DelegateProtocols/AlertPresentable.swift rename to SignUp/SignUp/Views/New Group/AlertPresentable.swift From 78374f9f728e275237051c711219dc31dc774f9b Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Thu, 31 Mar 2022 16:19:54 +0900 Subject: [PATCH 02/12] =?UTF-8?q?Chore:=20=EB=94=94=EB=A0=89=ED=86=A0?= =?UTF-8?q?=EB=A6=AC=20=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SignUp/SignUp.xcodeproj/project.pbxproj | 8 ++++---- .../Views/{New Group => Protocols}/AlertPresentable.swift | 0 2 files changed, 4 insertions(+), 4 deletions(-) rename SignUp/SignUp/Views/{New Group => Protocols}/AlertPresentable.swift (100%) diff --git a/SignUp/SignUp.xcodeproj/project.pbxproj b/SignUp/SignUp.xcodeproj/project.pbxproj index d2321ee..36f2abc 100644 --- a/SignUp/SignUp.xcodeproj/project.pbxproj +++ b/SignUp/SignUp.xcodeproj/project.pbxproj @@ -11,7 +11,7 @@ B557E9C627F4401700448EC6 /* LoginViewDeleagte.swift in Sources */ = {isa = PBXBuildFile; fileRef = B557E9C527F4401700448EC6 /* LoginViewDeleagte.swift */; }; B557E9C827F441D200448EC6 /* HttpRequestHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = B557E9C727F441D200448EC6 /* HttpRequestHandler.swift */; }; B557E9CC27F4776A00448EC6 /* LoginUserProcotol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B557E9CB27F4776A00448EC6 /* LoginUserProcotol.swift */; }; - B557E9CE27F48A1E00448EC6 /* AlertPresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = B557E9CD27F48A1E00448EC6 /* AlertPresentable.swift */; }; + B557E9DE27F58D8500448EC6 /* AlertPresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = B557E9DD27F58D8500448EC6 /* AlertPresentable.swift */; }; B571ECD527F354E400DF501F /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B571ECD427F354E400DF501F /* LoginViewController.swift */; }; B571ECD827F355C300DF501F /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B571ECD727F355C300DF501F /* LoginView.swift */; }; B571ECDA27F363BE00DF501F /* SigningUpView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B571ECD927F363BE00DF501F /* SigningUpView.swift */; }; @@ -30,7 +30,7 @@ B557E9C527F4401700448EC6 /* LoginViewDeleagte.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewDeleagte.swift; sourceTree = ""; }; B557E9C727F441D200448EC6 /* HttpRequestHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpRequestHandler.swift; sourceTree = ""; }; B557E9CB27F4776A00448EC6 /* LoginUserProcotol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginUserProcotol.swift; sourceTree = ""; }; - B557E9CD27F48A1E00448EC6 /* AlertPresentable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertPresentable.swift; sourceTree = ""; }; + B557E9DD27F58D8500448EC6 /* AlertPresentable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AlertPresentable.swift; path = SignUp/Views/Protocols/AlertPresentable.swift; sourceTree = SOURCE_ROOT; }; B571ECD427F354E400DF501F /* LoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = ""; }; B571ECD727F355C300DF501F /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = ""; }; B571ECD927F363BE00DF501F /* SigningUpView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SigningUpView.swift; sourceTree = ""; }; @@ -93,7 +93,7 @@ B557E9D027F4BD4D00448EC6 /* New Group */ = { isa = PBXGroup; children = ( - B557E9CD27F48A1E00448EC6 /* AlertPresentable.swift */, + B557E9DD27F58D8500448EC6 /* AlertPresentable.swift */, ); path = "New Group"; sourceTree = ""; @@ -224,11 +224,11 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + B557E9DE27F58D8500448EC6 /* AlertPresentable.swift in Sources */, B557E9CC27F4776A00448EC6 /* LoginUserProcotol.swift in Sources */, B571ECD527F354E400DF501F /* LoginViewController.swift in Sources */, B571ECDA27F363BE00DF501F /* SigningUpView.swift in Sources */, B5F8730B27F19EE700FFFE7F /* SigningUpViewController.swift in Sources */, - B557E9CE27F48A1E00448EC6 /* AlertPresentable.swift in Sources */, B557E9C827F441D200448EC6 /* HttpRequestHandler.swift in Sources */, B557E9C627F4401700448EC6 /* LoginViewDeleagte.swift in Sources */, B557E9C327F43B8800448EC6 /* LoginUser.swift in Sources */, diff --git a/SignUp/SignUp/Views/New Group/AlertPresentable.swift b/SignUp/SignUp/Views/Protocols/AlertPresentable.swift similarity index 100% rename from SignUp/SignUp/Views/New Group/AlertPresentable.swift rename to SignUp/SignUp/Views/Protocols/AlertPresentable.swift From 936dfea300182fdc497daa934a727b845ab55846 Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Thu, 31 Mar 2022 16:34:08 +0900 Subject: [PATCH 03/12] =?UTF-8?q?Refactor:=20LoginView=20=EB=82=B4?= =?UTF-8?q?=EB=B6=80=EC=9D=98=20=EC=9E=85=EB=A0=A5=EA=B0=92=EC=9D=98=20?= =?UTF-8?q?=EC=9C=A0=ED=9A=A8=EC=84=B1=20=ED=8C=90=EB=8B=A8=EC=9D=84=20Log?= =?UTF-8?q?inViewController=EC=97=90=EC=84=9C=20=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/LoginViewController.swift | 15 ++++++++++ .../DelegateProtocols/LoginViewDeleagte.swift | 1 + SignUp/SignUp/Views/LoginView.swift | 30 ++++++------------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/SignUp/SignUp/Controllers/LoginViewController.swift b/SignUp/SignUp/Controllers/LoginViewController.swift index 8590313..d5b3236 100644 --- a/SignUp/SignUp/Controllers/LoginViewController.swift +++ b/SignUp/SignUp/Controllers/LoginViewController.swift @@ -21,6 +21,21 @@ extension LoginViewController: LoginViewDelegate{ } } + func determiningLoginButtonValidationRequested(id: String, password: String) { + if(isInputValid(id: id, password: password)){ + self.loginView.validateLoginButton() + }else{ + self.loginView.invalidateLoginButton() + } + + func isInputValid(id: String, password: String)-> Bool{ + if(id.count <= 0 || password.count <= 0){ + return false + } + return true + } + } + @objc func responseReceived(_ notification: Notification){ guard let responseBody = notification.userInfo?[HttpResponseKey.responseBodyData] as? Dictionary else { return } guard let status = responseBody["status"] else { return } diff --git a/SignUp/SignUp/Views/DelegateProtocols/LoginViewDeleagte.swift b/SignUp/SignUp/Views/DelegateProtocols/LoginViewDeleagte.swift index 4e4a57a..a585979 100644 --- a/SignUp/SignUp/Views/DelegateProtocols/LoginViewDeleagte.swift +++ b/SignUp/SignUp/Views/DelegateProtocols/LoginViewDeleagte.swift @@ -2,4 +2,5 @@ import Foundation protocol LoginViewDelegate: AnyObject{ func sendIdAndPasswordInput(id: String, password: String) + func determiningLoginButtonValidationRequested(id: String, password: String) } diff --git a/SignUp/SignUp/Views/LoginView.swift b/SignUp/SignUp/Views/LoginView.swift index adc1225..9864c21 100644 --- a/SignUp/SignUp/Views/LoginView.swift +++ b/SignUp/SignUp/Views/LoginView.swift @@ -14,40 +14,28 @@ class LoginView: UIView { } @IBAction func idInputEditingChanged(_ sender: UITextField) { - self.checkLoginButtonValidation() + guard let idInput = self.idTextField.text else { return } + guard let passwordInput = self.passwordTextField.text else { return } + self.delegate?.determiningLoginButtonValidationRequested(id: idInput, password: passwordInput) } @IBAction func passwordInputEditingChanged(_ sender: Any) { - self.checkLoginButtonValidation() + guard let idInput = self.idTextField.text else { return } + guard let passwordInput = self.passwordTextField.text else { return } + self.delegate?.determiningLoginButtonValidationRequested(id: idInput, password: passwordInput) } override func touchesBegan(_ touches: Set, with event: UIEvent?) { + super.touchesBegan(touches, with: event) self.endEditing(true) } - private func checkLoginButtonValidation(){ - guard let idInput = self.idTextField.text else { return } - guard let passwordInput = self.passwordTextField.text else { return } - if(isInputValid(id: idInput, password: passwordInput)){ - validateLoginButton() - }else{ - invalidateLoginButton() - } - - func isInputValid(id: String, password: String)-> Bool{ - if(id.count <= 0 || password.count <= 0){ - return false - } - return true - } - } - - private func invalidateLoginButton(){ + func invalidateLoginButton(){ guard let loginButton = self.loginButton else { return } loginButton.isEnabled = false } - private func validateLoginButton(){ + func validateLoginButton(){ guard let loginButton = self.loginButton else { return } loginButton.isEnabled = true } From 5e11064e5d5946cf589405dd6238c5c252d8cb33 Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Thu, 31 Mar 2022 16:35:33 +0900 Subject: [PATCH 04/12] =?UTF-8?q?Refactor:=20LoginView=20=EB=82=B4?= =?UTF-8?q?=EB=B6=80=EC=9D=98=20delegate=20=EC=95=BD=ED=95=9C=EC=B0=B8?= =?UTF-8?q?=EC=A1=B0=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SignUp/SignUp/Views/LoginView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SignUp/SignUp/Views/LoginView.swift b/SignUp/SignUp/Views/LoginView.swift index 9864c21..5d44fd8 100644 --- a/SignUp/SignUp/Views/LoginView.swift +++ b/SignUp/SignUp/Views/LoginView.swift @@ -5,7 +5,7 @@ class LoginView: UIView { @IBOutlet weak var idTextField: UITextField! @IBOutlet weak var passwordTextField: UITextField! @IBOutlet weak var loginButton: UIButton! - var delegate: LoginViewDelegate? + weak var delegate: LoginViewDelegate? @IBAction func loginButtonTouched(_ sender: UIButton) { guard let idInput = self.idTextField.text else { return } From bacf73672d44d858abb683128d7a95eaaf47bab9 Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Sat, 2 Apr 2022 00:28:51 +0900 Subject: [PATCH 05/12] =?UTF-8?q?Refactor:=20LoginViewController=20-=20Htt?= =?UTF-8?q?pRequestHandler=20=EA=B0=84=20NotificationCenter=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EB=8A=94=20=EB=B6=80=EB=B6=84=20=EB=AA=A8?= =?UTF-8?q?=EB=91=90=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - responseReceived 메소드는 @objc 제거 후, alert를 구성한다는 의미로 composeAlertWithResponse로 변경 --- SignUp/SignUp/Common/HttpRequestHandler.swift | 6 ------ SignUp/SignUp/Controllers/LoginViewController.swift | 3 --- 2 files changed, 9 deletions(-) diff --git a/SignUp/SignUp/Common/HttpRequestHandler.swift b/SignUp/SignUp/Common/HttpRequestHandler.swift index ba1c32e..4b4f768 100644 --- a/SignUp/SignUp/Common/HttpRequestHandler.swift +++ b/SignUp/SignUp/Common/HttpRequestHandler.swift @@ -7,12 +7,8 @@ enum HttpMethod: String{ case put = "PUT" } -enum HttpResponseKey: String{ - case responseBodyData = "responseBodyData" } -struct NotificationName{ - static let normalResponseReceived = Notification.Name("normalResponseReceived") } class HttpRequestHandler{ @@ -40,8 +36,6 @@ class HttpRequestHandler{ } guard let responseBody = try? JSONDecoder().decode(Dictionary.self, from: data) else { return } - NotificationCenter.default.post(name: NotificationName.normalResponseReceived, object: self, userInfo: [HttpResponseKey.responseBodyData:responseBody]) - }.resume() } } diff --git a/SignUp/SignUp/Controllers/LoginViewController.swift b/SignUp/SignUp/Controllers/LoginViewController.swift index d5b3236..bfe7f85 100644 --- a/SignUp/SignUp/Controllers/LoginViewController.swift +++ b/SignUp/SignUp/Controllers/LoginViewController.swift @@ -7,7 +7,6 @@ class LoginViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() loginView.delegate = self - NotificationCenter.default.addObserver(self, selector: #selector(responseReceived(_:)), name: NotificationName.normalResponseReceived, object: HttpRequestHandler.self) } } @@ -36,8 +35,6 @@ extension LoginViewController: LoginViewDelegate{ } } - @objc func responseReceived(_ notification: Notification){ - guard let responseBody = notification.userInfo?[HttpResponseKey.responseBodyData] as? Dictionary else { return } guard let status = responseBody["status"] else { return } var alertMessage: String = "" From 1d026c436c4cc338ce5fd6ae2eff8af8fbbc76d9 Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Sat, 2 Apr 2022 00:33:08 +0900 Subject: [PATCH 06/12] =?UTF-8?q?Refactor:=20HttpRequestHandler=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EC=9D=91=EB=8B=B5=20=EC=98=A4=EB=A5=98=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=20=EC=8B=9C=20=EB=A1=9C=EA=B7=B8=20=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=ED=95=98=EB=8D=98=20=EB=B6=80=EB=B6=84=EC=9D=84=20=EB=AA=A8?= =?UTF-8?q?=EB=91=90=20=EC=A0=9C=EA=B1=B0=ED=95=98=EA=B3=A0=20=ED=81=B4?= =?UTF-8?q?=EB=A1=9C=EC=A0=80=20=ED=98=B8=EC=B6=9C=EB=A1=9C=20=EB=8C=80?= =?UTF-8?q?=EC=B2=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로그 찍는 코드 모두 제거 - 오류가 발생했다고 판단되면, Result 타입을 파라미터로 받는 클로저를 호출하도록 변경 --- SignUp/SignUp/Common/HttpRequestHandler.swift | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/SignUp/SignUp/Common/HttpRequestHandler.swift b/SignUp/SignUp/Common/HttpRequestHandler.swift index 4b4f768..c04c664 100644 --- a/SignUp/SignUp/Common/HttpRequestHandler.swift +++ b/SignUp/SignUp/Common/HttpRequestHandler.swift @@ -1,5 +1,4 @@ import Foundation -import OSLog enum HttpMethod: String{ case get = "GET" @@ -9,29 +8,43 @@ enum HttpMethod: String{ } +enum HttpError: Error, CustomStringConvertible{ + case normalError(error: Error) + case dataNotReceivedError + case requestError + + var description: String{ + switch self { + case .normalError(let error): + return error.localizedDescription + case .dataNotReceivedError: + return "data not received" + case .requestError: + return "client request error" + } + } } class HttpRequestHandler{ - static let logger = Logger() static func sendRequest(data: Data?, url: URL, httpMethod: HttpMethod, completion: @escaping ()->Void){ var request = URLRequest(url: url) request.httpMethod = httpMethod.rawValue request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.httpBody = data URLSession.shared.dataTask(with: request){ data, response, error in - guard error == nil else { - self.logger.error("\(error.debugDescription)") + if let error = error{ + completion(.failure(HttpError.normalError(error: error))) return } - + guard let data = data, let response = response as? HTTPURLResponse else { - self.logger.error("Could not receive any response or data") + completion(.failure(HttpError.dataNotReceivedError)) return } - + if(response.statusCode >= 400){ - self.logger.debug("detected response with \(response.statusCode) status\nurl : \(url)") + completion(.failure(HttpError.requestError)) return } From ba70c0f387d00a06d165c866b85f41942dffe176 Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Sat, 2 Apr 2022 00:34:27 +0900 Subject: [PATCH 07/12] =?UTF-8?q?Add:=20HttpResponse=20=EA=B5=AC=EC=A1=B0?= =?UTF-8?q?=EC=B2=B4,=20HttpResponseHandleable=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=ED=86=A0=EC=BD=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SignUp/SignUp.xcodeproj/project.pbxproj | 8 ++++++++ SignUp/SignUp/Common/HttpResponse.swift | 12 ++++++++++++ SignUp/SignUp/Common/HttpResponseHandleable.swift | 6 ++++++ 3 files changed, 26 insertions(+) create mode 100644 SignUp/SignUp/Common/HttpResponse.swift create mode 100644 SignUp/SignUp/Common/HttpResponseHandleable.swift diff --git a/SignUp/SignUp.xcodeproj/project.pbxproj b/SignUp/SignUp.xcodeproj/project.pbxproj index 36f2abc..698fc3b 100644 --- a/SignUp/SignUp.xcodeproj/project.pbxproj +++ b/SignUp/SignUp.xcodeproj/project.pbxproj @@ -12,11 +12,13 @@ B557E9C827F441D200448EC6 /* HttpRequestHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = B557E9C727F441D200448EC6 /* HttpRequestHandler.swift */; }; B557E9CC27F4776A00448EC6 /* LoginUserProcotol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B557E9CB27F4776A00448EC6 /* LoginUserProcotol.swift */; }; B557E9DE27F58D8500448EC6 /* AlertPresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = B557E9DD27F58D8500448EC6 /* AlertPresentable.swift */; }; + B557E9E027F5966400448EC6 /* HttpResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = B557E9DF27F5966400448EC6 /* HttpResponse.swift */; }; B571ECD527F354E400DF501F /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B571ECD427F354E400DF501F /* LoginViewController.swift */; }; B571ECD827F355C300DF501F /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B571ECD727F355C300DF501F /* LoginView.swift */; }; B571ECDA27F363BE00DF501F /* SigningUpView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B571ECD927F363BE00DF501F /* SigningUpView.swift */; }; B571ECDC27F3722900DF501F /* PersonalInfoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B571ECDB27F3722900DF501F /* PersonalInfoViewController.swift */; }; B571ECDE27F3723C00DF501F /* PersonalInfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B571ECDD27F3723C00DF501F /* PersonalInfoView.swift */; }; + B5EC66A427F74A010042EFCF /* HttpResponseHandleable.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5EC66A327F74A010042EFCF /* HttpResponseHandleable.swift */; }; B5F8730727F19EE700FFFE7F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5F8730627F19EE700FFFE7F /* AppDelegate.swift */; }; B5F8730927F19EE700FFFE7F /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5F8730827F19EE700FFFE7F /* SceneDelegate.swift */; }; B5F8730B27F19EE700FFFE7F /* SigningUpViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5F8730A27F19EE700FFFE7F /* SigningUpViewController.swift */; }; @@ -31,11 +33,13 @@ B557E9C727F441D200448EC6 /* HttpRequestHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpRequestHandler.swift; sourceTree = ""; }; B557E9CB27F4776A00448EC6 /* LoginUserProcotol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginUserProcotol.swift; sourceTree = ""; }; B557E9DD27F58D8500448EC6 /* AlertPresentable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AlertPresentable.swift; path = SignUp/Views/Protocols/AlertPresentable.swift; sourceTree = SOURCE_ROOT; }; + B557E9DF27F5966400448EC6 /* HttpResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpResponse.swift; sourceTree = ""; }; B571ECD427F354E400DF501F /* LoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = ""; }; B571ECD727F355C300DF501F /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = ""; }; B571ECD927F363BE00DF501F /* SigningUpView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SigningUpView.swift; sourceTree = ""; }; B571ECDB27F3722900DF501F /* PersonalInfoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersonalInfoViewController.swift; sourceTree = ""; }; B571ECDD27F3723C00DF501F /* PersonalInfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersonalInfoView.swift; sourceTree = ""; }; + B5EC66A327F74A010042EFCF /* HttpResponseHandleable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = HttpResponseHandleable.swift; path = SignUp/Common/HttpResponseHandleable.swift; sourceTree = SOURCE_ROOT; }; B5F8730327F19EE700FFFE7F /* SignUp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SignUp.app; sourceTree = BUILT_PRODUCTS_DIR; }; B5F8730627F19EE700FFFE7F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; B5F8730827F19EE700FFFE7F /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -78,6 +82,7 @@ isa = PBXGroup; children = ( B557E9C727F441D200448EC6 /* HttpRequestHandler.swift */, + B557E9DF27F5966400448EC6 /* HttpResponse.swift */, ); path = Common; sourceTree = ""; @@ -94,6 +99,7 @@ isa = PBXGroup; children = ( B557E9DD27F58D8500448EC6 /* AlertPresentable.swift */, + B5EC66A327F74A010042EFCF /* HttpResponseHandleable.swift */, ); path = "New Group"; sourceTree = ""; @@ -235,7 +241,9 @@ B5F8730727F19EE700FFFE7F /* AppDelegate.swift in Sources */, B571ECDE27F3723C00DF501F /* PersonalInfoView.swift in Sources */, B571ECDC27F3722900DF501F /* PersonalInfoViewController.swift in Sources */, + B5EC66A427F74A010042EFCF /* HttpResponseHandleable.swift in Sources */, B571ECD827F355C300DF501F /* LoginView.swift in Sources */, + B557E9E027F5966400448EC6 /* HttpResponse.swift in Sources */, B5F8730927F19EE700FFFE7F /* SceneDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/SignUp/SignUp/Common/HttpResponse.swift b/SignUp/SignUp/Common/HttpResponse.swift new file mode 100644 index 0000000..ded419e --- /dev/null +++ b/SignUp/SignUp/Common/HttpResponse.swift @@ -0,0 +1,12 @@ +import Foundation + +struct HttpResponse: Codable{ + + var result: String? + var status: String? + + init(result: String, status: String){ + self.result = result + self.status = status + } +} diff --git a/SignUp/SignUp/Common/HttpResponseHandleable.swift b/SignUp/SignUp/Common/HttpResponseHandleable.swift new file mode 100644 index 0000000..954f3d0 --- /dev/null +++ b/SignUp/SignUp/Common/HttpResponseHandleable.swift @@ -0,0 +1,6 @@ +import Foundation + +protocol HttpResponseHandlable{ + func handleSuccess(data: Data) + func handleFailure(error: Error) +} From 390914f1ced9924354d8567b0b66846901d658c4 Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Sat, 2 Apr 2022 00:39:24 +0900 Subject: [PATCH 08/12] =?UTF-8?q?Refactor:=20LoginViewController=EC=97=90?= =?UTF-8?q?=20HttpResponseHandleable=20=EC=B1=84=ED=83=9D,=20HttpRequestHa?= =?UTF-8?q?ndler=EC=9D=98=20=EC=9D=91=EB=8B=B5=20=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=EB=8A=94=20=EB=AA=A8=EB=91=90=20=EC=A0=84=EB=8B=AC=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=ED=81=B4=EB=A1=9C=EC=A0=80=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=88=98=ED=96=89=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - HttpRequestHandler.sendRequest() 메소드는 우선 응답을 받으면, 이에 대한 처리를 모두 파라미터로 받은 클로저 호출을 통해 처리 - LoginViewController은 해당 클로저를 HttpRquestHandler.handleResponse() 메소드에서 success/failure 여부에 따라 달리 처리하도록 함 - 해당 메소드에서 파라미터로 HttpResponseHandleable을 채택한 타겟을 받아서, handleSuccess, handleFailure 메소드를 각기 호출 - LoginViewController 내부에 HttpResponseHandleable 채택해서, 필요한 메소드 구현 --- SignUp/SignUp/Common/HttpRequestHandler.swift | 14 ++++++-- .../Controllers/LoginViewController.swift | 33 ++++++++++++------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/SignUp/SignUp/Common/HttpRequestHandler.swift b/SignUp/SignUp/Common/HttpRequestHandler.swift index c04c664..9dc407a 100644 --- a/SignUp/SignUp/Common/HttpRequestHandler.swift +++ b/SignUp/SignUp/Common/HttpRequestHandler.swift @@ -32,6 +32,7 @@ class HttpRequestHandler{ request.httpMethod = httpMethod.rawValue request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.httpBody = data + static func sendRequest(data: Data?, url: URL, httpMethod: HttpMethod, completion: @escaping (_ result: Result)->Void){ URLSession.shared.dataTask(with: request){ data, response, error in if let error = error{ completion(.failure(HttpError.normalError(error: error))) @@ -47,8 +48,17 @@ class HttpRequestHandler{ completion(.failure(HttpError.requestError)) return } - - guard let responseBody = try? JSONDecoder().decode(Dictionary.self, from: data) else { return } + + completion(.success(data)) }.resume() } + + static func handleResponse(target: HttpResponseHandlable, result: Result){ + switch result{ + case .success(let data): + target.handleSuccess(data: data) + case .failure(let error): + target.handleFailure(error: error) + } + } } diff --git a/SignUp/SignUp/Controllers/LoginViewController.swift b/SignUp/SignUp/Controllers/LoginViewController.swift index bfe7f85..eae92da 100644 --- a/SignUp/SignUp/Controllers/LoginViewController.swift +++ b/SignUp/SignUp/Controllers/LoginViewController.swift @@ -10,15 +10,7 @@ class LoginViewController: UIViewController { } } -extension LoginViewController: LoginViewDelegate{ - - func sendIdAndPasswordInput(id: String, password: String) { - let loginUser = LoginUser(id: id, password: password) - let jsonData = try? JSONEncoder().encode(loginUser) - if let url = URL(string: "https://api.codesquad.kr/signup"){ - HttpRequestHandler.sendRequest(data: jsonData, url: url, httpMethod: HttpMethod.post){} - } - } +extension LoginViewController: LoginViewDelegate, HttpResponseHandlable{ func determiningLoginButtonValidationRequested(id: String, password: String) { if(isInputValid(id: id, password: password)){ @@ -35,8 +27,27 @@ extension LoginViewController: LoginViewDelegate{ } } - guard let status = responseBody["status"] else { return } - + func sendIdAndPasswordInput(id: String, password: String) { + let loginUser = LoginUser(id: id, password: password) + let jsonData = try? JSONEncoder().encode(loginUser) + if let url = URL(string: "https://api.codesquad.kr/signup"){ + HttpRequestHandler.sendRequest(data: jsonData, url: url, httpMethod: HttpMethod.post){ result in + HttpRequestHandler.handleResponse(target: self, result: result) + } + } + } + + func handleSuccess(data: Data) { + guard let responseBody = try? JSONDecoder().decode(HttpResponse.self, from: data) else { return } + self.composeAlertWithResponse(responseBody: responseBody) + } + + func handleFailure(error: Error) { + self.presentAlert(title: "네트워크 에러", message: "\(error)") + } + + func composeAlertWithResponse(responseBody: HttpResponse){ + guard let status = responseBody.status else { return } var alertMessage: String = "" if(status == "409"){ alertMessage = "로그인 성공" From 2c61e539087cebe536a85cab90a76712ce6d8e20 Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Sat, 2 Apr 2022 00:41:07 +0900 Subject: [PATCH 09/12] =?UTF-8?q?Refactor:=20HttpMethod=20=EC=97=B4?= =?UTF-8?q?=EA=B1=B0=ED=98=95=EC=95=A0=20CustomStringConvertible=20?= =?UTF-8?q?=EC=B1=84=ED=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SignUp/SignUp/Common/HttpRequestHandler.swift | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/SignUp/SignUp/Common/HttpRequestHandler.swift b/SignUp/SignUp/Common/HttpRequestHandler.swift index 9dc407a..4b22caf 100644 --- a/SignUp/SignUp/Common/HttpRequestHandler.swift +++ b/SignUp/SignUp/Common/HttpRequestHandler.swift @@ -1,11 +1,20 @@ import Foundation -enum HttpMethod: String{ - case get = "GET" - case post = "POST" - case put = "PUT" -} - +enum HttpMethod: CustomStringConvertible{ + case get + case post + case put + + var description: String{ + switch self{ + case .get: + return "GET" + case .post: + return "POST" + case .put: + return "PUT" + } + } } enum HttpError: Error, CustomStringConvertible{ @@ -30,6 +39,7 @@ class HttpRequestHandler{ static func sendRequest(data: Data?, url: URL, httpMethod: HttpMethod, completion: @escaping ()->Void){ var request = URLRequest(url: url) request.httpMethod = httpMethod.rawValue + request.httpMethod = "\(httpMethod)" request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.httpBody = data static func sendRequest(data: Data?, url: URL, httpMethod: HttpMethod, completion: @escaping (_ result: Result)->Void){ From 73d463dce600dccd17e475f6ba38b3b65ffd6e2d Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Sat, 2 Apr 2022 00:41:46 +0900 Subject: [PATCH 10/12] =?UTF-8?q?Refactor:=20URLRequest=20=ED=83=80?= =?UTF-8?q?=EC=9E=85=EC=9D=84=20=EB=A6=AC=ED=84=B4=ED=95=98=EB=8A=94=20gen?= =?UTF-8?q?erateURLRequest()=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SignUp/SignUp/Common/HttpRequestHandler.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/SignUp/SignUp/Common/HttpRequestHandler.swift b/SignUp/SignUp/Common/HttpRequestHandler.swift index 4b22caf..b79c742 100644 --- a/SignUp/SignUp/Common/HttpRequestHandler.swift +++ b/SignUp/SignUp/Common/HttpRequestHandler.swift @@ -36,13 +36,16 @@ enum HttpError: Error, CustomStringConvertible{ class HttpRequestHandler{ - static func sendRequest(data: Data?, url: URL, httpMethod: HttpMethod, completion: @escaping ()->Void){ + static func generateURLRequest(data: Data?, url: URL, httpMethod: HttpMethod) -> URLRequest{ var request = URLRequest(url: url) - request.httpMethod = httpMethod.rawValue request.httpMethod = "\(httpMethod)" request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.httpBody = data + return request + } + static func sendRequest(data: Data?, url: URL, httpMethod: HttpMethod, completion: @escaping (_ result: Result)->Void){ + let request = generateURLRequest(data: data, url: url, httpMethod: httpMethod) URLSession.shared.dataTask(with: request){ data, response, error in if let error = error{ completion(.failure(HttpError.normalError(error: error))) From e069edfed57fa34419e4325c38e3acdc6d15ad94 Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Sat, 2 Apr 2022 00:42:42 +0900 Subject: [PATCH 11/12] =?UTF-8?q?Chore:=20=ED=94=84=EB=A1=9C=ED=86=A0?= =?UTF-8?q?=EC=BD=9C=20=EC=84=A0=EC=96=B8=EB=90=9C=20=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=EB=93=A4=20=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SignUp/SignUp.xcodeproj/project.pbxproj | 24 +++++++++---------- .../Protocols/AlertPresentable.swift | 0 .../Protocols}/HttpResponseHandleable.swift | 0 3 files changed, 12 insertions(+), 12 deletions(-) rename SignUp/SignUp/{Views => Controllers}/Protocols/AlertPresentable.swift (100%) rename SignUp/SignUp/{Common => Controllers/Protocols}/HttpResponseHandleable.swift (100%) diff --git a/SignUp/SignUp.xcodeproj/project.pbxproj b/SignUp/SignUp.xcodeproj/project.pbxproj index 698fc3b..5b92130 100644 --- a/SignUp/SignUp.xcodeproj/project.pbxproj +++ b/SignUp/SignUp.xcodeproj/project.pbxproj @@ -32,14 +32,14 @@ B557E9C527F4401700448EC6 /* LoginViewDeleagte.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewDeleagte.swift; sourceTree = ""; }; B557E9C727F441D200448EC6 /* HttpRequestHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpRequestHandler.swift; sourceTree = ""; }; B557E9CB27F4776A00448EC6 /* LoginUserProcotol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginUserProcotol.swift; sourceTree = ""; }; - B557E9DD27F58D8500448EC6 /* AlertPresentable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AlertPresentable.swift; path = SignUp/Views/Protocols/AlertPresentable.swift; sourceTree = SOURCE_ROOT; }; + B557E9DD27F58D8500448EC6 /* AlertPresentable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AlertPresentable.swift; path = SignUp/Controllers/Protocols/AlertPresentable.swift; sourceTree = SOURCE_ROOT; }; B557E9DF27F5966400448EC6 /* HttpResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpResponse.swift; sourceTree = ""; }; B571ECD427F354E400DF501F /* LoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = ""; }; B571ECD727F355C300DF501F /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = ""; }; B571ECD927F363BE00DF501F /* SigningUpView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SigningUpView.swift; sourceTree = ""; }; B571ECDB27F3722900DF501F /* PersonalInfoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersonalInfoViewController.swift; sourceTree = ""; }; B571ECDD27F3723C00DF501F /* PersonalInfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersonalInfoView.swift; sourceTree = ""; }; - B5EC66A327F74A010042EFCF /* HttpResponseHandleable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = HttpResponseHandleable.swift; path = SignUp/Common/HttpResponseHandleable.swift; sourceTree = SOURCE_ROOT; }; + B5EC66A327F74A010042EFCF /* HttpResponseHandleable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = HttpResponseHandleable.swift; path = SignUp/Controllers/Protocols/HttpResponseHandleable.swift; sourceTree = SOURCE_ROOT; }; B5F8730327F19EE700FFFE7F /* SignUp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SignUp.app; sourceTree = BUILT_PRODUCTS_DIR; }; B5F8730627F19EE700FFFE7F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; B5F8730827F19EE700FFFE7F /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -95,18 +95,10 @@ path = Protocols; sourceTree = ""; }; - B557E9D027F4BD4D00448EC6 /* New Group */ = { - isa = PBXGroup; - children = ( - B557E9DD27F58D8500448EC6 /* AlertPresentable.swift */, - B5EC66A327F74A010042EFCF /* HttpResponseHandleable.swift */, - ); - path = "New Group"; - sourceTree = ""; - }; B571ECD327F1FA1100DF501F /* Controllers */ = { isa = PBXGroup; children = ( + B5EC66A527F754CE0042EFCF /* Protocols */, B5F8730A27F19EE700FFFE7F /* SigningUpViewController.swift */, B571ECD427F354E400DF501F /* LoginViewController.swift */, B571ECDB27F3722900DF501F /* PersonalInfoViewController.swift */, @@ -117,7 +109,6 @@ B571ECD627F355A700DF501F /* Views */ = { isa = PBXGroup; children = ( - B557E9D027F4BD4D00448EC6 /* New Group */, B557E9C427F4400400448EC6 /* DelegateProtocols */, B571ECD727F355C300DF501F /* LoginView.swift */, B571ECD927F363BE00DF501F /* SigningUpView.swift */, @@ -126,6 +117,15 @@ path = Views; sourceTree = ""; }; + B5EC66A527F754CE0042EFCF /* Protocols */ = { + isa = PBXGroup; + children = ( + B557E9DD27F58D8500448EC6 /* AlertPresentable.swift */, + B5EC66A327F74A010042EFCF /* HttpResponseHandleable.swift */, + ); + path = Protocols; + sourceTree = ""; + }; B5F872FA27F19EE700FFFE7F = { isa = PBXGroup; children = ( diff --git a/SignUp/SignUp/Views/Protocols/AlertPresentable.swift b/SignUp/SignUp/Controllers/Protocols/AlertPresentable.swift similarity index 100% rename from SignUp/SignUp/Views/Protocols/AlertPresentable.swift rename to SignUp/SignUp/Controllers/Protocols/AlertPresentable.swift diff --git a/SignUp/SignUp/Common/HttpResponseHandleable.swift b/SignUp/SignUp/Controllers/Protocols/HttpResponseHandleable.swift similarity index 100% rename from SignUp/SignUp/Common/HttpResponseHandleable.swift rename to SignUp/SignUp/Controllers/Protocols/HttpResponseHandleable.swift From c46662f8e67f8c64a8fb3b63be35e9d2758e93da Mon Sep 17 00:00:00 2001 From: Joonwoo Lee <68586291+junu0516@users.noreply.github.com> Date: Sat, 2 Apr 2022 01:01:47 +0900 Subject: [PATCH 12/12] =?UTF-8?q?Refactor:=20LoginViewController=EC=97=90?= =?UTF-8?q?=EC=84=9C=20HttpRequestHandler.sendRequest()=20=ED=98=B8?= =?UTF-8?q?=EC=B6=9C=20=EC=8B=9C=20=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0?= =?UTF-8?q?=EB=A1=9C=20=EB=84=98=EA=B2=A8=EC=A3=BC=EB=8D=98=20=ED=81=B4?= =?UTF-8?q?=EB=A1=9C=EC=A0=80=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 클로저 내부에서 이미 HttpRequestHandler 내부의 다른 정적 메소드를 호출하기 때문에, 굳이 클로저를 사용할 필요가 없으므로 제거 - 파라미터로 LoginViewController의 추상 타입 참조정보만 넘겨주도록 수정 --- SignUp/SignUp/Common/HttpRequestHandler.swift | 14 +++++++------- .../SignUp/Controllers/LoginViewController.swift | 4 +--- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/SignUp/SignUp/Common/HttpRequestHandler.swift b/SignUp/SignUp/Common/HttpRequestHandler.swift index b79c742..15d8154 100644 --- a/SignUp/SignUp/Common/HttpRequestHandler.swift +++ b/SignUp/SignUp/Common/HttpRequestHandler.swift @@ -44,28 +44,28 @@ class HttpRequestHandler{ return request } - static func sendRequest(data: Data?, url: URL, httpMethod: HttpMethod, completion: @escaping (_ result: Result)->Void){ + static func sendRequest(data: Data?, url: URL, httpMethod: HttpMethod, target: HttpResponseHandlable) { let request = generateURLRequest(data: data, url: url, httpMethod: httpMethod) URLSession.shared.dataTask(with: request){ data, response, error in if let error = error{ - completion(.failure(HttpError.normalError(error: error))) + self.handleResponse(target: target, result: .failure(HttpError.normalError(error: error))) return } guard let data = data, let response = response as? HTTPURLResponse else { - completion(.failure(HttpError.dataNotReceivedError)) + self.handleResponse(target: target, result: .failure(HttpError.dataNotReceivedError)) return } if(response.statusCode >= 400){ - completion(.failure(HttpError.requestError)) + self.handleResponse(target: target, result: .failure(HttpError.requestError)) return } - - completion(.success(data)) + + self.handleResponse(target: target, result: .success(data)) }.resume() } - + static func handleResponse(target: HttpResponseHandlable, result: Result){ switch result{ case .success(let data): diff --git a/SignUp/SignUp/Controllers/LoginViewController.swift b/SignUp/SignUp/Controllers/LoginViewController.swift index eae92da..31e4770 100644 --- a/SignUp/SignUp/Controllers/LoginViewController.swift +++ b/SignUp/SignUp/Controllers/LoginViewController.swift @@ -31,9 +31,7 @@ extension LoginViewController: LoginViewDelegate, HttpResponseHandlable{ let loginUser = LoginUser(id: id, password: password) let jsonData = try? JSONEncoder().encode(loginUser) if let url = URL(string: "https://api.codesquad.kr/signup"){ - HttpRequestHandler.sendRequest(data: jsonData, url: url, httpMethod: HttpMethod.post){ result in - HttpRequestHandler.handleResponse(target: self, result: result) - } + HttpRequestHandler.sendRequest(data: jsonData, url: url, httpMethod: HttpMethod.post, target: self) } }