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>
|
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-10-27 03:01:12 +00:00
|
|
|
private let nextPageMaxIdSubject: CurrentValueSubject<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-27 03:01:12 +00:00
|
|
|
|
|
|
|
let nextPageMaxIdSubject = CurrentValueSubject<String, Never>(String(Int.max))
|
|
|
|
|
|
|
|
self.nextPageMaxIdSubject = nextPageMaxIdSubject
|
2020-10-06 20:44:22 +00:00
|
|
|
sections = contentDatabase.timelinePublisher(timeline)
|
2020-10-27 03:01:12 +00:00
|
|
|
.handleEvents(receiveOutput: {
|
|
|
|
guard case let .status(status, _) = $0.last?.last,
|
|
|
|
status.id < nextPageMaxIdSubject.value
|
|
|
|
else { return }
|
|
|
|
|
|
|
|
nextPageMaxIdSubject.send(status.id)
|
|
|
|
})
|
|
|
|
.eraseToAnyPublisher()
|
2020-10-05 19:58:03 +00:00
|
|
|
navigationService = NavigationService(mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
|
2020-10-27 03:01:12 +00:00
|
|
|
nextPageMaxId = nextPageMaxIdSubject.dropFirst().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-10-27 03:01:12 +00:00
|
|
|
guard let maxId = $0.info.maxId, maxId < nextPageMaxIdSubject.value else { return }
|
2020-10-05 20:21:06 +00:00
|
|
|
|
2020-10-05 22:50:05 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|