diff --git a/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewController/HomeViewController+UICollectionViewDelegate.swift b/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewController/HomeViewController+UICollectionViewDelegate.swift deleted file mode 100644 index 39316038..00000000 --- a/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewController/HomeViewController+UICollectionViewDelegate.swift +++ /dev/null @@ -1,10 +0,0 @@ -// -// HomeViewController+UICollectionViewDelegate.swift -// HomeFeature -// -// Created by MaraMincho on 1/2/24. -// Copyright © 2024 kr.codesquad.boostcamp8. All rights reserved. -// - -import Log -import UIKit diff --git a/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewController/HomeViewController.swift b/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewController/HomeViewController.swift index 214fbd14..eab630ad 100644 --- a/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewController/HomeViewController.swift +++ b/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewController/HomeViewController.swift @@ -24,6 +24,8 @@ final class HomeViewController: UIViewController { private var fetchFeedPublisher: PassthroughSubject = .init() + private var feedCount: Int = 0 + // MARK: UI Components private let button: UIButton = .init(configuration: .mainEnabled(title: "test button")) @@ -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) @@ -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 { @@ -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() + } } } diff --git a/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewModel/HomeViewModel.swift b/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewModel/HomeViewModel.swift index 81cd1b0b..9e4b49b1 100644 --- a/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewModel/HomeViewModel.swift +++ b/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewModel/HomeViewModel.swift @@ -37,6 +37,7 @@ final class HomeViewModel { private let useCase: HomeUseCaseRepresentable private var subscriptions: Set = [] + var tempID: Int = 0 init(useCase: HomeUseCaseRepresentable) { self.useCase = useCase } @@ -49,10 +50,13 @@ 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() @@ -60,4 +64,137 @@ extension HomeViewModel: HomeViewModelRepresentable { 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 + ), + ] + } }