metatext-app-ios-iphone-ipad/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift

45 lines
1.7 KiB
Swift
Raw Normal View History

2020-09-23 03:00:56 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import DB
import Foundation
import Mastodon
import MastodonAPI
2020-10-05 21:11:41 +02:00
public struct AccountListService {
2020-10-05 08:36:22 +02:00
public let sections: AnyPublisher<[[CollectionItem]], Error>
2020-10-06 00:50:05 +02:00
public let nextPageMaxId: AnyPublisher<String, Never>
2020-09-25 07:39:06 +02:00
public let navigationService: NavigationService
2020-09-23 03:00:56 +02:00
2020-09-29 00:40:03 +02:00
private let list: AccountList
2020-10-05 21:11:41 +02:00
private let endpoint: AccountsEndpoint
2020-09-23 03:00:56 +02:00
private let mastodonAPIClient: MastodonAPIClient
private let contentDatabase: ContentDatabase
2020-10-06 00:50:05 +02:00
private let nextPageMaxIdSubject = PassthroughSubject<String, Never>()
2020-09-23 03:00:56 +02:00
2020-10-05 21:11:41 +02:00
init(endpoint: AccountsEndpoint, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) {
list = AccountList()
self.endpoint = endpoint
self.mastodonAPIClient = mastodonAPIClient
self.contentDatabase = contentDatabase
sections = contentDatabase.accountListObservation(list)
.map { [$0.map(CollectionItem.account)] }
.eraseToAnyPublisher()
2020-10-06 00:50:05 +02:00
nextPageMaxId = nextPageMaxIdSubject.eraseToAnyPublisher()
2020-10-05 21:58:03 +02:00
navigationService = NavigationService(mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
2020-09-29 01:23:41 +02:00
}
}
2020-10-05 21:11:41 +02:00
extension AccountListService: CollectionService {
2020-10-06 00:50:05 +02:00
public func request(maxId: String?, minId: String?) -> AnyPublisher<Never, Error> {
mastodonAPIClient.pagedRequest(endpoint, maxId: maxId, minId: minId)
2020-10-05 22:21:06 +02:00
.handleEvents(receiveOutput: {
2020-10-06 00:50:05 +02:00
guard let maxId = $0.info.maxId else { return }
2020-10-05 22:21:06 +02:00
2020-10-06 00:50:05 +02:00
nextPageMaxIdSubject.send(maxId)
2020-10-05 22:21:06 +02:00
})
2020-10-05 21:11:41 +02:00
.flatMap { contentDatabase.append(accounts: $0.result, toList: list) }
.eraseToAnyPublisher()
2020-09-25 07:39:06 +02:00
}
2020-09-23 03:00:56 +02:00
}