-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
041a82d
commit 1f3b9f4
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...cts/Features/LoginFeature/Sources/Refactor_LoginFeature_Combine/Domain/LoginUseCase.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// LoginUseCase.swift | ||
// LoginFeatureInterface | ||
// | ||
// Created by Seonwoo Kim on 10/9/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import Core | ||
import Domain | ||
|
||
public enum LoginResponseType { | ||
case loginSuccess | ||
case loginFailure | ||
case noUserInfo | ||
} | ||
|
||
protocol LoginUseCaseType { | ||
func requestLogin(platform: String, socialToken: String) -> AnyPublisher<SocialLogineResponseDTO, Error> | ||
var loginResponse: CurrentValueSubject<LoginResponseType, Error> { get set } | ||
} | ||
|
||
final class LoginUseCase: LoginUseCaseType { | ||
|
||
public var loginResponse = CurrentValueSubject<LoginResponseType, Error>(.loginFailure) | ||
|
||
|
||
private var container: DIContainer | ||
private var cancelBag = CancelBag() | ||
|
||
init(container: DIContainer) { | ||
self.container = container | ||
} | ||
|
||
func requestLogin(platform: String, socialToken: String) -> AnyPublisher<Domain.SocialLogineResponseDTO, any Error> { | ||
return container.services.userService.signIn(platform: platform) | ||
.handleEvents(receiveOutput: { response in | ||
UserManager.shared.socialToken = socialToken | ||
UserManager.shared.accessToken = response.data | ||
UserManager.shared.refreshToken = response.data | ||
}) | ||
.map { response in | ||
handleLoginResponse(statusCode: response.statusCode) | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
private func handleLoginResponse(statusCode: Int) -> LoginResponseType { | ||
switch statusCode { | ||
case 200..<300: | ||
self.loginResponse.send(.loginSuccess) | ||
return .loginSuccess | ||
case 403: | ||
self.loginResponse.send(.noUserInfo) | ||
return .noUserInfo | ||
default: | ||
self.loginResponse.send(.loginFailure) | ||
return .loginFailure | ||
} | ||
} | ||
} | ||
|