From ed26bad8f3cc779ad8fda11c94e4196488dae59d Mon Sep 17 00:00:00 2001 From: MaraMincho <103064352+MaraMincho@users.noreply.github.com> Date: Wed, 3 Jan 2024 13:39:05 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=99=94=EB=A9=B4=EC=97=90=20=ED=94=BC?= =?UTF-8?q?=EB=93=9C=EA=B0=80=20=ED=91=9C=EC=8B=9C=EB=90=98=EC=97=88?= =?UTF-8?q?=EC=9D=84=20=EB=95=8C=20Page=EB=84=98=EB=B2=84=EB=A5=BC=20?= =?UTF-8?q?=EC=A6=9D=EA=B0=80=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Home/Sources/Domain/HomeUseCase.swift | 17 +++++++++++------ .../ViewController/HomeViewController.swift | 9 ++++++--- .../HomeScene/ViewModel/HomeViewModel.swift | 9 ++++++++- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/iOS/Projects/Features/Home/Sources/Domain/HomeUseCase.swift b/iOS/Projects/Features/Home/Sources/Domain/HomeUseCase.swift index 5ca3874d..c1c6e580 100644 --- a/iOS/Projects/Features/Home/Sources/Domain/HomeUseCase.swift +++ b/iOS/Projects/Features/Home/Sources/Domain/HomeUseCase.swift @@ -13,18 +13,19 @@ import Foundation public protocol HomeUseCaseRepresentable { func fetchFeed() -> AnyPublisher<[FeedElement], Never> + mutating func didDisplayFeed() } // MARK: - HomeUseCase public struct HomeUseCase: HomeUseCaseRepresentable { - let feedRepositoryRepresentable: FeedRepositoryRepresentable - var latestFeedPage = 0 + private let feedRepositoryRepresentable: FeedRepositoryRepresentable - var pipeLine: PassthroughSubject = .init() - let checkManager: FetchCheckManager = .init() + private var latestFeedPage = 0 + private var feedElementPublisher: PassthroughSubject<[FeedElement], Never> = .init() + private let checkManager: FetchCheckManager = .init() - init(feedRepositoryRepresentable: FeedRepositoryRepresentable) { + public init(feedRepositoryRepresentable: FeedRepositoryRepresentable) { self.feedRepositoryRepresentable = feedRepositoryRepresentable } @@ -33,7 +34,11 @@ public struct HomeUseCase: HomeUseCaseRepresentable { return Empty().eraseToAnyPublisher() } checkManager[latestFeedPage] = true - return feedRepositoryRepresentable.fetchFeed(at: latestFeedPage) + return feedElementPublisher.eraseToAnyPublisher() + } + + public mutating func didDisplayFeed() { + latestFeedPage += 1 } } 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 eab630ad..f4eb7c49 100644 --- a/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewController/HomeViewController.swift +++ b/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewController/HomeViewController.swift @@ -22,7 +22,8 @@ final class HomeViewController: UIViewController { private var dataSource: UICollectionViewDiffableDataSource? = nil - private var fetchFeedPublisher: PassthroughSubject = .init() + private let fetchFeedPublisher: PassthroughSubject = .init() + private let didDisplayFeedPublisher: PassthroughSubject = .init() private var feedCount: Int = 0 @@ -132,7 +133,8 @@ private extension HomeViewController { func bind() { let output = viewModel.transform( input: HomeViewModelInput( - requestFeedPublisher: fetchFeedPublisher.eraseToAnyPublisher() + requestFeedPublisher: fetchFeedPublisher.eraseToAnyPublisher(), + didDisplayFeed: didDisplayFeedPublisher.eraseToAnyPublisher() ) ) @@ -158,8 +160,9 @@ private extension HomeViewController { } var snapshot = dataSource.snapshot() snapshot.appendItems(item) - DispatchQueue.main.async { + DispatchQueue.main.async { [weak self] in dataSource.apply(snapshot) + self?.didDisplayFeedPublisher.send() } feedCount = snapshot.numberOfItems 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 502740be..25b337bc 100644 --- a/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewModel/HomeViewModel.swift +++ b/iOS/Projects/Features/Home/Sources/Presntaion/HomeScene/ViewModel/HomeViewModel.swift @@ -13,6 +13,7 @@ import Foundation public struct HomeViewModelInput { let requestFeedPublisher: AnyPublisher + let didDisplayFeed: AnyPublisher } public typealias HomeViewModelOutput = AnyPublisher @@ -35,7 +36,7 @@ protocol HomeViewModelRepresentable { final class HomeViewModel { // MARK: - Properties - private let useCase: HomeUseCaseRepresentable + private var useCase: HomeUseCaseRepresentable private var subscriptions: Set = [] var tempID: Int = 0 init(useCase: HomeUseCaseRepresentable) { @@ -58,6 +59,12 @@ extension HomeViewModel: HomeViewModelRepresentable { } .eraseToAnyPublisher() + input.didDisplayFeed + .sink { [weak self] _ in + self?.useCase.didDisplayFeed() + } + .store(in: &subscriptions) + let initialState: HomeViewModelOutput = Just(.idle).eraseToAnyPublisher() return initialState.merge(with: fetched)