From aa1c87240bddce5576155c19f7d77098f8b26bf1 Mon Sep 17 00:00:00 2001 From: kim-seonwoo Date: Fri, 8 Nov 2024 15:19:30 +0900 Subject: [PATCH] =?UTF-8?q?[Refactor/#91]=20LoginViewModel=20=EB=A6=AC?= =?UTF-8?q?=ED=8E=99=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/ViewModels/LoginViewModel.swift | 114 +++--------------- 1 file changed, 20 insertions(+), 94 deletions(-) diff --git a/HMH_Tuist_iOS/Projects/Features/LoginFeature/Sources/ViewModels/LoginViewModel.swift b/HMH_Tuist_iOS/Projects/Features/LoginFeature/Sources/ViewModels/LoginViewModel.swift index 69304827..a2daa9f1 100644 --- a/HMH_Tuist_iOS/Projects/Features/LoginFeature/Sources/ViewModels/LoginViewModel.swift +++ b/HMH_Tuist_iOS/Projects/Features/LoginFeature/Sources/ViewModels/LoginViewModel.swift @@ -1,106 +1,32 @@ -import SwiftUI -import AuthenticationServices +// +// LoginViewModel.swift +// LoginFeature +// +// Created by Seonwoo Kim on 11/8/24. +// Copyright Β© 2024 HMH-iOS. All rights reserved. +// -import KakaoSDKUser +import Foundation import Core -import DSKit +import Domain -public class LoginViewModel: NSObject, ObservableObject { +final class LoginViewModel: ObservableObject { + @Published var loginStatus: LoginResponseType = .loginFailure - @Published public var isLoading: Bool = true - @Published var isPresented: Bool = false - @Published var alertType: CustomAlertType = .unlock + private var cancelBag = CancelBag() - public func handleSplashScreen() { - self.isLoading = false - } + private let loginUseCase: LoginUseCaseType - func handleAppleLogin() { - let request = ASAuthorizationAppleIDProvider().createRequest() - request.requestedScopes = [.fullName, .email] - - let authorizationController = ASAuthorizationController(authorizationRequests: [request]) - authorizationController.delegate = self - authorizationController.performRequests() + init(loginUseCase: LoginUseCaseType) { + self.loginUseCase = loginUseCase } - func handleKakaoLogin() { - if (UserApi.isKakaoTalkLoginAvailable()) { - UserApi.shared.loginWithKakaoTalk {(oauthToken, error) in - if let error = error { - print(error) - } - if let oauthToken = oauthToken{ - let idToken = oauthToken.accessToken - UserManager.shared.socialPlatform = "KAKAO" - UserManager.shared.socialToken = "Bearer " + idToken - self.postSocialLoginData() - } - } - } else { - UserApi.shared.loginWithKakaoAccount {(oauthToken, error) in - if let error = error { - print("πŸ€",error) - } - if let oauthToken = oauthToken{ - print("kakao success") - UserManager.shared.socialPlatform = "KAKAO" - let idToken = oauthToken.accessToken - UserManager.shared.socialToken = "Bearer " + idToken - self.postSocialLoginData() - } + func handleLoginButton(provider: OAuthProviderType) { + loginUseCase.login(provider: provider) + .sink(receiveCompletion: { _ in }) { [weak self] response in + self?.loginStatus = response } - } - } - - //TODO: λ„€νŠΈμ›Œν¬ 뢀뢄은 μ˜μ‘΄μ„± μ •λ¦¬ν•œ 뒀에 λ‹€μ‹œ μ—°κ²°ν•΄λ΄…μ‹œλ‹€ - func postSocialLoginData() { -// let provider = Providers.AuthProvider -// let request = SocialLoginRequestDTO(socialPlatform: UserManager.shared.socialPlatform ?? "") -// -// provider.request(target: .socialLogin(data: request), instance: BaseResponse.self) { data in -// if data.status == 403 { -// UserManager.shared.appStateString = "onboarding" -// } else if data.status == 200 { -// guard let data = data.data else { return } -// UserManager.shared.refreshToken = data.token.refreshToken -// UserManager.shared.accessToken = data.token.accessToken -// UserManager.shared.appStateString = "home" -// } -// } - } -} - -extension LoginViewModel: ASAuthorizationControllerDelegate { - - public func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { - switch authorization.credential { - case let appleIDCredential as ASAuthorizationAppleIDCredential: - let userIdentifier = appleIDCredential.user - let fullName = appleIDCredential.fullName - - if let identityToken = appleIDCredential.identityToken, - let identifyTokenString = String(data: identityToken, encoding: .utf8) { - UserManager.shared.socialToken = identifyTokenString - UserManager.shared.socialPlatform = "APPLE" - self.postSocialLoginData() - } else { - print("Identity token is nil or failed to convert to string.") - } - default: - break - } - } - - func handleAppleIDCredential(_ credential: ASAuthorizationAppleIDCredential) { - let fullName = credential.fullName - let name = (fullName?.familyName ?? "") + (fullName?.givenName ?? "") - UserManager.shared.userName = name - guard let idToken = String(data: credential.identityToken ?? Data(), encoding: .utf8) else { return print("no idToken!!") } - } - - public func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) { - print(error.localizedDescription) + .store(in: cancelBag) } }