Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feat/#25-MainApp
Browse files Browse the repository at this point in the history
  • Loading branch information
kim-seonwoo committed May 12, 2024
2 parents 8f6617b + 91c9369 commit 7c4a760
Show file tree
Hide file tree
Showing 32 changed files with 1,122 additions and 1 deletion.
148 changes: 148 additions & 0 deletions HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "3663af6b3771454c566b7793eb68be359aa8d364c59392086cf004cc9b916b1e",
"originHash" : "d3e9876ec49ca5b50bad5ca2775b7dd98532bd02ac823cca0b3be78c33780f04",
"pins" : [
{
"identity" : "alamofire",
Expand Down
65 changes: 65 additions & 0 deletions HMH_iOS/HMH_iOS/Network/Base/AuthInterceptor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// AuthInterceptor.swift
// HMH_iOS
//
// Created by 이지희 on 5/12/24.
//
//

import Foundation

import Alamofire
import Moya

///// 토큰 만료 시 자동으로 refresh를 위한 서버 통신
final class AuthInterceptor: RequestInterceptor {

private var retryLimit = 2
static let shared = AuthInterceptor()

private init() {}

func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (Result<URLRequest, Error>) -> Void) {
print("---adater 진입----")
completion(.success(urlRequest))
}

func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
print("-------🔧retry 시작🔧-------")
guard
let statusCode = request.response?.statusCode,
request.retryCount < retryLimit
else {
print("🚨재시도 횟수가 너무 많습니다🚨")
return completion(.doNotRetry)
}

if let statusCode = request.response?.statusCode,
request.retryCount < retryLimit {
if statusCode == 401 {
let provider = Providers.AuthProvider
provider.request(target: .tokenRefresh, instance: BaseResponse<RefreshTokebResponseDTO>.self) { result in
if result.status == 200 {
if let data = result.data {
// UserManager.shared.updateToken(data.token.accessToken, data.token.refreshToken)
}
print("🪄토큰 재발급에 성공했습니다🪄")
completion(.retry)
} else if statusCode == 401 {
/// 리프레쉬 토큰도 만료된 상황
}
}
}
} else if statusCode == 404 {
/// 유저를 찾을 수 없는 상태
} else {
if request.retryCount > retryLimit {
print("🚨재시도 횟수가 너무 많습니다🚨")
}
completion(.doNotRetryWithError(error))
return
}
}
}


14 changes: 14 additions & 0 deletions HMH_iOS/HMH_iOS/Network/Base/BaseModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// BaseModel.swift
// HMH_iOS
//
// Created by 이지희 on 5/12/24.
//

import Foundation

struct BaseResponse<T: Decodable>: Decodable {
var status: Int
var message: String?
var data: T?
}
32 changes: 32 additions & 0 deletions HMH_iOS/HMH_iOS/Network/Base/BaseTargetType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// BaseTargetType.swift
// HMH_iOS
//
// Created by 이지희 on 5/12/24.
//


import Foundation
import Moya

protocol BaseTargetType: TargetType {}


extension BaseTargetType {
typealias Parameters = [String: String]
var baseURL: URL {
guard let baseURL = URL(string: Config.baseURL) else {
print("🚨🚨BASEURL ERROR🚨🚨")
fatalError()
}
return baseURL
}

var sampleData: Data {
return Data()
}

var validationType: ValidationType {
return .successCodes
}
}
71 changes: 71 additions & 0 deletions HMH_iOS/HMH_iOS/Network/Base/NetworkProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// NetworkProvider.swift
// HMH_iOS
//
// Created by 이지희 on 5/12/24.
//
//

import SwiftUI
import Moya

class NetworkProvider<Provider : TargetType> : MoyaProvider<Provider> {
func request<Model : Codable>(target : Provider, instance : BaseResponse<Model>.Type , viewController: UIViewController, completion : @escaping(BaseResponse<Model>) -> ()){
self.request(target) { result in
switch result {
/// 서버 통신 성공
case .success(let response):
if (200..<300).contains(response.statusCode) ||
response.statusCode == 403 {
if let decodeData = try? JSONDecoder().decode(instance, from: response.data) {
completion(decodeData)
} else{
print("🚨 decoding Error 발생")
}
} else {
print("🚨 Client Error")
}
/// 서버 통신 실패
case .failure(let error):
if let response = error.response {
if let responseData = String(data: response.data, encoding: .utf8) {
print(responseData)
} else {
print(error.localizedDescription)
}
} else {
print(error.localizedDescription)
}
}
}
}

func request<Model : Codable>(target : Provider, instance : BaseResponse<Model>.Type , completion : @escaping(BaseResponse<Model>) -> ()){
self.request(target) { result in
switch result {
/// 서버 통신 성공
case .success(let response):
if (200..<300).contains(response.statusCode) {
if let decodeData = try? JSONDecoder().decode(instance, from: response.data) {
completion(decodeData)
} else{
print("🚨 decoding Error 발생")
}
} else {
print("🚨 Client Error")
}
/// 서버 통신 실패
case .failure(let error):
if let response = error.response {
if let responseData = String(data: response.data, encoding: .utf8) {
print(responseData)
} else {
print(error.localizedDescription)
}
} else {
print(error.localizedDescription)
}
}
}
}
}
12 changes: 12 additions & 0 deletions HMH_iOS/HMH_iOS/Network/DTO/Auth/RefreshTokenResponseDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// RefreshTokenResponseDTO.swift
// HMH_iOS
//
// Created by Seonwoo Kim on 1/15/24.
//

