Skip to content

Commit

Permalink
[Feat] #243 - ThirdOnboardingView MVVM + Combine 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
yungu0010 committed Mar 14, 2024
1 parent 4e18840 commit 594ad44
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ extension ViewControllerFactoryImpl {
return viewController
}
func makeThirdOnboardingViewController(coordinator: AuthCoordinator) -> ThirdOnboardingViewController {
let viewController = ThirdOnboardingViewController(coordinator: coordinator)
let viewModel = ThirdOnboardingViewModelImpl(coordinator: coordinator)
let viewController = ThirdOnboardingViewController(viewModel: viewModel)
return viewController
}
func makeFourthOnboardingViewController(coordinator: AuthCoordinator) -> FourthOnboardingViewController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import UIKit

import Combine
import SnapKit
import Then

Expand All @@ -22,7 +23,11 @@ final class ThirdOnboardingViewController: UIViewController {
private var dataSource: UICollectionViewDiffableDataSource<Section, ThirdOnboardingModel>! = nil
private lazy var safeArea = self.view.safeAreaLayoutGuide
private var selectList: [String] = []
private weak var coordinator: AuthCoordinator?

private let viewModel: any ThirdOnboardingViewModel
private var cancelBag = Set<AnyCancellable>()

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

// MARK: - UI Components

Expand All @@ -31,8 +36,9 @@ final class ThirdOnboardingViewController: UIViewController {
private var isTapped: Bool = false

// MARK: - init
init(coordinator: AuthCoordinator) {
self.coordinator = coordinator

init(viewModel: some ThirdOnboardingViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
}

Expand All @@ -50,6 +56,7 @@ final class ThirdOnboardingViewController: UIViewController {
setLayout()
setupDataSource()
reloadData()
setBindings()
}
}

Expand All @@ -60,6 +67,7 @@ extension ThirdOnboardingViewController {
collectionView.register(OnboardingCollectionViewCell.self, forCellWithReuseIdentifier: OnboardingCollectionViewCell.identifier)
collectionView.register(OnboardingHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: OnboardingHeaderView.identifier)
}

private func setUI() {
view.backgroundColor = .ntdBlack

Expand Down Expand Up @@ -141,14 +149,19 @@ extension ThirdOnboardingViewController {
section.boundarySupplementaryItems = [header]
return UICollectionViewCompositionalLayout(section: section)
}

private func setBindings() {
let input = ThirdOnboardingViewModelInput(
nextButtonDidTapped: nextButtonDidTapped)
_ = viewModel.transform(input: input)
}
}

extension ThirdOnboardingViewController {
@objc
private func buttonTapped() {

AmplitudeAnalyticsService.shared.send(event: AnalyticsEvent.OnboardingClick.clickOnboardingNext3(select: self.selectList))
self.coordinator?.showFourthOnboardingViewController()
self.nextButtonDidTapped.send()
}
}

Expand All @@ -171,7 +184,6 @@ extension ThirdOnboardingViewController: UICollectionViewDelegate {
}
self.isTapped = false
updateButton(isTapped: self.isTapped)

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@
// Created by 강윤서 on 3/14/24.
//

import Foundation
import Combine

protocol ThirdOnboardingViewModel: ViewModel where Input == ThirdOnboardingViewModelInput, Output == ThirdOnboardingViewModelOutput {}

struct ThirdOnboardingViewModelInput {
let nextButtonDidTapped: PassthroughSubject<Void, Never>
}

struct ThirdOnboardingViewModelOutput {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,25 @@
// Created by 강윤서 on 3/14/24.
//

import Foundation
import Combine

final class ThirdOnboardingViewModelImpl: ThirdOnboardingViewModel {

private weak var coordinator: AuthCoordinator?
private var cancelBag = Set<AnyCancellable>()

init(coordinator: AuthCoordinator) {
self.coordinator = coordinator
}

func transform(input: ThirdOnboardingViewModelInput) -> ThirdOnboardingViewModelOutput {
input.nextButtonDidTapped
.sink { [weak self] _ in
guard let self else { return }
self.coordinator?.showFourthOnboardingViewController()
}
.store(in: &cancelBag)

return ThirdOnboardingViewModelOutput()
}
}

0 comments on commit 594ad44

Please sign in to comment.