diff --git a/Instagram/Instagram.xcodeproj/project.pbxproj b/Instagram/Instagram.xcodeproj/project.pbxproj index e4246a0..7d8c762 100644 --- a/Instagram/Instagram.xcodeproj/project.pbxproj +++ b/Instagram/Instagram.xcodeproj/project.pbxproj @@ -27,11 +27,10 @@ 921E576827FADA4C00CDC913 /* TempAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E576727FADA4C00CDC913 /* TempAPI.swift */; }; 921E576A27FADA5700CDC913 /* TempDataModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E576927FADA5700CDC913 /* TempDataModel.swift */; }; 921E576C27FADA5F00CDC913 /* TempService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E576B27FADA5F00CDC913 /* TempService.swift */; }; - 921E576E27FADA7800CDC913 /* GeneralResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E576D27FADA7800CDC913 /* GeneralResponse.swift */; }; + 921E576E27FADA7800CDC913 /* BaseResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E576D27FADA7800CDC913 /* BaseResponse.swift */; }; 921E577027FADA8900CDC913 /* GeneralAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E576F27FADA8900CDC913 /* GeneralAPI.swift */; }; 921E577227FADA9800CDC913 /* NetworkResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E577127FADA9800CDC913 /* NetworkResult.swift */; }; 921E577427FADADC00CDC913 /* MoyaLoggingPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E577327FADADC00CDC913 /* MoyaLoggingPlugin.swift */; }; - 921E577627FADB1A00CDC913 /* BaseTargetType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E577527FADB1A00CDC913 /* BaseTargetType.swift */; }; 921E577B27FADC2E00CDC913 /* SignInViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E577A27FADC2E00CDC913 /* SignInViewController.swift */; }; 921E578127FADC5B00CDC913 /* AuthCompleteView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E578027FADC5B00CDC913 /* AuthCompleteView.swift */; }; 921E578327FADC6600CDC913 /* AuthModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 921E578227FADC6600CDC913 /* AuthModel.swift */; }; @@ -51,6 +50,12 @@ 92920C7527FAE95F00DCABC7 /* Assets+Generated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92920C7427FAE95F00DCABC7 /* Assets+Generated.swift */; }; 92920C7727FAF71C00DCABC7 /* Reactive+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92920C7627FAF71C00DCABC7 /* Reactive+.swift */; }; 92920C7927FAFE6800DCABC7 /* UserPasswordViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92920C7827FAFE6800DCABC7 /* UserPasswordViewController.swift */; }; + 92BF252C2829678200DE0205 /* SignUpAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92BF252B2829678200DE0205 /* SignUpAPI.swift */; }; + 92BF252E2829678F00DE0205 /* SignUpService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92BF252D2829678F00DE0205 /* SignUpService.swift */; }; + 92BF25312829687300DE0205 /* SignInResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92BF25302829687300DE0205 /* SignInResponse.swift */; }; + 92BF25332829689700DE0205 /* SignInRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92BF25322829689700DE0205 /* SignInRequest.swift */; }; + 92BF253528296A8800DE0205 /* SignUpRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92BF253428296A8800DE0205 /* SignUpRequest.swift */; }; + 92BF253728296A9300DE0205 /* SignUpResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92BF253628296A9300DE0205 /* SignUpResponse.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -77,11 +82,10 @@ 921E576727FADA4C00CDC913 /* TempAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TempAPI.swift; sourceTree = ""; }; 921E576927FADA5700CDC913 /* TempDataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TempDataModel.swift; sourceTree = ""; }; 921E576B27FADA5F00CDC913 /* TempService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TempService.swift; sourceTree = ""; }; - 921E576D27FADA7800CDC913 /* GeneralResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralResponse.swift; sourceTree = ""; }; + 921E576D27FADA7800CDC913 /* BaseResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseResponse.swift; sourceTree = ""; }; 921E576F27FADA8900CDC913 /* GeneralAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralAPI.swift; sourceTree = ""; }; 921E577127FADA9800CDC913 /* NetworkResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkResult.swift; sourceTree = ""; }; 921E577327FADADC00CDC913 /* MoyaLoggingPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoyaLoggingPlugin.swift; sourceTree = ""; }; - 921E577527FADB1A00CDC913 /* BaseTargetType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseTargetType.swift; sourceTree = ""; }; 921E577A27FADC2E00CDC913 /* SignInViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInViewController.swift; sourceTree = ""; }; 921E578027FADC5B00CDC913 /* AuthCompleteView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthCompleteView.swift; sourceTree = ""; }; 921E578227FADC6600CDC913 /* AuthModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthModel.swift; sourceTree = ""; }; @@ -101,6 +105,12 @@ 92920C7427FAE95F00DCABC7 /* Assets+Generated.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Assets+Generated.swift"; sourceTree = ""; }; 92920C7627FAF71C00DCABC7 /* Reactive+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Reactive+.swift"; sourceTree = ""; }; 92920C7827FAFE6800DCABC7 /* UserPasswordViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserPasswordViewController.swift; sourceTree = ""; }; + 92BF252B2829678200DE0205 /* SignUpAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignUpAPI.swift; sourceTree = ""; }; + 92BF252D2829678F00DE0205 /* SignUpService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignUpService.swift; sourceTree = ""; }; + 92BF25302829687300DE0205 /* SignInResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInResponse.swift; sourceTree = ""; }; + 92BF25322829689700DE0205 /* SignInRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInRequest.swift; sourceTree = ""; }; + 92BF253428296A8800DE0205 /* SignUpRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignUpRequest.swift; sourceTree = ""; }; + 92BF253628296A9300DE0205 /* SignUpResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignUpResponse.swift; sourceTree = ""; }; DB41544B785A02F968ACF11F /* Pods-Instagram.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Instagram.release.xcconfig"; path = "Target Support Files/Pods-Instagram/Pods-Instagram.release.xcconfig"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -297,6 +307,7 @@ 921E576327FAD9F500CDC913 /* API */ = { isa = PBXGroup; children = ( + 92BF252A2829676A00DE0205 /* Auth */, 921E576727FADA4C00CDC913 /* TempAPI.swift */, ); path = API; @@ -305,6 +316,7 @@ 921E576427FAD9FF00CDC913 /* DataModel */ = { isa = PBXGroup; children = ( + 92BF252F2829685000DE0205 /* Auth */, 921E576927FADA5700CDC913 /* TempDataModel.swift */, ); path = DataModel; @@ -313,6 +325,7 @@ 921E576527FADA0700CDC913 /* Service */ = { isa = PBXGroup; children = ( + 92BF25292829675400DE0205 /* Auth */, 921E576B27FADA5F00CDC913 /* TempService.swift */, ); path = Service; @@ -321,9 +334,8 @@ 921E576627FADA3A00CDC913 /* Utility */ = { isa = PBXGroup; children = ( - 921E577527FADB1A00CDC913 /* BaseTargetType.swift */, 921E576F27FADA8900CDC913 /* GeneralAPI.swift */, - 921E576D27FADA7800CDC913 /* GeneralResponse.swift */, + 921E576D27FADA7800CDC913 /* BaseResponse.swift */, 921E577327FADADC00CDC913 /* MoyaLoggingPlugin.swift */, 921E577127FADA9800CDC913 /* NetworkResult.swift */, ); @@ -434,6 +446,33 @@ path = ViewModel; sourceTree = ""; }; + 92BF25292829675400DE0205 /* Auth */ = { + isa = PBXGroup; + children = ( + 92BF252D2829678F00DE0205 /* SignUpService.swift */, + ); + path = Auth; + sourceTree = ""; + }; + 92BF252A2829676A00DE0205 /* Auth */ = { + isa = PBXGroup; + children = ( + 92BF252B2829678200DE0205 /* SignUpAPI.swift */, + ); + path = Auth; + sourceTree = ""; + }; + 92BF252F2829685000DE0205 /* Auth */ = { + isa = PBXGroup; + children = ( + 92BF253628296A9300DE0205 /* SignUpResponse.swift */, + 92BF253428296A8800DE0205 /* SignUpRequest.swift */, + 92BF25302829687300DE0205 /* SignInResponse.swift */, + 92BF25322829689700DE0205 /* SignInRequest.swift */, + ); + path = Auth; + sourceTree = ""; + }; FF974F9CEE94739839915902 /* Frameworks */ = { isa = PBXGroup; children = ( @@ -584,15 +623,17 @@ 9244A9DF27FEDD0400D7C2EC /* SignInViewModel.swift in Sources */, 921E575527FAD0EE00CDC913 /* UIView+.swift in Sources */, 921E579027FADF7B00CDC913 /* SignUpView.swift in Sources */, - 921E576E27FADA7800CDC913 /* GeneralResponse.swift in Sources */, + 92BF252E2829678F00DE0205 /* SignUpService.swift in Sources */, + 921E576E27FADA7800CDC913 /* BaseResponse.swift in Sources */, 92920C7527FAE95F00DCABC7 /* Assets+Generated.swift in Sources */, - 921E577627FADB1A00CDC913 /* BaseTargetType.swift in Sources */, 921E579227FAE23B00CDC913 /* IDSNavigationBar.swift in Sources */, 921E576827FADA4C00CDC913 /* TempAPI.swift in Sources */, + 92BF25332829689700DE0205 /* SignInRequest.swift in Sources */, 920995AF280DD9AD008C3491 /* ShopMainViewController.swift in Sources */, 92920C7927FAFE6800DCABC7 /* UserPasswordViewController.swift in Sources */, 921E576C27FADA5F00CDC913 /* TempService.swift in Sources */, 921E573F27FACB1A00CDC913 /* HomeMainViewController.swift in Sources */, + 92BF252C2829678200DE0205 /* SignUpAPI.swift in Sources */, 921E575727FAD11800CDC913 /* UITextField+.swift in Sources */, 921E578327FADC6600CDC913 /* AuthModel.swift in Sources */, 921E578127FADC5B00CDC913 /* AuthCompleteView.swift in Sources */, @@ -601,6 +642,7 @@ 920995AB280DD965008C3491 /* SearchMainViewController.swift in Sources */, 921E576A27FADA5700CDC913 /* TempDataModel.swift in Sources */, 921E578E27FADD8E00CDC913 /* AuthCompleteViewController.swift in Sources */, + 92BF253528296A8800DE0205 /* SignUpRequest.swift in Sources */, 92920C7727FAF71C00DCABC7 /* Reactive+.swift in Sources */, 921E578827FADCFA00CDC913 /* HomeMainModel.swift in Sources */, 920995A8280DD79F008C3491 /* TabBarController.swift in Sources */, @@ -609,12 +651,14 @@ 921E579827FAE4C900CDC913 /* SignInView.swift in Sources */, 921E576027FAD3AA00CDC913 /* IDSButton.swift in Sources */, 921E575E27FAD25300CDC913 /* IDSTextField.swift in Sources */, + 92BF253728296A9300DE0205 /* SignUpResponse.swift in Sources */, 921E577027FADA8900CDC913 /* GeneralAPI.swift in Sources */, 921E579427FAE2BA00CDC913 /* BackButton.swift in Sources */, 920995B4280DD9D9008C3491 /* ProfileMainViewController.swift in Sources */, 920995B1280DD9B5008C3491 /* ReelsMainViewController.swift in Sources */, 922DD0E8281290B9008F29DF /* UIWindow+.swift in Sources */, 921E577227FADA9800CDC913 /* NetworkResult.swift in Sources */, + 92BF25312829687300DE0205 /* SignInResponse.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Instagram/Instagram/Network/API/Auth/SignUpAPI.swift b/Instagram/Instagram/Network/API/Auth/SignUpAPI.swift new file mode 100644 index 0000000..1e70369 --- /dev/null +++ b/Instagram/Instagram/Network/API/Auth/SignUpAPI.swift @@ -0,0 +1,50 @@ +// +// SignUpAPI.swift +// Instagram +// +// Created by 소연 on 2022/05/10. +// + +import Foundation + +import Moya +import Accelerate + +final class SignUpAPI { + + // MARK: - Static Properties + + static let shared: SignUpAPI = SignUpAPI() + private init() { } + + // MARK: - Network Properties + + var authProvider = MoyaProvider(plugins: [MoyaLoggerPlugin()]) + + public private(set) var signUpResponse: BaseResponse? + public private(set) var signUpData: SignUpResponse? + + // MARK: - GET + + func signUp(parameter: SignUpRequest, completion: @escaping ((SignUpResponse?, Error?) -> ())) { + authProvider.request(.signUp(parameter: parameter)) { [weak self] response in + switch response { + case .success(let result): + do { + self?.signUpResponse = try result.map(BaseResponse.self) + guard let data = self?.signUpResponse?.data else { + completion(nil, Error.self as? Error) + return + } + completion(data, nil) + } catch(let err) { + print(err.localizedDescription) + completion(nil, err) + } + case .failure(let err): + print(err.localizedDescription) + completion(nil, err) + } + } + } +} diff --git a/Instagram/Instagram/Network/DataModel/Auth/SignInRequest.swift b/Instagram/Instagram/Network/DataModel/Auth/SignInRequest.swift new file mode 100644 index 0000000..43a533d --- /dev/null +++ b/Instagram/Instagram/Network/DataModel/Auth/SignInRequest.swift @@ -0,0 +1,14 @@ +// +// SignInRequest.swift +// Instagram +// +// Created by 소연 on 2022/05/10. +// + +import Foundation + +// MARK: - SignUp Request + +struct SignInRequest: Codable { + let email, password: String +} diff --git a/Instagram/Instagram/Network/DataModel/Auth/SignInResponse.swift b/Instagram/Instagram/Network/DataModel/Auth/SignInResponse.swift new file mode 100644 index 0000000..a00f378 --- /dev/null +++ b/Instagram/Instagram/Network/DataModel/Auth/SignInResponse.swift @@ -0,0 +1,14 @@ +// +// SignInResponse.swift +// Instagram +// +// Created by 소연 on 2022/05/10. +// + +import Foundation + +// MARK: - SignIn Response + +struct SignInResponse: Codable { + let name, email: String +} diff --git a/Instagram/Instagram/Network/DataModel/Auth/SignUpRequest.swift b/Instagram/Instagram/Network/DataModel/Auth/SignUpRequest.swift new file mode 100644 index 0000000..6b259b4 --- /dev/null +++ b/Instagram/Instagram/Network/DataModel/Auth/SignUpRequest.swift @@ -0,0 +1,14 @@ +// +// SignUpRequest.swift +// Instagram +// +// Created by 소연 on 2022/05/10. +// + +import Foundation + +// MARK: - SignUp Request + +struct SignUpRequest: Codable { + let name, email, password: String +} diff --git a/Instagram/Instagram/Network/DataModel/Auth/SignUpResponse.swift b/Instagram/Instagram/Network/DataModel/Auth/SignUpResponse.swift new file mode 100644 index 0000000..b854c23 --- /dev/null +++ b/Instagram/Instagram/Network/DataModel/Auth/SignUpResponse.swift @@ -0,0 +1,15 @@ +// +// SignUpResponse.swift +// Instagram +// +// Created by 소연 on 2022/05/10. +// + +import Foundation + +// MARK: - SignUp Response + +struct SignUpResponse: Codable { + let id: Int +} + diff --git a/Instagram/Instagram/Network/Service/Auth/SignUpService.swift b/Instagram/Instagram/Network/Service/Auth/SignUpService.swift new file mode 100644 index 0000000..02ed4e3 --- /dev/null +++ b/Instagram/Instagram/Network/Service/Auth/SignUpService.swift @@ -0,0 +1,58 @@ +// +// AuthService.swift +// Instagram +// +// Created by 소연 on 2022/05/10. +// + +import Foundation + +import Moya + +enum SignUpService { + case signUp(parameter: SignUpRequest) +} + +extension SignUpService: TargetType { + var baseURL: URL { + return URL(string: GeneralAPI.baseURL)! + } + + var path: String { + switch self { + case .signUp: + return GeneralAPI.signUpURL + } + } + + var parameterEncoding: ParameterEncoding { + switch self { + case .signUp: + return JSONEncoding.default + } + } + + var method: Moya.Method { + switch self { + case .signUp: + return .post + } + } + + var task: Task { + switch self { + case .signUp(let parameter): + let parameter: [String: Any] = ["name": parameter.name, + "email": parameter.email, + "password": parameter.password] + return .requestParameters(parameters: parameter, encoding: JSONEncoding.default) + } + } + + var headers: [String : String]? { + switch self { + case .signUp: + return ["Content-Type": "application/json"] + } + } +} diff --git a/Instagram/Instagram/Network/Utility/GeneralResponse.swift b/Instagram/Instagram/Network/Utility/BaseResponse.swift similarity index 70% rename from Instagram/Instagram/Network/Utility/GeneralResponse.swift rename to Instagram/Instagram/Network/Utility/BaseResponse.swift index a5f093b..4838791 100644 --- a/Instagram/Instagram/Network/Utility/GeneralResponse.swift +++ b/Instagram/Instagram/Network/Utility/BaseResponse.swift @@ -7,15 +7,11 @@ import Foundation -struct GenericResponse: Decodable { - var status: Int - var success: Bool - var message: String? - var data: T? - - var statusCase: NetworkResult? { - return NetworkResult(rawValue: status) - } +struct BaseResponse: Decodable { + let status: Int + let success: Bool + let message: String + let data: T? enum CodingKeys: String, CodingKey { case message @@ -33,16 +29,12 @@ struct GenericResponse: Decodable { } } -struct GenericArrayResponse: Decodable { +struct BaseArrayResponseType: Decodable { let status: Int - let message: String? let success: Bool? + let message: String? let data: [T]? - var statusCase: NetworkResult? { - return NetworkResult(rawValue: status) - } - enum CodingKeys: String, CodingKey { case message case data @@ -58,6 +50,3 @@ struct GenericArrayResponse: Decodable { success = (try? values.decode(Bool.self, forKey: .success)) ?? false } } - -/// status, message, success 이외에 정보를 사용하지 않는 경우에 VoidType를 설정해주면 됩니다! -struct VoidType: Decodable {} diff --git a/Instagram/Instagram/Network/Utility/BaseTargetType.swift b/Instagram/Instagram/Network/Utility/BaseTargetType.swift deleted file mode 100644 index fe6f1ef..0000000 --- a/Instagram/Instagram/Network/Utility/BaseTargetType.swift +++ /dev/null @@ -1,23 +0,0 @@ -// -// BaseTargetType.swift -// Instagram -// -// Created by 소연 on 2022/04/04. -// - -import Foundation - -import Moya - -protocol BaseTargetType: TargetType { } - -extension BaseTargetType { - var baseURL: URL { - // FIXME: - Constant로 관리 + 강제언래핑 수정 - return URL(string: "")! - } - - var sampleData: Data { - return Data() - } -} diff --git a/Instagram/Instagram/Network/Utility/GeneralAPI.swift b/Instagram/Instagram/Network/Utility/GeneralAPI.swift index a3dc50d..4dda49e 100644 --- a/Instagram/Instagram/Network/Utility/GeneralAPI.swift +++ b/Instagram/Instagram/Network/Utility/GeneralAPI.swift @@ -6,3 +6,15 @@ // import Foundation + +struct GeneralAPI { + + // MARK: - Base URL + + static let baseURL = "http://13.124.62.236" + + // MARK: - Feature URL + + static let loginURL = "/auth/signin" + static let signUpURL = "/auth/signup" +} diff --git a/Instagram/Instagram/Network/Utility/MoyaLoggingPlugin.swift b/Instagram/Instagram/Network/Utility/MoyaLoggingPlugin.swift index 2f2c261..fe7361f 100644 --- a/Instagram/Instagram/Network/Utility/MoyaLoggingPlugin.swift +++ b/Instagram/Instagram/Network/Utility/MoyaLoggingPlugin.swift @@ -1,5 +1,5 @@ // -// MoyaLoggingPlugin.swift +// MoyaLoggerPlugin.swift // Instagram // // Created by 소연 on 2022/04/04. @@ -9,7 +9,8 @@ import Foundation import Moya -final class MoyaLoggingPlugin: PluginType { +final class MoyaLoggerPlugin: PluginType { + // Request를 보낼 때 호출 func willSend(_ request: RequestType, target: TargetType) { guard let httpRequest = request.request else { @@ -18,15 +19,15 @@ final class MoyaLoggingPlugin: PluginType { } let url = httpRequest.description let method = httpRequest.httpMethod ?? "unknown method" - var log = "----------------------------------------------------\n\n[\(method)] \(url)\n\n----------------------------------------------------\n" - log.append("API: \(target)\n") + var log = "----------------------------------------------------\n1️⃣[\(method)] \(url)\n----------------------------------------------------\n" + log.append("2️⃣API: \(target)\n") if let headers = httpRequest.allHTTPHeaderFields, !headers.isEmpty { log.append("header: \(headers)\n") } if let body = httpRequest.httpBody, let bodyString = String(bytes: body, encoding: String.Encoding.utf8) { log.append("\(bodyString)\n") } - log.append("------------------- END \(method) --------------------------") + log.append("------------------- END \(method) -------------------") print(log) } @@ -34,36 +35,33 @@ final class MoyaLoggingPlugin: PluginType { func didReceive(_ result: Result, target: TargetType) { switch result { case let .success(response): - onSuceed(response, target: target, isFromError: false) + onSuceed(response) case let .failure(error): - onFail(error, target: target) + onFail(error) } } - func onSuceed(_ response: Response, target: TargetType, isFromError: Bool) { + func onSuceed(_ response: Response) { let request = response.request let url = request?.url?.absoluteString ?? "nil" let statusCode = response.statusCode - var log = "------------------- 네트워크 통신 성공 -------------------" - log.append("\n[\(statusCode)] \(url)\n----------------------------------------------------\n") - log.append("API: \(target)\n") - response.response?.allHeaderFields.forEach { - log.append("\($0): \($1)\n") - } + var log = "------------------- 네트워크 통신 성공했는가? -------------------" + log.append("\n3️⃣[\(statusCode)] \(url)\n----------------------------------------------------\n") + log.append("response: \n") if let reString = String(bytes: response.data, encoding: String.Encoding.utf8) { - log.append("\(reString)\n") + log.append("4️⃣\(reString)\n") } - log.append("------------------- END HTTP (\(response.data.count)-byte body) -------------------") + log.append("------------------- END HTTP -------------------") print(log) } - func onFail(_ error: MoyaError, target: TargetType) { + func onFail(_ error: MoyaError) { if let response = error.response { - onSuceed(response, target: target, isFromError: true) + onSuceed(response) return } var log = "네트워크 오류" - log.append("<-- \(error.errorCode) \(target)\n") + log.append("<-- \(error.errorCode)\n") log.append("\(error.failureReason ?? error.errorDescription ?? "unknown error")\n") log.append("<-- END HTTP") print(log) diff --git a/Instagram/Instagram/Network/Utility/NetworkResult.swift b/Instagram/Instagram/Network/Utility/NetworkResult.swift index f3e2d68..03f4765 100644 --- a/Instagram/Instagram/Network/Utility/NetworkResult.swift +++ b/Instagram/Instagram/Network/Utility/NetworkResult.swift @@ -7,18 +7,11 @@ import Foundation -enum NetworkResult: Int { - case okay = 200 - case created = 201 - case noContent = 204 - case badRequest = 400 - case unAuthorized = 401 - case nullValue = 402 - case forbidden = 403 - case notFound = 404 - case conflict = 409 - case internalServerError = 500 - case serviceUnavailable = 503 - case dbError = 600 +enum NetworkResult { + case success(T) // 서버 통신 성공했을 때, + case requestErr(T) // 요청 에러 발생했을 때, + case pathErr // 경로 에러 발생했을 때, + case serverErr // 서버의 내부적 에러가 발생했을 때, + case networkFail // 네트워크 연결 실패했을 때 } diff --git a/Instagram/Instagram/Source/Screen/Auth/Controller/AuthCompleteViewController.swift b/Instagram/Instagram/Source/Screen/Auth/Controller/AuthCompleteViewController.swift index c4aaf9a..7e8e3c5 100644 --- a/Instagram/Instagram/Source/Screen/Auth/Controller/AuthCompleteViewController.swift +++ b/Instagram/Instagram/Source/Screen/Auth/Controller/AuthCompleteViewController.swift @@ -14,6 +14,10 @@ import Then final class AuthCompleteViewController: UIViewController { + // MARK: - Network + + private let signUpAPI = SignUpAPI.shared + // MARK: - Properties var disposeBag = DisposeBag() @@ -45,6 +49,10 @@ final class AuthCompleteViewController: UIViewController { bind() } + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + self.view.endEditing(true) + } + // MARK: - Init UI private func configNavigationUI() { @@ -60,7 +68,7 @@ final class AuthCompleteViewController: UIViewController { navigationBar.snp.makeConstraints { $0.top.left.right.equalTo(view.safeAreaLayoutGuide) - $0.height.equalTo(44) + $0.height.equalTo(IDSNavigationBar.Metric.navigationHeight) } rootView.snp.makeConstraints { @@ -73,13 +81,16 @@ final class AuthCompleteViewController: UIViewController { private func bind() { rootView.tapCompleteObservable + .throttleOnBackground(.seconds(1)) + .observeOnMain() .withUnretained(self) .subscribe(onNext: { (`self`, _ ) in - let dvc = TabBarController() - guard let window = self.view.window else { - return + self.signUpAPI.signUp(parameter: SignUpRequest(name: AuthModel.name, email: AuthModel.email, password: AuthModel.password)) { data, err in + guard let data = data else { + return + } + self.alert(message: "회원가입 성공") } - window.switchRootViewController(dvc) }) .disposed(by: disposeBag) @@ -88,9 +99,25 @@ final class AuthCompleteViewController: UIViewController { .subscribe(onNext: { (`self`, _ ) in guard let parentVC = self.presentingViewController as? UINavigationController else { return } self.dismiss(animated: true) { - parentVC.popToRootViewController(animated: true) + parentVC.dismiss(animated: true) } }) .disposed(by: disposeBag) } + + func alert(message: String) { + let alertVC = UIAlertController(title: message, message: nil, preferredStyle: .alert) + let okAction = UIAlertAction(title: "확인", style: .default) { action in + print("action") + self.navigationController?.popViewController(animated: true) + } + alertVC.addAction(okAction) + present(alertVC, animated: true) + } +} + +// MARK: - Network + +extension AuthCompleteViewController { + } diff --git a/Instagram/Instagram/Source/Screen/Auth/Controller/UserNameViewController.swift b/Instagram/Instagram/Source/Screen/Auth/Controller/UserNameViewController.swift index 8bd444a..6bd72fd 100644 --- a/Instagram/Instagram/Source/Screen/Auth/Controller/UserNameViewController.swift +++ b/Instagram/Instagram/Source/Screen/Auth/Controller/UserNameViewController.swift @@ -54,7 +54,7 @@ final class UserNameViewController: UIViewController { navigationBar.snp.makeConstraints { $0.top.left.right.equalTo(view.safeAreaLayoutGuide) - $0.height.equalTo(44) + $0.height.equalTo(IDSNavigationBar.Metric.navigationHeight) } rootView.snp.makeConstraints { @@ -70,7 +70,7 @@ final class UserNameViewController: UIViewController { .withUnretained(self) .subscribe(onNext: { (`self`, _ ) in let passwordViewController = UserPasswordViewController() - passwordViewController.userName = self.rootView.textData + AuthModel.email = self.rootView.textData self.navigationController?.pushViewController(passwordViewController, animated: true) }) .disposed(by: disposeBag) diff --git a/Instagram/Instagram/Source/Screen/Auth/Controller/UserPasswordViewController.swift b/Instagram/Instagram/Source/Screen/Auth/Controller/UserPasswordViewController.swift index 9f2a21f..2d1e8b9 100644 --- a/Instagram/Instagram/Source/Screen/Auth/Controller/UserPasswordViewController.swift +++ b/Instagram/Instagram/Source/Screen/Auth/Controller/UserPasswordViewController.swift @@ -58,7 +58,7 @@ final class UserPasswordViewController: UIViewController { navigationBar.snp.makeConstraints { $0.top.left.right.equalTo(view.safeAreaLayoutGuide) - $0.height.equalTo(44) + $0.height.equalTo(IDSNavigationBar.Metric.navigationHeight) } rootView.snp.makeConstraints { @@ -74,7 +74,8 @@ final class UserPasswordViewController: UIViewController { .withUnretained(self) .subscribe(onNext: { (`self`, _ ) in let completeViewController = AuthCompleteViewController() - completeViewController.userName = self.userName + completeViewController.userName = AuthModel.name + AuthModel.password = self.rootView.textData let dvc = UINavigationController(rootViewController: completeViewController) dvc.modalPresentationStyle = .fullScreen diff --git a/Instagram/Instagram/Source/Screen/Auth/Model/AuthModel.swift b/Instagram/Instagram/Source/Screen/Auth/Model/AuthModel.swift index 9216f64..e6bdefb 100644 --- a/Instagram/Instagram/Source/Screen/Auth/Model/AuthModel.swift +++ b/Instagram/Instagram/Source/Screen/Auth/Model/AuthModel.swift @@ -6,3 +6,9 @@ // import Foundation + +internal struct AuthModel { + static var name: String = "" + static var email: String = "" + static var password: String = "" +}