Refactoring

This commit is contained in:
Justin Mazzocchi 2020-10-05 13:21:06 -07:00
parent 35de31553e
commit 18d1b61101
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
7 changed files with 27 additions and 18 deletions

View file

@ -8,14 +8,14 @@ import MastodonAPI
public struct AccountListService {
public let sections: AnyPublisher<[[CollectionItem]], Error>
public let nextPageMaxIDs: AnyPublisher<String?, Never>
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 nextPageMaxIDsSubject = PassthroughSubject<String?, Never>()
private let nextPageMaxIDsSubject = PassthroughSubject<String, Never>()
init(endpoint: AccountsEndpoint, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) {
list = AccountList()
@ -33,7 +33,11 @@ public struct AccountListService {
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) })
.handleEvents(receiveOutput: {
guard let maxID = $0.info.maxID else { return }
nextPageMaxIDsSubject.send(maxID)
})
.flatMap { contentDatabase.append(accounts: $0.result, toList: list) }
.eraseToAnyPublisher()
}

View file

@ -4,14 +4,17 @@ import Combine
public protocol CollectionService {
var sections: AnyPublisher<[[CollectionItem]], Error> { get }
var nextPageMaxIDs: AnyPublisher<String?, Never> { get }
var nextPageMaxIDs: AnyPublisher<String, Never> { get }
var title: AnyPublisher<String, Never> { get }
var navigationService: NavigationService { get }
var title: String? { get }
var contextParentID: String? { get }
func request(maxID: String?, minID: String?) -> AnyPublisher<Never, Error>
}
extension CollectionService {
public var title: String? { nil }
public var nextPageMaxIDs: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() }
public var title: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() }
public var contextParentID: String? { nil }
}

View file

@ -9,8 +9,6 @@ import MastodonAPI
public struct ContextService {
public let sections: AnyPublisher<[[CollectionItem]], Error>
public let navigationService: NavigationService
public let nextPageMaxIDs: AnyPublisher<String?, Never> = Empty().eraseToAnyPublisher()
public let title: String? = nil
public var contextParentID: String? { statusID }
private let statusID: String

View file

@ -9,14 +9,14 @@ import MastodonAPI
public struct TimelineService {
public let sections: AnyPublisher<[[CollectionItem]], Error>
public let navigationService: NavigationService
public let nextPageMaxIDs: AnyPublisher<String?, Never>
public let title: String?
public let nextPageMaxIDs: AnyPublisher<String, Never>
public let title: AnyPublisher<String, Never>
public let contextParentID: String? = nil
private let timeline: Timeline
private let mastodonAPIClient: MastodonAPIClient
private let contentDatabase: ContentDatabase
private let nextPageMaxIDsSubject = PassthroughSubject<String?, Never>()
private let nextPageMaxIDsSubject = PassthroughSubject<String, Never>()
init(timeline: Timeline, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) {
self.timeline = timeline
@ -27,9 +27,9 @@ public struct TimelineService {
nextPageMaxIDs = nextPageMaxIDsSubject.eraseToAnyPublisher()
if case let .tag(tag) = timeline {
title = "#".appending(tag)
title = Just("#".appending(tag)).eraseToAnyPublisher()
} else {
title = nil
title = Empty().eraseToAnyPublisher()
}
}
}
@ -37,7 +37,11 @@ public struct TimelineService {
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) })
.handleEvents(receiveOutput: {
guard let maxID = $0.info.maxID else { return }
nextPageMaxIDsSubject.send(maxID)
})
.flatMap { contentDatabase.insert(statuses: $0.result, timeline: timeline) }
.eraseToAnyPublisher()
}

View file

@ -38,7 +38,7 @@ extension CollectionItemsViewModel: CollectionViewModel {
items.map { $0.map { $0.map(CollectionItemIdentifier.init(item:)) } }.eraseToAnyPublisher()
}
public var title: AnyPublisher<String?, Never> { Just(collectionService.title).eraseToAnyPublisher() }
public var title: AnyPublisher<String, Never> { collectionService.title }
public var alertItems: AnyPublisher<AlertItem, Never> { $alertItem.compactMap { $0 }.eraseToAnyPublisher() }

View file

@ -5,7 +5,7 @@ import Foundation
public protocol CollectionViewModel {
var sections: AnyPublisher<[[CollectionItemIdentifier]], Never> { get }
var title: AnyPublisher<String?, Never> { get }
var title: AnyPublisher<String, Never> { get }
var alertItems: AnyPublisher<AlertItem, Never> { get }
var loading: AnyPublisher<Bool, Never> { get }
var navigationEvents: AnyPublisher<NavigationEvent, Never> { get }

View file

@ -43,8 +43,8 @@ extension ProfileViewModel: CollectionViewModel {
collectionViewModel.flatMap(\.sections).eraseToAnyPublisher()
}
public var title: AnyPublisher<String?, Never> {
$accountViewModel.map { $0?.accountName }.eraseToAnyPublisher()
public var title: AnyPublisher<String, Never> {
$accountViewModel.compactMap { $0?.accountName }.eraseToAnyPublisher()
}
public var alertItems: AnyPublisher<AlertItem, Never> {