From b1cc59a7e986b328ba37f6875ad74bad36dd33a1 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sun, 24 Jan 2021 00:19:57 -0800 Subject: [PATCH] Remove state from service layer --- .../Services/AccountListService.swift | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift b/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift index 89947c8..0ad9c68 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift @@ -12,7 +12,7 @@ public struct AccountListService { public let navigationService: NavigationService public let canRefresh = false - private let accountList = CurrentValueSubject<[Account], Error>([]) + private let accountsSubject = PassthroughSubject<[Account], Error>() private let endpoint: AccountsEndpoint private let mastodonAPIClient: MastodonAPIClient private let contentDatabase: ContentDatabase @@ -27,7 +27,13 @@ public struct AccountListService { self.mastodonAPIClient = mastodonAPIClient self.contentDatabase = contentDatabase 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() navigationService = NavigationService(mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase) } @@ -37,19 +43,13 @@ extension AccountListService: CollectionService { public func request(maxId: String?, minId: String?, search: Search?) -> AnyPublisher { mastodonAPIClient.pagedRequest(endpoint, maxId: maxId, minId: minId) .handleEvents(receiveOutput: { + accountsSubject.send($0.result) + guard let maxId = $0.info.maxId else { return } nextPageMaxIdSubject.send(maxId) }) - .flatMap { response in - contentDatabase.insert(accounts: response.result) - .collect() - .map { _ in - let presentIds = Set(accountList.value.map(\.id)) - - accountList.value += response.result.filter { !presentIds.contains($0.id) } - } - } + .flatMap { contentDatabase.insert(accounts: $0.result) } .ignoreOutput() .eraseToAnyPublisher() }