metatext/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift
Justin Mazzocchi a6d959a6b9
Localized title
2020-12-02 17:41:22 -08:00

58 lines
2.1 KiB
Swift

// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import DB
import Foundation
import Mastodon
import MastodonAPI
public struct AccountListService {
public let sections: AnyPublisher<[[CollectionItem]], Error>
public let nextPageMaxId: AnyPublisher<String, Never>
public let navigationService: NavigationService
private let list: AccountList
private let endpoint: AccountsEndpoint
private let mastodonAPIClient: MastodonAPIClient
private let contentDatabase: ContentDatabase
private let titleComponents: [String]?
private let nextPageMaxIdSubject = PassthroughSubject<String, Never>()
init(endpoint: AccountsEndpoint,
mastodonAPIClient: MastodonAPIClient,
contentDatabase: ContentDatabase,
titleComponents: [String]? = nil) {
list = AccountList()
self.endpoint = endpoint
self.mastodonAPIClient = mastodonAPIClient
self.contentDatabase = contentDatabase
self.titleComponents = titleComponents
sections = contentDatabase.accountListPublisher(list)
.map { [$0.map(CollectionItem.account)] }
.eraseToAnyPublisher()
nextPageMaxId = nextPageMaxIdSubject.eraseToAnyPublisher()
navigationService = NavigationService(mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
}
}
extension AccountListService: CollectionService {
public func request(maxId: String?, minId: String?) -> AnyPublisher<Never, Error> {
mastodonAPIClient.pagedRequest(endpoint, maxId: maxId, minId: minId)
.handleEvents(receiveOutput: {
guard let maxId = $0.info.maxId else { return }
nextPageMaxIdSubject.send(maxId)
})
.flatMap { contentDatabase.append(accounts: $0.result, toList: list) }
.eraseToAnyPublisher()
}
public var titleLocalizationComponents: AnyPublisher<[String], Never> {
if let titleComponents = titleComponents {
return Just(titleComponents).eraseToAnyPublisher()
} else {
return Empty().eraseToAnyPublisher()
}
}
}