Skip to content

Commit

Permalink
[REFACTOR] LoginViewController MVVM으로 변경 (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffalswo2 committed Nov 12, 2023
1 parent 5266c68 commit 565c160
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@

import UIKit

//protocol LoginViewControllerable where Self: UIViewController {
// var navigator: LoginNavigation { get set }
//}

protocol ViewModel where Self: AnyObject {
associatedtype Input
associatedtype Output

func transform(input: Input) -> Output
}

protocol LoginViewModelPresentable { // ViewModel Property를 설정해준다.
protocol LoginViewModelPresentable {
var navigator: LoginNavigation { get set } // Coordinator

// func setAricleId(_ id: Int) // Factory return type
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import UIKit

struct AuthFactoryImpl: AuthFactory {
func makeLoginViewModel(coordinator: AuthCoordinator) -> LoginViewModelPresentable {

func makeLoginViewModel(coordinator: AuthCoordinator) -> any LoginViewModel & LoginViewModelPresentable {
let adaptor = self.makeAuthAdaptor(coordinator: coordinator)

let apiService = APIService()
let serviceImpl = AuthServiceImpl(apiService: apiService)
let manager = LoginMangerImpl(authService: serviceImpl)
return LoginViewModelImpl(navigator: adaptor, manager: manager)
let managerImpl = LoginMangerImpl(authService: serviceImpl)
return LoginViewModelImpl(navigator: adaptor, manager: managerImpl)
}

func makeAuthAdaptor(coordinator: AuthCoordinator) -> EntireAuthNaviation {
Expand All @@ -27,7 +28,8 @@ struct AuthFactoryImpl: AuthFactory {
}

func makeCompleteOnbardingViewController(coordinator: AuthCoordinator) -> CompleteOnbardingViewControllerable {
let completeViewController = CompleteOnbardingViewController(navigator: self.makeAuthAdaptor(coordinator: coordinator))
let adaptor = self.makeAuthAdaptor(coordinator: coordinator)
let completeViewController = CompleteOnbardingViewController(navigator: adaptor)
return completeViewController
}

Expand All @@ -36,5 +38,5 @@ struct AuthFactoryImpl: AuthFactory {
return onboardingViewController
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import Foundation

protocol AuthFactory {
func makeLoginViewModel(coordinator: AuthCoordinator) -> LoginViewModelPresentable

func makeLoginViewModel(coordinator: AuthCoordinator) -> any LoginViewModel & LoginViewModelPresentable
func makeAuthAdaptor(coordinator: AuthCoordinator) -> EntireAuthNaviation

func makeLoginViewController(coordinator: AuthCoordinator) -> LoginViewController
Expand Down
57 changes: 3 additions & 54 deletions LionHeart-iOS/LionHeart-iOS/Scenes/Login/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ import Combine


final class LoginViewController: UIViewController {
// var userData: UserOnboardingModel?

// var navigator: LoginNavigation
//
// private let manager: LoginManager

// private var kakaoAccessToken: String? {
// didSet {
// guard let kakaoToken = self.kakaoAccessToken else {
// LHToast.show(message: "카카오토큰 언래핑 실패 21")
// return
// }
// self.loginAPI(kakaoToken: kakaoToken)
// }
// }

// MARK: - Properties

Expand All @@ -35,7 +20,7 @@ final class LoginViewController: UIViewController {

private let kakakoLoginButtonTap = PassthroughSubject<Void, Never>()

private let viewModel: any LoginViewModel & LoginViewModelPresentable
private let viewModel: any LoginViewModel

private var cancelBag = Set<AnyCancellable>()

Expand All @@ -54,19 +39,10 @@ final class LoginViewController: UIViewController {
.setBackgroundColor(color: .kakao)

// MARK: - LifeCycle
/*
some이나 any가 없다면...
init에 어떤 객체를 넣어주게 될텐데, 그 객체의 upper bound는 컴파일러 입장에서는 알수가 없다.
즉, LoginViewModel & LoginUseCase 프로토콜들을 채택하고 있는 객체인지 보장할 수가 없다는 뜻이다.

some이나 any를 써주게 된다면
*/

// some이나 any를 associatedtype이 있는 protocol
init(viewModel: any LoginViewModel & LoginViewModelPresentable) {
init(viewModel: some LoginViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)

}

required init?(coder: NSCoder) {
Expand All @@ -90,26 +66,8 @@ final class LoginViewController: UIViewController {
let output = viewModel.transform(input: input)

output.loginSuccess
.sink(receiveCompletion: { completion in
print(completion)
}, receiveValue: { value in
print(value)
})
.sink(receiveValue: { _ in })
.store(in: &cancelBag)


//TODO: - Output binding
// output.loginSuccess
//// .receive(on: DispatchQueue.global())
//// .subscribe(on: DispatchQueue.main)
// .sink { _ in }
// .store(in: &cancelBag)
//
// output.errorStream
// .sink { errorMessage in
// print(errorMessage)
// }
// .store(in: &cancelBag)
}

private func bindInput() {
Expand All @@ -118,15 +76,6 @@ final class LoginViewController: UIViewController {
self?.kakakoLoginButtonTap.send(())
}
.store(in: &cancelBag)

kakakoLoginButtonTap
.sink { a in
print(a)
} receiveValue: { _ in
print("✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅")
}
.store(in: &cancelBag)

}
}

Expand Down

0 comments on commit 565c160

Please sign in to comment.