metatext/ViewModels/Sources/ViewModels/View Models/LoadMoreViewModel.swift

34 lines
1.2 KiB
Swift
Raw Normal View History

2020-10-02 07:41:30 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
2020-10-03 09:19:05 +00:00
import Combine
2020-10-04 08:39:54 +00:00
import ServiceLayer
2020-10-02 07:41:30 +00:00
2021-02-04 22:24:27 +00:00
public final class LoadMoreViewModel: ObservableObject {
2020-10-05 01:25:02 +00:00
public var direction = LoadMore.Direction.up
@Published public private(set) var loading = false
2021-02-17 07:06:41 +00:00
public let identityContext: IdentityContext
2020-10-04 08:39:54 +00:00
private let loadMoreService: LoadMoreService
2021-02-04 22:24:27 +00:00
private let eventsSubject: PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>
2020-10-04 08:39:54 +00:00
2021-02-04 22:24:27 +00:00
init(loadMoreService: LoadMoreService,
2021-02-17 07:06:41 +00:00
eventsSubject: PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>,
identityContext: IdentityContext) {
2020-10-04 08:39:54 +00:00
self.loadMoreService = loadMoreService
2021-02-04 22:24:27 +00:00
self.eventsSubject = eventsSubject
2021-02-17 07:06:41 +00:00
self.identityContext = identityContext
2020-10-04 08:39:54 +00:00
}
}
2021-02-02 21:32:39 +00:00
public extension LoadMoreViewModel {
2020-10-04 08:39:54 +00:00
func loadMore() {
eventsSubject.send(
2020-10-05 01:25:02 +00:00
loadMoreService.request(direction: direction)
2020-10-04 08:39:54 +00:00
.handleEvents(
2020-10-05 01:25:02 +00:00
receiveSubscription: { [weak self] _ in self?.loading = true },
receiveCompletion: { [weak self] _ in self?.loading = false })
2020-10-04 08:39:54 +00:00
.map { _ in CollectionItemEvent.ignorableOutput }
.eraseToAnyPublisher())
}
2020-10-02 07:41:30 +00:00
}