Skip to content

Commit

Permalink
Refactor: 프리뷰 사용을 위해 RxDataSource -> DiffableDataSource로 변경
Browse files Browse the repository at this point in the history
UserDTO -> UserDomain
  • Loading branch information
ibcylon committed Oct 8, 2023
1 parent 2305ef0 commit d59e697
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 23 additions & 20 deletions Falling/Sources/Feature/Main/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class MainViewController: TFBaseViewController {

private let viewModel: MainViewModel
private lazy var mainView = MainView()

private var dataSource: UICollectionViewDiffableDataSource<MainProfileSection, UserDomain>!
init(viewModel: MainViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
Expand Down Expand Up @@ -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<UserSection> { dataSource, collectionView, indexPath, item in

let cell = collectionView.dequeueReusableCell(for: indexPath,
cellType: MainCollectionViewCell.self)

// DiffableDataSource

let profileCellRegistration = UICollectionView.CellRegistration<MainCollectionViewCell, UserDomain> { [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<MainProfileSection, UserDomain>()
snapshot.appendSections([.profile])
snapshot.appendItems(list)
self?.dataSource.apply(snapshot, animatingDifferences: true)
}).disposed(by: disposeBag)

output.currentPage
.do(onNext: { index in
Expand Down Expand Up @@ -112,6 +114,7 @@ extension Reactive where Base: MainViewController {
return ControlEvent(events: source)
}
}

#if DEBUG
import SwiftUI

Expand Down
8 changes: 6 additions & 2 deletions Falling/Sources/Feature/Main/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class MainViewModel: ViewModelType {
}

struct Output {
let userList: Driver<[UserSection]>
let userList: Driver<[UserDomain]>
let currentPage: Driver<Int>
}

Expand All @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions Falling/Sources/Feature/Main/UserSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit d59e697

Please sign in to comment.