Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[홈] 무한 캐러셀 구현 #123

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ final class HomeViewController: UIViewController, BaseViewController {

var viewModel: HomeViewModel?
var disposeBag = DisposeBag()


var datasource: [HomeCapsuleCellItem] = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. datasource 라는 변수명이 CollectionView 의 DataSource 와 헷갈릴 가능성이 있다고 생각합니다!
  2. 해당 데이터는 ViewModel 이 가지고 있는게 자연스럽다고 생각합니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 변경하였습니다!
  2. VM으로 옮겼습니다!


var centerIndex: CGFloat {
return homeView.capsuleCollectionView.contentOffset.x / (FrameResource.homeCapsuleCellWidth * 0.75 + 10)
}
Expand All @@ -30,13 +32,16 @@ final class HomeViewController: UIViewController, BaseViewController {
title = "홈"
configureView()
bind()
homeView.capsuleCollectionView.dataSource = self
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
viewModel?.input.viewWillAppear.onNext(())
}

// MARK: - Functions

private func configureView() {
view.backgroundColor = .themeBackground

Expand All @@ -49,6 +54,12 @@ final class HomeViewController: UIViewController, BaseViewController {
} else {
owner.emptyView = nil
owner.showHomeView()
owner.datasource = capsuleCellItems
owner.homeView.capsuleCollectionView.reloadData()
DispatchQueue.main.async {
Thread.sleep(forTimeInterval: 0.01)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 메인스레드를 재우는 이유가 있나요?!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앱을 처음 실행 시 간혹 캡슐이 짤려서 두줄로 나오는 버그가 발생하였습니다.
저는 이 문제가 컬렉션뷰가 불린 후 초기 화면 설정을 위해 scrollToItem을 실행 시에 발생하는 버그라고 판단하였습니다.
따라서 먼저 컬렉션뷰가 초기에 멀쩡하게 호출된 후에 텀을 두어 scrollToItem 함수가 실행되도록 하고자 하였습니다........
그러나 간헐적으로 버그가 발생하기는 합니다..

owner.homeView.capsuleCollectionView.scrollToItem(at: IndexPath(item: 10000000, section: 0), at: .centeredHorizontally, animated: false)
}
}
}
.disposed(by: disposeBag)
Expand Down Expand Up @@ -88,18 +99,6 @@ final class HomeViewController: UIViewController, BaseViewController {
return
}

viewModel.output.featuredCapsuleCellItems
.bind(to: homeView.capsuleCollectionView.rx.items) { collectionView, index, element in
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: HomeCapsuleCell.identifier,
for: IndexPath(item: index, section: 0)
) as? HomeCapsuleCell else {
return UICollectionViewCell()
}
cell.configure(capsuleCellModel: element)
return cell
}.disposed(by: disposeBag)

viewModel.output.userCapsuleStatus
.subscribe(onNext: { [weak self] status in
self?.homeView.mainStatusLabel.updateUserCapsuleStatus(
Expand Down Expand Up @@ -151,6 +150,7 @@ final class HomeViewController: UIViewController, BaseViewController {
.withUnretained(self)
.subscribe(
onNext: { owner, indexPath in
print(owner.centerIndex)
if owner.getIndexRange(index: indexPath.item) ~= owner.centerIndex {
if let cell = owner.homeView.capsuleCollectionView.cellForItem(at: indexPath) as? HomeCapsuleCell {
guard let uuid = cell.uuid else {
Expand All @@ -164,3 +164,20 @@ final class HomeViewController: UIViewController, BaseViewController {
}).disposed(by: disposeBag)
}
}

extension HomeViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return datasource.isEmpty ? 0 : Int.max
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: HomeCapsuleCell.identifier,
for: indexPath
) as? HomeCapsuleCell else {
return UICollectionViewCell()
}
cell.configure(capsuleCellModel: datasource[indexPath.item % datasource.count])
return cell
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class HomeViewModel: BaseViewModel, CapsuleCellNeedable {
owner.output.featuredCapsuleCellItems.accept(
CapsuleType.allCases
.map { owner.getHomeCapsuleCellItem(capsules: capsuleList, type: $0) }
.compactMap({ $0 })
.compactMap({ $0 }) // + Array(repeating: nil, count: Int.max)
Copy link
Member

@trumanfromkorea trumanfromkorea Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

큰 상관은 없지만 소괄호를 제외해도 될것 같아요

그리고 59번째 라인을 없애고 58번째 라인의 map 메소드를 compactMap 으로 바꿔도 같은 기능을 하지 않을까 싶네요~

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compactMap으로 변경하였습니다!

)

})
Expand Down