Skip to content

Commit

Permalink
feat: 화면에 피드가 표시되었을 때 Page넘버를 증가하는 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
MaraMincho committed Jan 3, 2024
1 parent 6fa7d48 commit ed26bad
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
17 changes: 11 additions & 6 deletions iOS/Projects/Features/Home/Sources/Domain/HomeUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Never> = .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
}

Expand All @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ final class HomeViewController: UIViewController {

private var dataSource: UICollectionViewDiffableDataSource<Int, FeedElement>? = nil

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

private var feedCount: Int = 0

Expand Down Expand Up @@ -132,7 +133,8 @@ private extension HomeViewController {
func bind() {
let output = viewModel.transform(
input: HomeViewModelInput(
requestFeedPublisher: fetchFeedPublisher.eraseToAnyPublisher()
requestFeedPublisher: fetchFeedPublisher.eraseToAnyPublisher(),
didDisplayFeed: didDisplayFeedPublisher.eraseToAnyPublisher()
)
)

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Foundation

public struct HomeViewModelInput {
let requestFeedPublisher: AnyPublisher<Void, Never>
let didDisplayFeed: AnyPublisher<Void, Never>
}

public typealias HomeViewModelOutput = AnyPublisher<HomeState, Never>
Expand All @@ -35,7 +36,7 @@ protocol HomeViewModelRepresentable {
final class HomeViewModel {
// MARK: - Properties

private let useCase: HomeUseCaseRepresentable
private var useCase: HomeUseCaseRepresentable
private var subscriptions: Set<AnyCancellable> = []
var tempID: Int = 0
init(useCase: HomeUseCaseRepresentable) {
Expand All @@ -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)
Expand Down

0 comments on commit ed26bad

Please sign in to comment.