mirror of
https://github.com/metabolist/metatext.git
synced 2025-02-16 14:05:14 +00:00
Refactoring
This commit is contained in:
parent
abc1a5cf19
commit
f1f49b7c3f
3 changed files with 29 additions and 31 deletions
|
@ -6,44 +6,38 @@ import Foundation
|
|||
import Mastodon
|
||||
import MastodonAPI
|
||||
|
||||
public struct AccountListService: CollectionService {
|
||||
public struct AccountListService {
|
||||
public let sections: AnyPublisher<[[CollectionItem]], Error>
|
||||
public let nextPageMaxIDs: 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 requestClosure: (_ maxID: String?, _ minID: String?) -> AnyPublisher<Never, Error>
|
||||
}
|
||||
private let nextPageMaxIDsSubject = PassthroughSubject<String?, Never>()
|
||||
|
||||
public extension AccountListService {
|
||||
func request(maxID: String?, minID: String?) -> AnyPublisher<Never, Error> {
|
||||
requestClosure(maxID, minID)
|
||||
}
|
||||
}
|
||||
|
||||
extension AccountListService {
|
||||
init(endpoint: AccountsEndpoint, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) {
|
||||
let list = AccountList()
|
||||
let nextPageMaxIDsSubject = PassthroughSubject<String?, Never>()
|
||||
|
||||
self.init(
|
||||
sections: contentDatabase.accountListObservation(list)
|
||||
.map { [$0.map { CollectionItem.account($0) }] }
|
||||
.eraseToAnyPublisher(),
|
||||
nextPageMaxIDs: nextPageMaxIDsSubject.eraseToAnyPublisher(),
|
||||
navigationService: NavigationService(
|
||||
status: nil,
|
||||
mastodonAPIClient: mastodonAPIClient,
|
||||
contentDatabase: contentDatabase),
|
||||
list: list,
|
||||
list = AccountList()
|
||||
self.endpoint = endpoint
|
||||
self.mastodonAPIClient = mastodonAPIClient
|
||||
self.contentDatabase = contentDatabase
|
||||
sections = contentDatabase.accountListObservation(list)
|
||||
.map { [$0.map(CollectionItem.account)] }
|
||||
.eraseToAnyPublisher()
|
||||
nextPageMaxIDs = nextPageMaxIDsSubject.eraseToAnyPublisher()
|
||||
navigationService = NavigationService(
|
||||
status: nil,
|
||||
mastodonAPIClient: mastodonAPIClient,
|
||||
contentDatabase: contentDatabase) { maxID, minID -> AnyPublisher<Never, Error> in
|
||||
mastodonAPIClient.pagedRequest(endpoint, maxID: maxID, minID: minID)
|
||||
.handleEvents(receiveOutput: { nextPageMaxIDsSubject.send($0.info.maxID) })
|
||||
.flatMap { contentDatabase.append(accounts: $0.result, toList: list) }
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
contentDatabase: contentDatabase)
|
||||
}
|
||||
}
|
||||
|
||||
extension AccountListService: CollectionService {
|
||||
public func request(maxID: String?, minID: String?) -> AnyPublisher<Never, Error> {
|
||||
mastodonAPIClient.pagedRequest(endpoint, maxID: maxID, minID: minID)
|
||||
.handleEvents(receiveOutput: { nextPageMaxIDsSubject.send($0.info.maxID) })
|
||||
.flatMap { contentDatabase.append(accounts: $0.result, toList: list) }
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import Foundation
|
|||
import Mastodon
|
||||
import MastodonAPI
|
||||
|
||||
public struct ContextService: CollectionService {
|
||||
public struct ContextService {
|
||||
public let sections: AnyPublisher<[[CollectionItem]], Error>
|
||||
public let navigationService: NavigationService
|
||||
public let nextPageMaxIDs: AnyPublisher<String?, Never> = Empty().eraseToAnyPublisher()
|
||||
|
@ -27,7 +27,9 @@ public struct ContextService: CollectionService {
|
|||
mastodonAPIClient: mastodonAPIClient,
|
||||
contentDatabase: contentDatabase)
|
||||
}
|
||||
}
|
||||
|
||||
extension ContextService: CollectionService {
|
||||
public func request(maxID: String?, minID: String?) -> AnyPublisher<Never, Error> {
|
||||
mastodonAPIClient.request(StatusEndpoint.status(id: statusID))
|
||||
.flatMap(contentDatabase.insert(status:))
|
||||
|
|
|
@ -6,7 +6,7 @@ import Foundation
|
|||
import Mastodon
|
||||
import MastodonAPI
|
||||
|
||||
public struct TimelineService: CollectionService {
|
||||
public struct TimelineService {
|
||||
public let sections: AnyPublisher<[[CollectionItem]], Error>
|
||||
public let navigationService: NavigationService
|
||||
public let nextPageMaxIDs: AnyPublisher<String?, Never>
|
||||
|
@ -35,7 +35,9 @@ public struct TimelineService: CollectionService {
|
|||
title = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension TimelineService: CollectionService {
|
||||
public func request(maxID: String?, minID: String?) -> AnyPublisher<Never, Error> {
|
||||
mastodonAPIClient.pagedRequest(timeline.endpoint, maxID: maxID, minID: minID)
|
||||
.handleEvents(receiveOutput: { nextPageMaxIDsSubject.send($0.info.maxID) })
|
||||
|
|
Loading…
Reference in a new issue