import Foundation

struct RefreshTokebResponseDTO: Codable {
let token: Token
}
31 changes: 31 additions & 0 deletions HMH_iOS/HMH_iOS/Network/DTO/Auth/#RequestDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// #RequestDTO.swift
// HMH_iOS
//
// Created by Seonwoo Kim on 1/14/24.
//

import Foundation

struct #RequestDTO: Codable {
let socialPlatform: String
let name: String
let onboarding: Onboarding
let challenge: Challenge
}

struct Onboarding: Codable {
let averageUseTime: String
let problem: [String]
}

struct Challenge: Codable {
let period: Int
let goalTime: Int
let apps: [Apps]
}

struct Apps: Codable {
let appCode: String
let goalTime: Int
}
13 changes: 13 additions & 0 deletions HMH_iOS/HMH_iOS/Network/DTO/Auth/#ResponseDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// #ResponseDTO.swift
// HMH_iOS
//
// Created by Seonwoo Kim on 1/14/24.
//

import Foundation

struct #ResponseDTO: Codable {
let userId: Int
let token: Token
}
12 changes: 12 additions & 0 deletions HMH_iOS/HMH_iOS/Network/DTO/Auth/SocialLoginRequestDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// SocialLoginRequestDTO.swift
// HMH_iOS
//
// Created by Seonwoo Kim on 1/14/24.
//

import Foundation

struct SocialLoginRequestDTO: Codable {
let socialPlatform: String
}
18 changes: 18 additions & 0 deletions HMH_iOS/HMH_iOS/Network/DTO/Auth/SocialLoginResponseDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// SocialLoginResponseDTO.swift
// HMH_iOS
//
// Created by Seonwoo Kim on 1/14/24.
//

import Foundation

struct SocialLogineResponseDTO: Codable {
let userId: Int
let token: Token
}

struct Token: Codable {
let accessToken: String
let refreshToken: String
}
12 changes: 12 additions & 0 deletions HMH_iOS/HMH_iOS/Network/DTO/Challenge/AddAppRequestDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// AddAppRequestDTO.swift
// HMH_iOS
//
// Created by 지희의 MAC on 1/16/24.
//

import Foundation

struct AddAppRequestDTO: Codable {
let apps: [Apps]
}
12 changes: 12 additions & 0 deletions HMH_iOS/HMH_iOS/Network/DTO/Challenge/AddAppResponseDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// AddAppResponseDTO.swift
// HMH_iOS
//
// Created by 지희의 MAC on 1/16/24.
//

import Foundation

struct AddAppResponseDTO: Codable {
let apps: [Apps]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// CreateChallengeRequestDTO.swift
// HMH_iOS
//
// Created by 지희의 MAC on 1/12/24.
//

import Foundation

struct CreateChallengeRequestDTO: Codable {
let period, goalTime: Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// CreateChallengeResponseDTO.swift
// HMH_iOS
//
// Created by 지희의 MAC on 1/12/24.
//

import Foundation

struct CreateChallengeResponseDTO: Codable {
let challengeID: Int

enum CodingKeys: String, CodingKey {
case challengeID = "challengeId"
}
}
12 changes: 12 additions & 0 deletions HMH_iOS/HMH_iOS/Network/DTO/Challenge/DeleteAppRequestDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// DeleteAppRequestDTO.swift
// HMH_iOS
//
// Created by 지희의 MAC on 1/16/24.
//

import Foundation

struct DeleteAppRequestDTO: Codable {
let appCode: String
}
10 changes: 10 additions & 0 deletions HMH_iOS/HMH_iOS/Network/DTO/Challenge/EmptyResponseDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// EmptyResponseDTO.swift
// HMH_iOS
//
// Created by 지희의 MAC on 1/16/24.
//

import Foundation

struct EmptyResponseDTO: Codable { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// getChallengeResponseDTO.swift
// HMH_iOS
//
// Created by 지희의 MAC on 1/16/24.
//

import Foundation

struct GetChallengeResponseDTO: Codable {
let period: Int
let statuses: [String]
let todayIndex: Int
let goalTime: Int
let apps: [Apps]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// GetHomeChallengeResponseDTO.swift
// HMH_iOS
//
// Created by 김보연 on 1/18/24.
//

import Foundation

struct GetHomeChallengeResponseDTO: Codable {
let status: String
let goalTime: Int
let apps: [Apps]
}
Loading

0 comments on commit 7c4a760

Please # to comment.