mirror of
https://github.com/metabolist/metatext.git
synced 2024-05-28 13:18:05 +00:00
34 lines
1.1 KiB
Swift
34 lines
1.1 KiB
Swift
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
import Combine
|
|
import DB
|
|
import MastodonAPI
|
|
|
|
public struct LoadMoreService {
|
|
private let loadMore: LoadMore
|
|
private let mastodonAPIClient: MastodonAPIClient
|
|
private let contentDatabase: ContentDatabase
|
|
|
|
init(loadMore: LoadMore, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) {
|
|
self.loadMore = loadMore
|
|
self.mastodonAPIClient = mastodonAPIClient
|
|
self.contentDatabase = contentDatabase
|
|
}
|
|
}
|
|
|
|
public extension LoadMoreService {
|
|
func request(direction: LoadMore.Direction) -> AnyPublisher<Never, Error> {
|
|
mastodonAPIClient.pagedRequest(
|
|
loadMore.timeline.endpoint,
|
|
maxId: direction == .down ? loadMore.afterStatusId : nil,
|
|
minId: direction == .up ? loadMore.beforeStatusId : nil)
|
|
.flatMap {
|
|
contentDatabase.insert(
|
|
statuses: $0.result,
|
|
timeline: loadMore.timeline,
|
|
loadMoreAndDirection: (loadMore, direction))
|
|
}
|
|
.eraseToAnyPublisher()
|
|
}
|
|
}
|