mirror of
https://github.com/metabolist/metatext.git
synced 2025-02-17 22:45:13 +00:00
Remove state from service layer
This commit is contained in:
parent
a3491cec85
commit
b1cc59a7e9
1 changed files with 11 additions and 11 deletions
|
@ -12,7 +12,7 @@ public struct AccountListService {
|
||||||
public let navigationService: NavigationService
|
public let navigationService: NavigationService
|
||||||
public let canRefresh = false
|
public let canRefresh = false
|
||||||
|
|
||||||
private let accountList = CurrentValueSubject<[Account], Error>([])
|
private let accountsSubject = PassthroughSubject<[Account], Error>()
|
||||||
private let endpoint: AccountsEndpoint
|
private let endpoint: AccountsEndpoint
|
||||||
private let mastodonAPIClient: MastodonAPIClient
|
private let mastodonAPIClient: MastodonAPIClient
|
||||||
private let contentDatabase: ContentDatabase
|
private let contentDatabase: ContentDatabase
|
||||||
|
@ -27,7 +27,13 @@ public struct AccountListService {
|
||||||
self.mastodonAPIClient = mastodonAPIClient
|
self.mastodonAPIClient = mastodonAPIClient
|
||||||
self.contentDatabase = contentDatabase
|
self.contentDatabase = contentDatabase
|
||||||
self.titleComponents = titleComponents
|
self.titleComponents = titleComponents
|
||||||
sections = accountList.map { [.init(items: $0.map(CollectionItem.account))] }.eraseToAnyPublisher()
|
sections = accountsSubject.scan([]) {
|
||||||
|
let presentIds = Set($0.map(\.id))
|
||||||
|
|
||||||
|
return $0 + $1.filter { !presentIds.contains($0.id) }
|
||||||
|
}
|
||||||
|
.map { [.init(items: $0.map(CollectionItem.account))] }
|
||||||
|
.eraseToAnyPublisher()
|
||||||
nextPageMaxId = nextPageMaxIdSubject.eraseToAnyPublisher()
|
nextPageMaxId = nextPageMaxIdSubject.eraseToAnyPublisher()
|
||||||
navigationService = NavigationService(mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
|
navigationService = NavigationService(mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
|
||||||
}
|
}
|
||||||
|
@ -37,19 +43,13 @@ extension AccountListService: CollectionService {
|
||||||
public func request(maxId: String?, minId: String?, search: Search?) -> AnyPublisher<Never, Error> {
|
public func request(maxId: String?, minId: String?, search: Search?) -> AnyPublisher<Never, Error> {
|
||||||
mastodonAPIClient.pagedRequest(endpoint, maxId: maxId, minId: minId)
|
mastodonAPIClient.pagedRequest(endpoint, maxId: maxId, minId: minId)
|
||||||
.handleEvents(receiveOutput: {
|
.handleEvents(receiveOutput: {
|
||||||
|
accountsSubject.send($0.result)
|
||||||
|
|
||||||
guard let maxId = $0.info.maxId else { return }
|
guard let maxId = $0.info.maxId else { return }
|
||||||
|
|
||||||
nextPageMaxIdSubject.send(maxId)
|
nextPageMaxIdSubject.send(maxId)
|
||||||
})
|
})
|
||||||
.flatMap { response in
|
.flatMap { contentDatabase.insert(accounts: $0.result) }
|
||||||
contentDatabase.insert(accounts: response.result)
|
|
||||||
.collect()
|
|
||||||
.map { _ in
|
|
||||||
let presentIds = Set(accountList.value.map(\.id))
|
|
||||||
|
|
||||||
accountList.value += response.result.filter { !presentIds.contains($0.id) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.ignoreOutput()
|
.ignoreOutput()
|
||||||
.eraseToAnyPublisher()
|
.eraseToAnyPublisher()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue