metatext-app-ios-iphone-ipad/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift

57 lines
1.9 KiB
Swift
Raw Normal View History

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