Skip to content

Commit

Permalink
[Feat] #243 - secondOnboardingView MVVM + Combine 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
yungu0010 committed Mar 14, 2024
1 parent 7d287ba commit dc43e1f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ extension ViewControllerFactoryImpl {
return viewController
}
func makeSecondOnboardingViewController(coordinator: AuthCoordinator) -> SecondOnboardingViewController {
let viewController = SecondOnboardingViewController(coordinator: coordinator)
let viewModel = SecondOnboardingViewModelImpl(coordinator: coordinator)
let viewController = SecondOnboardingViewController(viewModel: viewModel)
return viewController
}
func makeThirdOnboardingViewController(coordinator: AuthCoordinator) -> ThirdOnboardingViewController {
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 @@ -20,16 +21,20 @@ final class SecondOnboardingViewController: UIViewController {
private let onboardingModel: [SecondOnboardingModel] = SecondOnboardingModel.titles
private var dataSource: UICollectionViewDiffableDataSource<Section, SecondOnboardingModel>! = nil
private lazy var safeArea = self.view.safeAreaLayoutGuide
private weak var coordinator: AuthCoordinator?

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

private let onbaordingCellTapped = PassthroughSubject<IndexPath, Never>()

// MARK: - UI Components

private lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout())

// MARK: - init

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

Expand All @@ -47,6 +52,7 @@ final class SecondOnboardingViewController: UIViewController {
setLayout()
setupDataSource()
reloadData()
setBindings()
}
}

Expand Down Expand Up @@ -113,12 +119,18 @@ extension SecondOnboardingViewController {
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}

private func setBindings() {
let input = SecondOnboardingViewModelInput(
cellTapped: onbaordingCellTapped)
_ = viewModel.transform(input: input)
}
}

extension SecondOnboardingViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
AmplitudeAnalyticsService.shared.send(event: AnalyticsEvent.OnboardingClick.clickOnboardingNext2(select: SecondOnboardingModel.titles[indexPath.row].title))

coordinator?.showThirdOnboardingViewController()
self.onbaordingCellTapped.send(indexPath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@
// Created by 강윤서 on 3/14/24.
//

import Combine
import Foundation

protocol SecondOnboardingViewModel: ViewModel where Input == SecondOnboardingViewModelInput, Output == SecondOnboardingViewModelOutput { }

struct SecondOnboardingViewModelInput {
let cellTapped: PassthroughSubject<IndexPath, Never>
}

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

import Foundation
import Combine

final class SecondOnboardingViewModelImpl: SecondOnboardingViewModel {

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

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

func transform(input: SecondOnboardingViewModelInput) -> SecondOnboardingViewModelOutput {
input.cellTapped
.sink { [weak self] _ in
guard let self else { return }
self.coordinator?.showThirdOnboardingViewController()
}
.store(in: &cancelBag)
return SecondOnboardingViewModelOutput()
}
}

0 comments on commit dc43e1f

Please sign in to comment.