From d59e69754190bff699e851d63003ce190cfc168b Mon Sep 17 00:00:00 2001 From: kangho lee Date: Sun, 8 Oct 2023 13:02:52 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20=ED=94=84=EB=A6=AC=EB=B7=B0=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9D=84=20=EC=9C=84=ED=95=B4=20RxDataSource?= =?UTF-8?q?=20->=20DiffableDataSource=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit UserDTO -> UserDomain --- .../Main/Cell/MainCollectionViewCell.swift | 4 +- .../MainCollectionViewItemViewModel.swift | 6 +-- .../Feature/Main/MainViewController.swift | 43 ++++++++++--------- .../Sources/Feature/Main/MainViewModel.swift | 8 +++- .../Sources/Feature/Main/UserSection.swift | 16 +++++++ 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/Falling/Sources/Feature/Main/Cell/MainCollectionViewCell.swift b/Falling/Sources/Feature/Main/Cell/MainCollectionViewCell.swift index c0dc9733..fe703e8c 100644 --- a/Falling/Sources/Feature/Main/Cell/MainCollectionViewCell.swift +++ b/Falling/Sources/Feature/Main/Cell/MainCollectionViewCell.swift @@ -98,8 +98,8 @@ final class MainCollectionViewCell: TFBaseCollectionViewCell { disposeBag = DisposeBag() } - func setup(item: UserDTO) { - viewModel = MainCollectionViewItemViewModel(userDTO: item) + func setup(item: UserDomain) { + viewModel = MainCollectionViewItemViewModel(userDomain: item) } func bindViewModel() { diff --git a/Falling/Sources/Feature/Main/Cell/MainCollectionViewItemViewModel.swift b/Falling/Sources/Feature/Main/Cell/MainCollectionViewItemViewModel.swift index 3ea8f44a..ec2f2d79 100644 --- a/Falling/Sources/Feature/Main/Cell/MainCollectionViewItemViewModel.swift +++ b/Falling/Sources/Feature/Main/Cell/MainCollectionViewItemViewModel.swift @@ -12,10 +12,10 @@ import RxCocoa final class MainCollectionViewItemViewModel: ViewModelType { - let userDTO: UserDTO + let userDomain: UserDomain - init(userDTO: UserDTO) { - self.userDTO = userDTO + init(userDomain: UserDomain) { + self.userDomain = userDomain } enum TimeState { diff --git a/Falling/Sources/Feature/Main/MainViewController.swift b/Falling/Sources/Feature/Main/MainViewController.swift index 961e625e..6ead5ae7 100644 --- a/Falling/Sources/Feature/Main/MainViewController.swift +++ b/Falling/Sources/Feature/Main/MainViewController.swift @@ -15,7 +15,7 @@ final class MainViewController: TFBaseViewController { private let viewModel: MainViewModel private lazy var mainView = MainView() - + private var dataSource: UICollectionViewDiffableDataSource! init(viewModel: MainViewModel) { self.viewModel = viewModel super.init(nibName: nil, bundle: nil) @@ -54,30 +54,32 @@ final class MainViewController: TFBaseViewController { var count = 0 output.userList .drive { userSection in - count = userSection[0].items.count + count = userSection.count }.disposed(by: disposeBag) - - - - let dataSource = RxCollectionViewSectionedAnimatedDataSource { dataSource, collectionView, indexPath, item in - - let cell = collectionView.dequeueReusableCell(for: indexPath, - cellType: MainCollectionViewCell.self) + + // DiffableDataSource + + let profileCellRegistration = UICollectionView.CellRegistration { [weak self] cell, indexPath, item in cell.setup(item: item) - output.currentPage - .do { index in - if index == indexPath.row { - cell.bindViewModel() - } - }.drive() - .disposed(by: self.disposeBag) cell.delegate = self - return cell + output.currentPage + .filter { $0 == indexPath.item } + .drive(onNext: {_ in + cell.bindViewModel() + }) + .disposed(by: cell.disposeBag) } - + + dataSource = UICollectionViewDiffableDataSource(collectionView: mainView.collectionView, cellProvider: { collectionView, indexPath, itemIdentifier in + return collectionView.dequeueConfiguredReusableCell(using: profileCellRegistration, for: indexPath, item: itemIdentifier) + }) output.userList - .drive(mainView.collectionView.rx.items(dataSource: dataSource)) - .disposed(by: self.disposeBag) + .drive(onNext: { [weak self] list in + var snapshot = NSDiffableDataSourceSnapshot() + snapshot.appendSections([.profile]) + snapshot.appendItems(list) + self?.dataSource.apply(snapshot, animatingDifferences: true) + }).disposed(by: disposeBag) output.currentPage .do(onNext: { index in @@ -112,6 +114,7 @@ extension Reactive where Base: MainViewController { return ControlEvent(events: source) } } + #if DEBUG import SwiftUI diff --git a/Falling/Sources/Feature/Main/MainViewModel.swift b/Falling/Sources/Feature/Main/MainViewModel.swift index f1343bfb..fee92c13 100644 --- a/Falling/Sources/Feature/Main/MainViewModel.swift +++ b/Falling/Sources/Feature/Main/MainViewModel.swift @@ -20,7 +20,7 @@ final class MainViewModel: ViewModelType { } struct Output { - let userList: Driver<[UserSection]> + let userList: Driver<[UserDomain]> let currentPage: Driver } @@ -41,7 +41,11 @@ final class MainViewModel: ViewModelType { UserDTO(userIdx: 2), ])] - let userList = Driver.just(userSectionList) + let userList = Driver.just([ + UserDomain(userIdx: 0), + UserDomain(userIdx: 1), + UserDomain(userIdx: 2), + ]) let currentPage = timeOverTrigger.withLatestFrom(currentIndex.asDriver(onErrorJustReturn: 0)) { _, page in currentIndex.onNext(page + 1) diff --git a/Falling/Sources/Feature/Main/UserSection.swift b/Falling/Sources/Feature/Main/UserSection.swift index 870d5065..241339b8 100644 --- a/Falling/Sources/Feature/Main/UserSection.swift +++ b/Falling/Sources/Feature/Main/UserSection.swift @@ -4,9 +4,25 @@ // // Created by SeungMin on 2023/10/06. // +import Foundation import RxDataSources +enum MainProfileSection { + case profile +} + +struct UserDomain: Hashable { + let identifier = UUID() + let userIdx: Int +} + +extension UserDTO { + func toDomain() -> UserDomain { + UserDomain(userIdx: self.userIdx) + } +} + struct UserSection { var header: String var items: [Item]