Skip to content

Commit

Permalink
[Merge] #55 - BaseURL 설정 및 네트워크 세팅
Browse files Browse the repository at this point in the history
Network [#55] BaseURL 설정 및 네트워크 세팅
  • Loading branch information
Heyjooo authored Jan 14, 2024
2 parents 73be122 + 274dc86 commit 6317b25
Show file tree
Hide file tree
Showing 17 changed files with 327 additions and 64 deletions.
40 changes: 40 additions & 0 deletions DontBe-iOS/DontBe-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
2A2671FF2B4C3AF0009D214F /* Publisher+UIControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2671FE2B4C3AF0009D214F /* Publisher+UIControl.swift */; };
2A2672022B4C3B44009D214F /* ViewModelType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2672012B4C3B44009D214F /* ViewModelType.swift */; };
2A2672052B4C3C00009D214F /* CancelBag.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2672042B4C3C00009D214F /* CancelBag.swift */; };
2A28453E2B531DDE0023F9B5 /* SocialLoginService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A28453D2B531DDE0023F9B5 /* SocialLoginService.swift */; };
2A2845402B531F0A0023F9B5 /* BaseResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A28453F2B531F0A0023F9B5 /* BaseResponse.swift */; };
2A2845432B5320070023F9B5 /* SocialLoginResponseDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2845422B5320070023F9B5 /* SocialLoginResponseDTO.swift */; };
2A2845462B532BCB0023F9B5 /* SocialLoginRequestDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2845452B532BCB0023F9B5 /* SocialLoginRequestDTO.swift */; };
2A31FF572B4F1E0400FEEED9 /* String+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A31FF562B4F1E0400FEEED9 /* String+.swift */; };
2A31FF592B4F3A8B00FEEED9 /* UserInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A31FF582B4F3A8B00FEEED9 /* UserInfo.swift */; };
2A51AE852B4B05AA00FF770A /* SplashViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A51AE842B4B05AA00FF770A /* SplashViewController.swift */; };
Expand Down Expand Up @@ -109,6 +113,10 @@
2A2672012B4C3B44009D214F /* ViewModelType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewModelType.swift; sourceTree = "<group>"; };
2A2672042B4C3C00009D214F /* CancelBag.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CancelBag.swift; sourceTree = "<group>"; };
2A26720D2B4C40CE009D214F /* Config.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = "<group>"; };
2A28453D2B531DDE0023F9B5 /* SocialLoginService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocialLoginService.swift; sourceTree = "<group>"; };
2A28453F2B531F0A0023F9B5 /* BaseResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseResponse.swift; sourceTree = "<group>"; };
2A2845422B5320070023F9B5 /* SocialLoginResponseDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocialLoginResponseDTO.swift; sourceTree = "<group>"; };
2A2845452B532BCB0023F9B5 /* SocialLoginRequestDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocialLoginRequestDTO.swift; sourceTree = "<group>"; };
2A31FF562B4F1E0400FEEED9 /* String+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+.swift"; sourceTree = "<group>"; };
2A31FF582B4F3A8B00FEEED9 /* UserInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserInfo.swift; sourceTree = "<group>"; };
2A51AE842B4B05AA00FF770A /* SplashViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SplashViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -243,6 +251,32 @@
path = Protocol;
sourceTree = "<group>";
};
2A28453C2B531DAD0023F9B5 /* SocialLogin */ = {
isa = PBXGroup;
children = (
2A2845442B5320D60023F9B5 /* Service */,
2A2845412B531FF90023F9B5 /* DTO */,
);
path = SocialLogin;
sourceTree = "<group>";
};
2A2845412B531FF90023F9B5 /* DTO */ = {
isa = PBXGroup;
children = (
2A2845422B5320070023F9B5 /* SocialLoginResponseDTO.swift */,
2A2845452B532BCB0023F9B5 /* SocialLoginRequestDTO.swift */,
);
path = DTO;
sourceTree = "<group>";
};
2A2845442B5320D60023F9B5 /* Service */ = {
isa = PBXGroup;
children = (
2A28453D2B531DDE0023F9B5 /* SocialLoginService.swift */,
);
path = Service;
sourceTree = "<group>";
};
2A51AE832B4B05AA00FF770A /* Splash */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -494,6 +528,7 @@
3C6193022B3A773100220CEB /* Network */ = {
isa = PBXGroup;
children = (
2A28453C2B531DAD0023F9B5 /* SocialLogin */,
3C61930E2B3A787000220CEB /* Foundation */,
);
path = Network;
Expand Down Expand Up @@ -572,6 +607,7 @@
children = (
3C6193182B3A7AC700220CEB /* Config.swift */,
3C61931A2B3A7AD000220CEB /* NetworkError.swift */,
2A28453F2B531F0A0023F9B5 /* BaseResponse.swift */,
);
path = Foundation;
sourceTree = "<group>";
Expand Down Expand Up @@ -813,6 +849,7 @@
3CEE4CBD2B500A7800F506AF /* DontBeTransparencyInfoView.swift in Sources */,
2F8735422B4BE66500E55552 /* HomeViewController.swift in Sources */,
2A8D70B42B4C999F009F4C6C /* CustomButton.swift in Sources */,
2A2845432B5320070023F9B5 /* SocialLoginResponseDTO.swift in Sources */,
3C61930A2B3A781300220CEB /* ImageLiterals.swift in Sources */,
2A8D70C52B4D8079009F4C6C /* UIViewController+.swift in Sources */,
2A6D54C12B479B4300F9891E /* adjusted+.swift in Sources */,
Expand Down Expand Up @@ -866,6 +903,7 @@
2A2671FD2B4C3A9F009D214F /* LoginViewModel.swift in Sources */,
2FB818DC2B51875D00B7498F /* PostReplyCollectionViewCell.swift in Sources */,
3C6192EF2B3A719A00220CEB /* SceneDelegate.swift in Sources */,
2A2845462B532BCB0023F9B5 /* SocialLoginRequestDTO.swift in Sources */,
2AF069B42B5194F300CA3E48 /* MyPageIntroductionEditView.swift in Sources */,
2A2672052B4C3C00009D214F /* CancelBag.swift in Sources */,
2FB818D92B5186FC00B7498F /* PostReplyCollectionView.swift in Sources */,
Expand All @@ -879,12 +917,14 @@
3CF184CB2B4EEC0B00816D5F /* MyPageViewController.swift in Sources */,
3C35565B2B494F0A0016BA49 /* UIColor+.swift in Sources */,
2AF069AE2B514B0100CA3E48 /* NotificationEmptyViewCell.swift in Sources */,
2A28453E2B531DDE0023F9B5 /* SocialLoginService.swift in Sources */,
2AC9FB1B2B4DE77400D31071 /* AgreementListCustomView.swift in Sources */,
2F17418A2B500CC20089FC4D /* HomeBottomsheetView.swift in Sources */,
3C01692A2B4DC82D0075334B /* DontBePopupView.swift in Sources */,
2A2671FF2B4C3AF0009D214F /* Publisher+UIControl.swift in Sources */,
3C4993672B4F2644002A99CF /* MyPageContentViewController.swift in Sources */,
3CEE4CC22B503F9E00F506AF /* DontBeCustomInfoView.swift in Sources */,
2A2845402B531F0A0023F9B5 /* BaseResponse.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion DontBe-iOS/DontBe-iOS/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
NSAttributedString.Key.foregroundColor: UIColor.donBlack
]

KakaoSDK.initSDK(appKey: Bundle.main.object(forInfoDictionaryKey: Config.Keys.Plist.nativeAppKey) as? String ?? "")
KakaoSDK.initSDK(appKey: Config.nativeAppKey)
return true
}

Expand Down
8 changes: 3 additions & 5 deletions DontBe-iOS/DontBe-iOS/Application/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
self.window?.rootViewController = SplashViewController()
self.window?.makeKeyAndVisible()

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2.0) {
// let navigationController = UINavigationController(rootViewController: LoginViewController(viewModel: LoginViewModel()))
// let navigationController = UINavigationController(rootViewController: DontBeTabBarController())
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0) {
if loadUserData()?.isSocialLogined == true && loadUserData()?.isJoinedApp == true && loadUserData()?.isOnboardingFinished == true {
let navigationController = UINavigationController(rootViewController: DontBeTabBarController())
self.window?.rootViewController = navigationController
} else if loadUserData()?.isJoinedApp == false {
let navigationController = UINavigationController(rootViewController: LoginViewController(viewModel: LoginViewModel()))
let navigationController = UINavigationController(rootViewController: LoginViewController(viewModel: LoginViewModel(networkProvider: SocialLoginService())))
self.window?.rootViewController = navigationController
} else if loadUserData()?.isOnboardingFinished == false {
let navigationController = UINavigationController(rootViewController: OnboardingViewController())
self.window?.rootViewController = navigationController
} else {
let navigationController = UINavigationController(rootViewController: LoginViewController(viewModel: LoginViewModel()))
let navigationController = UINavigationController(rootViewController: LoginViewController(viewModel: LoginViewModel(networkProvider: SocialLoginService())))
self.window?.rootViewController = navigationController
}
self.window?.makeKeyAndVisible()
Expand Down
7 changes: 7 additions & 0 deletions DontBe-iOS/DontBe-iOS/Global/Resources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>BASE_URL</key>
<string>$(BASE_URL)</string>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>LSApplicationQueriesSchemes</key>
Expand Down
15 changes: 15 additions & 0 deletions DontBe-iOS/DontBe-iOS/Network/Foundation/BaseResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// BaseResponse.swift
// DontBe-iOS
//
// Created by 변희주 on 1/14/24.
//

import Foundation

struct BaseResponse<T: Decodable>: Decodable {
let status: Int
let success: Bool
let message: String
let data: T?
}
17 changes: 17 additions & 0 deletions DontBe-iOS/DontBe-iOS/Network/Foundation/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum Config {
enum Keys {
enum Plist {
static let nativeAppKey = "NATIVE_APP_KEY"
static let baseURL = "BASE_URL"
}
}

Expand All @@ -21,3 +22,19 @@ enum Config {
return dictionary
}()
}

extension Config {
static let nativeAppKey: String = {
guard let key = Config.infoDictionary[Keys.Plist.nativeAppKey] as? String else {
fatalError("Base URL is not set in plist for this configuration")
}
return key
}()

static let baseURL: String = {
guard let key = Config.infoDictionary[Keys.Plist.baseURL] as? String else {
fatalError("Base URL is not set in plist for this configuration")
}
return key
}()
}
19 changes: 18 additions & 1 deletion DontBe-iOS/DontBe-iOS/Network/Foundation/NetworkError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@

import UIKit

enum NetworkError {
enum NetworkError: Int, Error, CustomStringConvertible {
var description: String { self.errorDescription }
case responseError
case badRequestError = 400
case unautohorizedError = 401
case notFoundError = 404
case internalServerError = 500
case unknownError

var errorDescription: String {
switch self {
case .responseError: return "REQUEST_ERROR"
case .badRequestError: return "BAD_REQUEST_ERROR"
case .unautohorizedError: return "UNAUTHORIZED_ERROR"
case .notFoundError: return "NOT_FOUND_ERROR"
case .internalServerError: return "SERVER_ERROR"
case .unknownError: return "UNKNOWN_ERROR"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// SocialLoginRequestDTO.swift
// DontBe-iOS
//
// Created by 변희주 on 1/14/24.
//

import Foundation

struct SocialLoginRequestDTO: Encodable {
let socialPlatform: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// SocialLoginResponseDTO.swift
// DontBe-iOS
//
// Created by 변희주 on 1/14/24.
//

import Foundation

// MARK: - SocilLoginResponseDTO
struct SocialLoginResponseDTO: Decodable {
let nickName: String
let memberId: Int
let accessToken, refreshToken: String
let memberProfileUrl: String
let isNewUser: Bool
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// KakaoLoginService.swift
// DontBe-iOS
//
// Created by 변희주 on 1/14/24.
//

import Foundation
import Foundation

protocol SocialLoginServiceType {
func makeRequestURL(accessToken: String) -> URLRequest
func postData(accessToken: String) async throws -> BaseResponse<SocialLoginResponseDTO>?
func parseData(data: Data) -> BaseResponse<SocialLoginResponseDTO>?
}

final class SocialLoginService: SocialLoginServiceType {

func makeRequestURL(accessToken: String) -> URLRequest {

let baseURL = Config.baseURL

let urlString = "\(baseURL)/auth"

// URL 생성
guard let url = URL(string: urlString) else {
fatalError("Failed to create URL")
}

// URLRequest 생성
var request = URLRequest(url: url)
request.httpMethod = "POST"

// 헤더 추가
let header = ["Content-Type": "application/json",
"Authorization": "Bearer \(accessToken)"]
header.forEach {
request.addValue($0.value, forHTTPHeaderField: $0.key)
}

// 리퀘스트 바디 설정
let requestBody = SocialLoginRequestDTO(socialPlatform: "KAKAO")
do {
let jsonData = try JSONEncoder().encode(requestBody)
request.httpBody = jsonData
} catch {
print("Failed to encode request body: \(error)")
}

return request
}

func postData(accessToken: String) async throws -> BaseResponse<SocialLoginResponseDTO>? {
do {
let request = self.makeRequestURL(accessToken: accessToken)
let (data, response) = try await URLSession.shared.data(for: request)

guard let httpResponse = response as? HTTPURLResponse else {
throw NetworkError.responseError
}

switch httpResponse.statusCode {
case 200..<300:
return parseData(data: data)
case 400:
throw NetworkError.badRequestError
case 401:
throw NetworkError.unautohorizedError
case 404:
throw NetworkError.notFoundError
case 500:
throw NetworkError.internalServerError
default:
throw NetworkError.unknownError
}
} catch {
throw error
}
}

func parseData(data: Data) -> BaseResponse<SocialLoginResponseDTO>? {
do {
let jsonDecoder = JSONDecoder()
let result = try jsonDecoder.decode(BaseResponse<SocialLoginResponseDTO>.self, from: data)
return result
} catch {
print(error)
return nil
}
}
}
1 change: 0 additions & 1 deletion DontBe-iOS/DontBe-iOS/Presentation/Helpers/UserInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Foundation
struct UserInfo: Codable {
let isSocialLogined: Bool
let isJoinedApp: Bool
let isNotFirstUser: Bool
let isOnboardingFinished: Bool
let userNickname: String
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ extension JoinProfileViewController {
} else {
saveUserData(UserInfo(isSocialLogined: true,
isJoinedApp: true,
isNotFirstUser: false,
isOnboardingFinished: false,
userNickname: self.originView.nickNameTextField.text ?? ""))

let viewContoller = OnboardingViewController()
viewContoller.originView.isFirstUser = true
self.navigationBackButton.removeFromSuperview()
self.navigationController?.pushViewController(viewContoller, animated: true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,18 @@ extension LoginViewController {
let output = self.viewModel.transform(from: input, cancelBag: self.cancelBag)

output.userInfoPublisher
// .receive(on: RunLoop.main)
.sink { userInfo in
print(userInfo)
// 서버통신 -> 첫 로그인 유저면 여기
let viewController = JoinAgreementViewController(viewModel: JoinAgreeViewModel())
saveUserData(UserInfo(isSocialLogined: true,
isJoinedApp: false,
isNotFirstUser: false,
isOnboardingFinished: false,
userNickname: ""))
// 서버통신 -> 이미 로그인 유저면
// let viewController = OnboardingViewController()
// saveUserData(UserInfo(isSocialLogined: true,
// isJoinedApp: true,
// isNotFirstUser: true,
// isOnboardingFinished: false,
// userNickname: ""))
self.navigationController?.pushViewController(viewController, animated: true)
.receive(on: RunLoop.main)
.sink { isFirstUser in
if isFirstUser {
// 첫 로그인 유저면 여기
let viewController = JoinAgreementViewController(viewModel: JoinAgreeViewModel())
self.navigationController?.pushViewController(viewController, animated: true)
} else {
// 이미 가입한 유저면 여기
let viewController = OnboardingViewController()
viewController.originView.isFirstUser = false
self.navigationController?.pushViewController(viewController, animated: true)
}
}
.store(in: self.cancelBag)
}
Expand Down
Loading

0 comments on commit 6317b25

Please sign in to comment.