metatext/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift

58 lines
2 KiB
Swift
Raw Normal View History

2020-10-05 07:04:15 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import DB
import Foundation
import Mastodon
import MastodonAPI
2020-10-05 19:11:41 +00:00
public struct TimelineService {
2020-10-05 07:04:15 +00:00
public let sections: AnyPublisher<[[CollectionItem]], Error>
public let navigationService: NavigationService
2020-10-05 22:50:05 +00:00
public let nextPageMaxId: AnyPublisher<String, Never>
2021-01-16 19:41:01 +00:00
public let preferLastPresentIdOverNextPageMaxId = true
2020-10-05 20:21:06 +00:00
public let title: AnyPublisher<String, Never>
2020-10-05 07:04:15 +00:00
private let timeline: Timeline
private let mastodonAPIClient: MastodonAPIClient
private let contentDatabase: ContentDatabase
2020-12-01 03:07:38 +00:00
private let nextPageMaxIdSubject = PassthroughSubject<String, Never>()
2020-10-05 07:04:15 +00:00
init(timeline: Timeline, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) {
self.timeline = timeline
self.mastodonAPIClient = mastodonAPIClient
self.contentDatabase = contentDatabase
2020-10-06 20:44:22 +00:00
sections = contentDatabase.timelinePublisher(timeline)
2020-10-05 19:58:03 +00:00
navigationService = NavigationService(mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
2020-12-01 03:07:38 +00:00
nextPageMaxId = nextPageMaxIdSubject.eraseToAnyPublisher()
2020-10-05 07:04:15 +00:00
if case let .tag(tag) = timeline {
2020-10-05 20:21:06 +00:00
title = Just("#".appending(tag)).eraseToAnyPublisher()
2020-10-05 07:04:15 +00:00
} else {
2020-10-05 20:21:06 +00:00
title = Empty().eraseToAnyPublisher()
2020-10-05 07:04:15 +00:00
}
}
2020-10-05 19:11:41 +00:00
}
2020-10-05 07:04:15 +00:00
2020-10-05 19:11:41 +00:00
extension TimelineService: CollectionService {
2020-10-27 03:01:12 +00:00
public var markerTimeline: Marker.Timeline? {
switch timeline {
case .home:
return .home
default:
return nil
}
}
2020-10-05 22:50:05 +00:00
public func request(maxId: String?, minId: String?) -> AnyPublisher<Never, Error> {
mastodonAPIClient.pagedRequest(timeline.endpoint, maxId: maxId, minId: minId)
2020-10-05 20:21:06 +00:00
.handleEvents(receiveOutput: {
2020-12-01 03:07:38 +00:00
if let maxId = $0.info.maxId {
nextPageMaxIdSubject.send(maxId)
}
2020-10-05 20:21:06 +00:00
})
2020-10-05 07:04:15 +00:00
.flatMap { contentDatabase.insert(statuses: $0.result, timeline: timeline) }
.eraseToAnyPublisher()
}
}