From f1f49b7c3fa7445d96ed7ae4049a78156f095602 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Mon, 5 Oct 2020 12:11:41 -0700 Subject: [PATCH] Refactoring --- .../Services/AccountListService.swift | 52 ++++++++----------- .../Services/ContextService.swift | 4 +- .../Services/TimelineService.swift | 4 +- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift b/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift index abc5dcf..e67afb9 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift @@ -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 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 -} + private let nextPageMaxIDsSubject = PassthroughSubject() -public extension AccountListService { - func request(maxID: String?, minID: String?) -> AnyPublisher { - requestClosure(maxID, minID) - } -} - -extension AccountListService { init(endpoint: AccountsEndpoint, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) { - let list = AccountList() - let nextPageMaxIDsSubject = PassthroughSubject() - - 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 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 { + mastodonAPIClient.pagedRequest(endpoint, maxID: maxID, minID: minID) + .handleEvents(receiveOutput: { nextPageMaxIDsSubject.send($0.info.maxID) }) + .flatMap { contentDatabase.append(accounts: $0.result, toList: list) } + .eraseToAnyPublisher() } } diff --git a/ServiceLayer/Sources/ServiceLayer/Services/ContextService.swift b/ServiceLayer/Sources/ServiceLayer/Services/ContextService.swift index 944fd44..39f10f2 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/ContextService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/ContextService.swift @@ -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 = 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 { mastodonAPIClient.request(StatusEndpoint.status(id: statusID)) .flatMap(contentDatabase.insert(status:)) diff --git a/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift b/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift index 1483e56..3fe41eb 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift @@ -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 @@ -35,7 +35,9 @@ public struct TimelineService: CollectionService { title = nil } } +} +extension TimelineService: CollectionService { public func request(maxID: String?, minID: String?) -> AnyPublisher { mastodonAPIClient.pagedRequest(timeline.endpoint, maxID: maxID, minID: minID) .handleEvents(receiveOutput: { nextPageMaxIDsSubject.send($0.info.maxID) })