Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACT :: [#344] auth 리펙토링 #345

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Projects/Core/Sources/Utils/UIImage+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@ public extension UIImage {
return UIGraphicsGetImageFromCurrentImageContext()
}
}

public extension UIImageView {
func imageFrom(url:URL) {
DispatchQueue.global().async { [weak self] in
if let data = try? Data(contentsOf: url) {
if let image = UIImage(data:data) {
DispatchQueue.main.async {
self?.image = image
}
}
}
}
}
}
6 changes: 4 additions & 2 deletions Projects/Data/Sources/Repository/PostureRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ public class PostureRepository: PostureRepositoryInterface {

private let networkService: PostureService

public func getRecommandData() -> Single<[PostureRecommandModel]> {
return networkService.requestRecommandData()
public func getRecommandData(accessToken: String) -> Single<PoseRecommandModel> {
return networkService.requestRecommandData(accessToken: accessToken)
.map(PoseRecommandDTO.self)
.map { $0.toDomain() }
}

public func getPartData(type: PosturePartType) -> Single<PosturePartModel> {
Expand Down
53 changes: 38 additions & 15 deletions Projects/Domain/Sources/Model/Posture/PostureRecommandModel.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
import UIKit

public struct PostureRecommandModel {
public var titleImage: UIImage
public var titleText: String
public var exerciseData: [PostureRecommandExerciseModel]
public struct PoseRecommandModel {
public var poses: PoseRecommandPartModel

public init(poses: PoseRecommandPartModel) {
self.poses = poses
}
}

public init(titleImage: UIImage, titleText: String, exerciseData: [PostureRecommandExerciseModel]) {
self.titleImage = titleImage
self.titleText = titleText
self.exerciseData = exerciseData
public struct PoseRecommandPartModel {
public var 어깨, 복근, 등, 가슴, 팔: PoseRecommandPartResponseModel

public init(어깨: PoseRecommandPartResponseModel, 복근: PoseRecommandPartResponseModel, 등: PoseRecommandPartResponseModel, 가슴: PoseRecommandPartResponseModel, 팔: PoseRecommandPartResponseModel) {
self.어깨 = 어깨
self.복근 = 복근
self.등 = 등
self.가슴 = 가슴
self.팔 = 팔
}
}

public struct PostureRecommandExerciseModel {
public var image: UIImage
public var name: String
public var part: String
public struct PoseRecommandPartResponseModel {
public var responses: [PoseRecommandResponseModel]

public init(responses: [PoseRecommandResponseModel]) {
self.responses = responses
}
}

public init(image: UIImage, name: String, part: String) {
self.image = image
public struct PoseRecommandResponseModel {
public var id: Int
public var category: [String]
public var needMachine: Bool
public var name: String
public var simplePart, exactPart: [String]
public var thumbnail: String

public init(id: Int, category: [String], needMachine: Bool, name: String, simplePart: [String], exactPart: [String], thumbnail: String) {
self.id = id
self.category = category
self.needMachine = needMachine
self.name = name
self.part = part
self.simplePart = simplePart
self.exactPart = exactPart
self.thumbnail = thumbnail
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public enum PosturePartType {
}

public protocol PostureRepositoryInterface {
func getRecommandData() -> Single<[PostureRecommandModel]>
func getRecommandData(accessToken: String) -> Single<PoseRecommandModel>
func getPartData(type: PosturePartType) -> Single<PosturePartModel>
func getDetailData(accessToken: String, id: Int) -> Single<PostureDetailModel>
func getSearchData() -> Single<PostureSearchModel>
Expand Down
65 changes: 46 additions & 19 deletions Projects/Domain/Sources/Response/PoseDTO.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation

import DSKit
import UIKit
//import DSKit

public struct PostureAllDTO: Decodable {
public let responses: [PostureAllResponse]
Expand Down Expand Up @@ -68,33 +69,33 @@ public struct PoseDetailDTO: Decodable {
public extension PoseDetailDTO {
func toDomain() -> PostureDetailModel {
return .init(needMachine: needMachine, category: category, simpleName: simpleName, exactName: exactName, thumbnail: thumbnail, video: video, simplePart: simplePart, exactPart: exactPart, startPose: startPose, exerciseWay: exerciseWay, breatheWay: breatheWay, caution: caution, pickleImage: [
DSKitAsset.Assets.posturePickleTest1.image,
DSKitAsset.Assets.posturePickleTest2.image,
DSKitAsset.Assets.posturePickleTest3.image,
DSKitAsset.Assets.posturePickleTest4.image
UIImage(),
UIImage(),
UIImage(),
UIImage()
])
}
}

struct PoseRecommandDTO: Decodable {
let poses: PoseRecommandPart
public struct PoseRecommandDTO: Decodable {
public let poses: PoseRecommandPart
}

struct PoseRecommandPart: Decodable {
let 어깨, 복근, 등, 가슴, 팔: PoseRecommandPartResponse
public struct PoseRecommandPart: Decodable {
public let 어깨, 복근, 등, 가슴, 팔: PoseRecommandPartResponse
}

struct PoseRecommandPartResponse: Decodable {
let responses: [PoseRecommandResponse]
public struct PoseRecommandPartResponse: Decodable {
public let responses: [PoseRecommandResponse]
}

struct PoseRecommandResponse: Decodable {
let id: Int
let category: [String]
let needMachine: Bool
let name: String
let simplePart, exactPart: [String]
let thumbnail: String
public struct PoseRecommandResponse: Decodable {
public let id: Int
public let category: [String]
public let needMachine: Bool
public let name: String
public let simplePart, exactPart: [String]
public let thumbnail: String

enum CodingKeys: String, CodingKey {
case id, category
Expand All @@ -107,5 +108,31 @@ struct PoseRecommandResponse: Decodable {
}

extension PoseRecommandDTO {

public func toDomain() -> PoseRecommandModel {
return .init(poses: poses.toDomain())
}
}

extension PoseRecommandPart {
public func toDomain() -> PoseRecommandPartModel {
return .init(어깨: 어깨.toDomain(), 복근: 복근.toDomain(), 등: 등.toDomain(), 가슴: 가슴.toDomain(), 팔: 팔.toDomain())
}
}

extension PoseRecommandPartResponse {
public func toDomain() -> PoseRecommandPartResponseModel {
return .init(responses: responses.toDomain())
}
}

extension PoseRecommandResponse {
public func toDomain() -> PoseRecommandResponseModel {
return .init(id: id, category: category, needMachine: needMachine, name: name, simplePart: simplePart, exactPart: exactPart, thumbnail: thumbnail)
}
}

extension Array where Element == PoseRecommandResponse {
public func toDomain() -> [PoseRecommandResponseModel] {
return self.map { $0.toDomain() }
}
}
118 changes: 8 additions & 110 deletions Projects/Domain/Sources/UseCase/AuthUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public protocol AuthUseCase {
func nextButtonTap() -> Bool
func getIntroData()
func tokenReIssue()
func completeButtonTap()
var appleSignupResult: PublishSubject<String> { get }
var introData: PublishSubject<IntroModel> { get }
}
Expand All @@ -38,7 +39,7 @@ public class DefaultAuthUseCase {
}

extension DefaultAuthUseCase: AuthUseCase {

public func changeNickname(nickname: String) {
self.nicknameText = nickname
}
Expand Down Expand Up @@ -90,23 +91,7 @@ extension DefaultAuthUseCase: AuthUseCase {

public func tokenReIssue() {
let refreshToken = TokenManagerImpl().get(key: .refreshToken)
guard let refreshToken = refreshToken else {
AuthStepper.shared.steps.accept(MGStep.authIntroIsRequired)
return
}
authRepository.tokenReIssue(refreshToken: refreshToken)
.flatMap { response -> Single<Response> in
switch response.statusCode {
case 200:
return Single.just(response)
case 401:
return Single.error(AuthErrorType.error401)
case 500:
return Single.error(AuthErrorType.error500)
default:
return Single.just(response)
}
}
authRepository.tokenReIssue(refreshToken: refreshToken ?? "토큰 없음")
.subscribe(onSuccess: { element in
MGLogger.debug("token ReIssue ✅ \(String(describing: element.response))")
if let headers = element.response?.headers {
Expand All @@ -125,27 +110,16 @@ extension DefaultAuthUseCase: AuthUseCase {
AuthStepper.shared.steps.accept(MGStep.authIntroIsRequired)
}).disposed(by: disposeBag)
}

public func completeButtonTap() {
MGLogger.debug("회원가입 완료 버튼 클릭함")
AuthStepper.shared.steps.accept(MGStep.initialization)
}
}

extension DefaultAuthUseCase {
private func oauthButtonTap(oauthToken: String) {
authRepository.oauthLogin(accessToken: oauthToken, oauth: oauthType)
.flatMap { response -> Single<Response> in
switch response.statusCode {
case 200:
return Single.just(response)
case 400:
return Single.error(AuthErrorType.error400)
case 401:
return Single.error(AuthErrorType.error401)
case 404:
return Single.error(AuthErrorType.error404)
case 500:
return Single.error(AuthErrorType.error500)
default:
return Single.just(response)
}
}
.subscribe(onSuccess: { element in
MGLogger.debug("appleButtonTap login ✅ \(String(describing: element.response))")
if let headers = element.response?.headers {
Expand All @@ -162,41 +136,9 @@ extension DefaultAuthUseCase {
}, onFailure: { [self] error in
MGLogger.debug("appleButtonTap login ❌ \(error)")
authRepository.oauthRecovery(accessToken: oauthToken, oauth: oauthType)
.flatMap { response -> Single<Response> in
switch response.statusCode {
case 200:
return Single.just(response)
case 400:
return Single.error(AuthErrorType.error400)
case 401:
return Single.error(AuthErrorType.error401)
case 404:
return Single.error(AuthErrorType.error404)
case 500:
return Single.error(AuthErrorType.error500)
default:
return Single.just(response)
}
}
.subscribe(onSuccess: { [self] element in
MGLogger.debug("appleButtonTap recovery ✅ \(element)")
authRepository.oauthLogin(accessToken: oauthToken, oauth: oauthType)
.flatMap { response -> Single<Response> in
switch response.statusCode {
case 200:
return Single.just(response)
case 400:
return Single.error(AuthErrorType.error400)
case 401:
return Single.error(AuthErrorType.error401)
case 404:
return Single.error(AuthErrorType.error404)
case 500:
return Single.error(AuthErrorType.error500)
default:
return Single.just(response)
}
}
.subscribe(onSuccess: { element in
MGLogger.debug("appleButtonTap login ✅ \(String(describing: element.response))")
if let headers = element.response?.headers {
Expand All @@ -222,18 +164,6 @@ extension DefaultAuthUseCase {
private func nicknameButtonTap(oauthToken: String) -> Bool {
var nicknameState: Bool = false
authRepository.nicknameCheck(nickname: nicknameText)
.flatMap { response -> Single<Response> in
switch response.statusCode {
case 200:
return Single.just(response)
case 400:
return Single.error(AuthErrorType.error400)
case 500:
return Single.error(AuthErrorType.error500)
default:
return Single.just(response)
}
}
.subscribe(onSuccess: { [self] element in
do {
if try element.map(Bool.self) == true {
Expand All @@ -249,41 +179,9 @@ extension DefaultAuthUseCase {
authRepository.oauthSignup(nickname: nicknameText,
accessToken: oauthToken,
oauth: oauthType)
.flatMap { response -> Single<Response> in
switch response.statusCode {
case 201:
return Single.just(response)
case 400:
return Single.error(AuthErrorType.error400)
case 401:
return Single.error(AuthErrorType.error401)
case 409:
return Single.error(AuthErrorType.error409)
case 500:
return Single.error(AuthErrorType.error500)
default:
return Single.just(response)
}
}
.subscribe(onSuccess: { [self] element in
MGLogger.debug("nicknameButtonTap Signup ✅ \(element)")
authRepository.oauthLogin(accessToken: oauthToken, oauth: oauthType)
.flatMap { response -> Single<Response> in
switch response.statusCode {
case 200:
return Single.just(response)
case 400:
return Single.error(AuthErrorType.error400)
case 401:
return Single.error(AuthErrorType.error401)
case 404:
return Single.error(AuthErrorType.error404)
case 500:
return Single.error(AuthErrorType.error500)
default:
return Single.just(response)
}
}
.subscribe(onSuccess: { element in
MGLogger.debug("nicknameButtonTap Login ✅ \(element)")
if let headers = element.response?.headers {
Expand Down
Loading
Loading