diff --git a/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift b/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift index f963c80..d91c97b 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift @@ -8,14 +8,14 @@ import MastodonAPI public struct AccountListService { public let sections: AnyPublisher<[[CollectionItem]], Error> - public let nextPageMaxIDs: AnyPublisher + 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 nextPageMaxIDsSubject = PassthroughSubject() + private let nextPageMaxIDsSubject = PassthroughSubject() 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 { 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() } diff --git a/ServiceLayer/Sources/ServiceLayer/Services/CollectionService.swift b/ServiceLayer/Sources/ServiceLayer/Services/CollectionService.swift index b584588..59459e8 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/CollectionService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/CollectionService.swift @@ -4,14 +4,17 @@ import Combine public protocol CollectionService { var sections: AnyPublisher<[[CollectionItem]], Error> { get } - var nextPageMaxIDs: AnyPublisher { get } + var nextPageMaxIDs: AnyPublisher { get } + var title: AnyPublisher { get } var navigationService: NavigationService { get } - var title: String? { get } var contextParentID: String? { get } func request(maxID: String?, minID: String?) -> AnyPublisher } extension CollectionService { - public var title: String? { nil } + public var nextPageMaxIDs: AnyPublisher { Empty().eraseToAnyPublisher() } + + public var title: AnyPublisher { Empty().eraseToAnyPublisher() } + public var contextParentID: String? { nil } } diff --git a/ServiceLayer/Sources/ServiceLayer/Services/ContextService.swift b/ServiceLayer/Sources/ServiceLayer/Services/ContextService.swift index 7914bfd..55e33d2 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/ContextService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/ContextService.swift @@ -9,8 +9,6 @@ import MastodonAPI public struct ContextService { public let sections: AnyPublisher<[[CollectionItem]], Error> public let navigationService: NavigationService - public let nextPageMaxIDs: AnyPublisher = Empty().eraseToAnyPublisher() - public let title: String? = nil public var contextParentID: String? { statusID } private let statusID: String diff --git a/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift b/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift index ac33a8c..ae4b8a6 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift @@ -9,14 +9,14 @@ import MastodonAPI public struct TimelineService { public let sections: AnyPublisher<[[CollectionItem]], Error> public let navigationService: NavigationService - public let nextPageMaxIDs: AnyPublisher - public let title: String? + public let nextPageMaxIDs: AnyPublisher + public let title: AnyPublisher public let contextParentID: String? = nil private let timeline: Timeline private let mastodonAPIClient: MastodonAPIClient private let contentDatabase: ContentDatabase - private let nextPageMaxIDsSubject = PassthroughSubject() + private let nextPageMaxIDsSubject = PassthroughSubject() 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 { 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() } diff --git a/ViewModels/Sources/ViewModels/CollectionItemsViewModel.swift b/ViewModels/Sources/ViewModels/CollectionItemsViewModel.swift index 849418a..1ef1129 100644 --- a/ViewModels/Sources/ViewModels/CollectionItemsViewModel.swift +++ b/ViewModels/Sources/ViewModels/CollectionItemsViewModel.swift @@ -38,7 +38,7 @@ extension CollectionItemsViewModel: CollectionViewModel { items.map { $0.map { $0.map(CollectionItemIdentifier.init(item:)) } }.eraseToAnyPublisher() } - public var title: AnyPublisher { Just(collectionService.title).eraseToAnyPublisher() } + public var title: AnyPublisher { collectionService.title } public var alertItems: AnyPublisher { $alertItem.compactMap { $0 }.eraseToAnyPublisher() } diff --git a/ViewModels/Sources/ViewModels/CollectionViewModel.swift b/ViewModels/Sources/ViewModels/CollectionViewModel.swift index 2ad616c..b68a091 100644 --- a/ViewModels/Sources/ViewModels/CollectionViewModel.swift +++ b/ViewModels/Sources/ViewModels/CollectionViewModel.swift @@ -5,7 +5,7 @@ import Foundation public protocol CollectionViewModel { var sections: AnyPublisher<[[CollectionItemIdentifier]], Never> { get } - var title: AnyPublisher { get } + var title: AnyPublisher { get } var alertItems: AnyPublisher { get } var loading: AnyPublisher { get } var navigationEvents: AnyPublisher { get } diff --git a/ViewModels/Sources/ViewModels/ProfileViewModel.swift b/ViewModels/Sources/ViewModels/ProfileViewModel.swift index 9e3434a..bb7048b 100644 --- a/ViewModels/Sources/ViewModels/ProfileViewModel.swift +++ b/ViewModels/Sources/ViewModels/ProfileViewModel.swift @@ -43,8 +43,8 @@ extension ProfileViewModel: CollectionViewModel { collectionViewModel.flatMap(\.sections).eraseToAnyPublisher() } - public var title: AnyPublisher { - $accountViewModel.map { $0?.accountName }.eraseToAnyPublisher() + public var title: AnyPublisher { + $accountViewModel.compactMap { $0?.accountName }.eraseToAnyPublisher() } public var alertItems: AnyPublisher {