metatext/ViewModels/Sources/ViewModels/View Models/NotificationViewModel.swift

58 lines
2.1 KiB
Swift
Raw Normal View History

2020-10-30 07:11:24 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import Foundation
import Mastodon
import ServiceLayer
public final class NotificationViewModel: CollectionItemViewModel, ObservableObject {
public let accountViewModel: AccountViewModel
2020-10-31 00:29:43 +00:00
public let statusViewModel: StatusViewModel?
2020-10-30 07:11:24 +00:00
public let events: AnyPublisher<AnyPublisher<CollectionItemEvent, Error>, Never>
public let identityContext: IdentityContext
2020-10-30 07:11:24 +00:00
private let notificationService: NotificationService
private let eventsSubject = PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>()
2021-01-26 00:06:35 +00:00
init(notificationService: NotificationService, identityContext: IdentityContext) {
2020-10-30 07:11:24 +00:00
self.notificationService = notificationService
2021-01-26 00:06:35 +00:00
self.identityContext = identityContext
2020-10-30 07:11:24 +00:00
self.accountViewModel = AccountViewModel(
accountService: notificationService.navigationService.accountService(
account: notificationService.notification.account),
2021-01-26 00:06:35 +00:00
identityContext: identityContext)
2020-10-31 00:29:43 +00:00
if let status = notificationService.notification.status {
statusViewModel = StatusViewModel(
statusService: notificationService.navigationService.statusService(status: status),
2021-01-26 00:06:35 +00:00
identityContext: identityContext)
2020-10-31 00:29:43 +00:00
} else {
statusViewModel = nil
}
2020-10-30 07:11:24 +00:00
self.events = eventsSubject.eraseToAnyPublisher()
}
}
public extension NotificationViewModel {
var type: MastodonNotification.NotificationType {
notificationService.notification.type
}
2020-10-31 00:29:43 +00:00
2021-01-27 02:31:00 +00:00
var time: String? { notificationService.notification.createdAt.timeAgo }
2021-02-02 21:04:11 +00:00
var accessibilityTime: String? {
notificationService.notification.createdAt.accessibilityTimeAgo
}
2020-10-31 00:29:43 +00:00
func accountSelected() {
eventsSubject.send(
Just(.navigation(
.profile(
notificationService.navigationService.profileService(
account: notificationService.notification.account))))
.setFailureType(to: Error.self)
.eraseToAnyPublisher())
}
2020-10-30 07:11:24 +00:00
}