Skip to content

Commit

Permalink
feat: 무한 스크롤 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
MaraMincho committed Jan 2, 2024
1 parent f27f0b6 commit af62fd6
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 162 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ final class HomeViewController: UIViewController {

private var fetchFeedPublisher: PassthroughSubject<Void, Never> = .init()

private var feedCount: Int = 0

// MARK: UI Components

private let button: UIButton = .init(configuration: .mainEnabled(title: "test button"))
Expand Down Expand Up @@ -81,17 +83,26 @@ private extension HomeViewController {
func setup() {
setCollectionViewDelegate()
setDataSource()
addSection()
setupStyles()
setupHierarchyAndConstraints()
setNavigationItem()
bind()
testCollectionViewDataSource()
fetchFeedPublisher.send()
}

func setCollectionViewDelegate() {
feedListCollectionView.delegate = self
}

func addSection() {
guard var snapshot = dataSource?.snapshot() else {
return
}
snapshot.appendSections([0])
dataSource?.apply(snapshot)
}

func setDataSource() {
dataSource = .init(collectionView: feedListCollectionView) { collectionView, indexPath, item in
let dequeuedCell = collectionView.dequeueReusableCell(withReuseIdentifier: FeedItemCollectionViewCell.identifier, for: indexPath)
Expand Down Expand Up @@ -147,21 +158,11 @@ private extension HomeViewController {
}
var snapshot = dataSource.snapshot()
snapshot.appendItems(item)
dataSource.apply(snapshot)
}

func testCollectionViewDataSource() {
guard let dataSource else {
return
DispatchQueue.main.async {
dataSource.apply(snapshot)
}
var snapshot = dataSource.snapshot()
snapshot.appendSections([0])
let fakeData = fakeData()
let data = try! JSONEncoder().encode(fakeData)
let string = String(data: data, encoding: .utf8)!
Log.make().debug("\(string)")
snapshot.appendItems(fakeData, toSection: 0)
dataSource.apply(snapshot)

feedCount = snapshot.numberOfItems
}

enum Constants {
Expand All @@ -184,144 +185,15 @@ private extension HomeViewController {

return UICollectionViewCompositionalLayout(section: section)
}

func fakeData() -> [FeedElement] {
return [
.init(
ID: 1,
publicID: "",
nickName: "정다함",
publishDate: .now,
profileImage: URL(string: "https://i.ytimg.com/vi/fzzjgBAaWZw/hqdefault.jpg"),
sportText: "달리기",
content: "오운완. 오늘도 운동 조졌음. 기분은 좋네 ^^",
postImages: [
URL(string: "https://cdn.seniordaily.co.kr/news/photo/202108/2444_1812_1557.jpg"),
URL(string: "https://t1.daumcdn.net/thumb/R1280x0/?fname=http://t1.daumcdn.net/brunch/service/guest/image/7MpZeU0-hBKjmb4tKFHR-Skd7bA.JPG"),
URL(string: "https://t1.daumcdn.net/brunch/service/guest/image/9xI2XnpJpggfVZV6l1opHBwyeqU.JPG"),
],
like: 2
),

.init(
ID: 2,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 2
),

.init(
ID: 3,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),

.init(
ID: 4,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),

.init(
ID: 5,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),

.init(
ID: 6,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),

.init(
ID: 7,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),

.init(
ID: 8,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),
]
}
}

// MARK: UICollectionViewDelegate

extension HomeViewController: UICollectionViewDelegate {
func scrollViewDidEndDragging(_: UIScrollView, willDecelerate _: Bool) {
fetchFeedPublisher.send()
func collectionView(_: UICollectionView, willDisplay _: UICollectionViewCell, forItemAt indexPath: IndexPath) {
// 만약 셀이 모자르다면 요청을 보냄
if (feedCount - 1) - indexPath.row < 3 {
fetchFeedPublisher.send()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class HomeViewModel {

private let useCase: HomeUseCaseRepresentable
private var subscriptions: Set<AnyCancellable> = []
var tempID: Int = 0
init(useCase: HomeUseCaseRepresentable) {
self.useCase = useCase
}
Expand All @@ -49,15 +50,151 @@ extension HomeViewModel: HomeViewModelRepresentable {
subscriptions.removeAll()

let fetched: HomeViewModelOutput = input.requestFeedPublisher
.flatMap { [useCase] _ in
useCase.fetchFeed()
// .flatMap { [useCase] _ in
// useCase.fetchFeed()
// }
.map { [weak self] _ in
// TODO: not fakeData _를 feed로 바꿔서 리턴하기 -
return HomeState.fetched(feed: self?.fakeData() ?? [])
}
.map { HomeState.fetched(feed: $0) }
.eraseToAnyPublisher()

let initialState: HomeViewModelOutput = Just(.idle).eraseToAnyPublisher()

return initialState.merge(with: fetched)
.eraseToAnyPublisher()
}

private func fakeData() -> [FeedElement] {
tempID += 8
return [
.init(
ID: 1 + tempID,
publicID: "",
nickName: "정다함",
publishDate: .now,
profileImage: URL(string: "https://i.ytimg.com/vi/fzzjgBAaWZw/hqdefault.jpg"),
sportText: "달리기",
content: "오운완. 오늘도 운동 조졌음. 기분은 좋네 ^^",
postImages: [
URL(string: "https://cdn.seniordaily.co.kr/news/photo/202108/2444_1812_1557.jpg"),
URL(string: "https://t1.daumcdn.net/thumb/R1280x0/?fname=http://t1.daumcdn.net/brunch/service/guest/image/7MpZeU0-hBKjmb4tKFHR-Skd7bA.JPG"),
URL(string: "https://t1.daumcdn.net/brunch/service/guest/image/9xI2XnpJpggfVZV6l1opHBwyeqU.JPG"),
],
like: 2
),

.init(
ID: 2 + tempID,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 2
),

.init(
ID: 3 + tempID,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),

.init(
ID: 4 + tempID,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),

.init(
ID: 5 + tempID,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),

.init(
ID: 6 + tempID,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),

.init(
ID: 7 + tempID,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),

.init(
ID: 8 + tempID,
publicID: "",
nickName: "고양이 애호가",
publishDate: .now,
profileImage: URL(string: "https://ca.slack-edge.com/T05N9HAKPFW-U05PCNTCV9N-8bbbd8736a14-512"),
sportText: "수영",
content: "고양이 애호가입니다. 차린건 없지만 고양이 보고가세요",
postImages: [
URL(string: "https://i.ytimg.com/vi/YCaGYUIfdy4/maxresdefault.jpg")!,
URL(string: "https://www.cats.org.uk/uploads/images/featurebox_sidebar_kids/grief-and-loss.jpg")!,
URL(string: "https://www.telegraph.co.uk/content/dam/pets/2017/01/06/1-JS117202740-yana-two-face-cat-news_trans_NvBQzQNjv4BqJNqHJA5DVIMqgv_1zKR2kxRY9bnFVTp4QZlQjJfe6H0.jpg?imwidth=450")!,
],
like: 4
),
]
}
}

0 comments on commit af62fd6

Please sign in to comment.