2020-08-21 02:29:01 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
2020-09-05 02:31:43 +00:00
|
|
|
import Foundation
|
2020-08-30 23:33:11 +00:00
|
|
|
import Mastodon
|
2020-08-31 18:57:02 +00:00
|
|
|
import ServiceLayer
|
2020-08-21 02:29:01 +00:00
|
|
|
|
2021-01-08 06:11:33 +00:00
|
|
|
public final class StatusViewModel: CollectionItemViewModel, AttachmentsRenderingViewModel, ObservableObject {
|
2020-09-01 07:33:49 +00:00
|
|
|
public let content: NSAttributedString
|
2021-01-12 07:33:35 +00:00
|
|
|
public let contentEmojis: [Emoji]
|
2020-09-01 07:33:49 +00:00
|
|
|
public let displayName: String
|
2021-01-12 07:33:35 +00:00
|
|
|
public let displayNameEmojis: [Emoji]
|
2020-09-01 07:33:49 +00:00
|
|
|
public let spoilerText: String
|
|
|
|
public let isReblog: Bool
|
|
|
|
public let rebloggedByDisplayName: String
|
2021-01-12 07:33:35 +00:00
|
|
|
public let rebloggedByDisplayNameEmojis: [Emoji]
|
2020-09-01 07:33:49 +00:00
|
|
|
public let attachmentViewModels: [AttachmentViewModel]
|
2021-01-12 07:33:35 +00:00
|
|
|
public let pollEmojis: [Emoji]
|
2020-10-25 02:31:44 +00:00
|
|
|
@Published public var pollOptionSelections = Set<Int>()
|
2020-10-06 00:33:58 +00:00
|
|
|
public var configuration = CollectionItem.StatusConfiguration.default
|
2020-09-25 05:39:06 +00:00
|
|
|
public let events: AnyPublisher<AnyPublisher<CollectionItemEvent, Error>, Never>
|
2020-08-21 02:29:01 +00:00
|
|
|
|
|
|
|
private let statusService: StatusService
|
2020-10-22 22:16:06 +00:00
|
|
|
private let identification: Identification
|
2020-09-25 05:39:06 +00:00
|
|
|
private let eventsSubject = PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>()
|
2020-08-21 02:29:01 +00:00
|
|
|
|
2020-10-07 21:06:26 +00:00
|
|
|
init(statusService: StatusService, identification: Identification) {
|
2020-08-21 02:29:01 +00:00
|
|
|
self.statusService = statusService
|
2020-10-07 21:06:26 +00:00
|
|
|
self.identification = identification
|
2020-08-21 02:29:01 +00:00
|
|
|
content = statusService.status.displayStatus.content.attributed
|
2021-01-12 07:33:35 +00:00
|
|
|
contentEmojis = statusService.status.displayStatus.emojis
|
2020-12-03 22:32:15 +00:00
|
|
|
displayName = statusService.status.displayStatus.account.displayName.isEmpty
|
2020-08-21 02:29:01 +00:00
|
|
|
? statusService.status.displayStatus.account.username
|
|
|
|
: statusService.status.displayStatus.account.displayName
|
2021-01-12 07:33:35 +00:00
|
|
|
displayNameEmojis = statusService.status.displayStatus.account.emojis
|
2020-08-21 02:29:01 +00:00
|
|
|
spoilerText = statusService.status.displayStatus.spoilerText
|
|
|
|
isReblog = statusService.status.reblog != nil
|
2020-12-03 22:32:15 +00:00
|
|
|
rebloggedByDisplayName = statusService.status.account.displayName.isEmpty
|
2020-08-21 02:29:01 +00:00
|
|
|
? statusService.status.account.username
|
|
|
|
: statusService.status.account.displayName
|
2021-01-12 07:33:35 +00:00
|
|
|
rebloggedByDisplayNameEmojis = statusService.status.account.emojis
|
2020-08-28 19:56:28 +00:00
|
|
|
attachmentViewModels = statusService.status.displayStatus.mediaAttachments
|
2021-01-08 06:11:33 +00:00
|
|
|
.map { AttachmentViewModel(attachment: $0, identification: identification, status: statusService.status) }
|
2021-01-12 07:33:35 +00:00
|
|
|
pollEmojis = statusService.status.displayStatus.poll?.emojis ?? []
|
2020-09-14 23:32:34 +00:00
|
|
|
events = eventsSubject.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:33:49 +00:00
|
|
|
public extension StatusViewModel {
|
2021-01-11 03:12:06 +00:00
|
|
|
var isMine: Bool { statusService.status.displayStatus.account.id == identification.identity.account?.id }
|
|
|
|
|
2020-10-14 00:03:01 +00:00
|
|
|
var shouldShowContent: Bool {
|
2020-10-11 00:02:02 +00:00
|
|
|
guard spoilerText != "" else { return true }
|
2020-10-07 21:06:26 +00:00
|
|
|
|
|
|
|
if identification.identity.preferences.readingExpandSpoilers {
|
2020-10-14 00:03:01 +00:00
|
|
|
return !configuration.showContentToggled
|
2020-08-21 02:29:01 +00:00
|
|
|
} else {
|
2020-10-14 00:03:01 +00:00
|
|
|
return configuration.showContentToggled
|
2020-08-21 02:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 00:03:01 +00:00
|
|
|
var shouldShowAttachments: Bool {
|
|
|
|
switch identification.identity.preferences.readingExpandMedia {
|
|
|
|
case .default, .unknown:
|
|
|
|
return !sensitive || configuration.showAttachmentsToggled
|
|
|
|
case .showAll:
|
|
|
|
return !configuration.showAttachmentsToggled
|
|
|
|
case .hideAll:
|
|
|
|
return configuration.showAttachmentsToggled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var shouldShowHideAttachmentsButton: Bool {
|
|
|
|
sensitive || identification.identity.preferences.readingExpandMedia == .hideAll
|
|
|
|
}
|
|
|
|
|
2021-01-10 05:56:15 +00:00
|
|
|
var id: Status.Id { statusService.status.displayStatus.id }
|
|
|
|
|
2020-12-03 22:40:33 +00:00
|
|
|
var accountName: String { "@".appending(statusService.status.displayStatus.account.acct) }
|
2020-08-21 02:29:01 +00:00
|
|
|
|
2020-10-22 22:16:06 +00:00
|
|
|
var avatarURL: URL {
|
|
|
|
if !identification.appPreferences.shouldReduceMotion,
|
|
|
|
identification.appPreferences.animateAvatars == .everywhere {
|
|
|
|
return statusService.status.displayStatus.account.avatar
|
|
|
|
} else {
|
|
|
|
return statusService.status.displayStatus.account.avatarStatic
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 07:44:01 +00:00
|
|
|
|
2020-08-21 02:29:01 +00:00
|
|
|
var time: String? { statusService.status.displayStatus.createdAt.timeAgo }
|
|
|
|
|
|
|
|
var contextParentTime: String {
|
|
|
|
Self.contextParentDateFormatter.string(from: statusService.status.displayStatus.createdAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
var applicationName: String? { statusService.status.displayStatus.application?.name }
|
|
|
|
|
|
|
|
var applicationURL: URL? {
|
|
|
|
guard let website = statusService.status.displayStatus.application?.website else { return nil }
|
|
|
|
|
|
|
|
return URL(string: website)
|
|
|
|
}
|
|
|
|
|
|
|
|
var repliesCount: Int { statusService.status.displayStatus.repliesCount }
|
|
|
|
|
|
|
|
var reblogsCount: Int { statusService.status.displayStatus.reblogsCount }
|
|
|
|
|
|
|
|
var favoritesCount: Int { statusService.status.displayStatus.favouritesCount }
|
|
|
|
|
2020-08-24 02:50:54 +00:00
|
|
|
var reblogged: Bool { statusService.status.displayStatus.reblogged }
|
2020-08-21 02:29:01 +00:00
|
|
|
|
2020-08-24 02:50:54 +00:00
|
|
|
var favorited: Bool { statusService.status.displayStatus.favourited }
|
2020-08-21 02:29:01 +00:00
|
|
|
|
2020-12-01 23:53:14 +00:00
|
|
|
var bookmarked: Bool { statusService.status.displayStatus.bookmarked }
|
|
|
|
|
2020-08-21 02:29:01 +00:00
|
|
|
var sensitive: Bool { statusService.status.displayStatus.sensitive }
|
|
|
|
|
2021-01-11 03:12:06 +00:00
|
|
|
var pinned: Bool? { statusService.status.displayStatus.pinned }
|
|
|
|
|
|
|
|
var muted: Bool { statusService.status.displayStatus.muted }
|
|
|
|
|
2020-08-21 02:29:01 +00:00
|
|
|
var sharingURL: URL? { statusService.status.displayStatus.url }
|
|
|
|
|
2020-10-25 02:31:44 +00:00
|
|
|
var isPollExpired: Bool { statusService.status.displayStatus.poll?.expired ?? true }
|
|
|
|
|
|
|
|
var hasVotedInPoll: Bool { statusService.status.displayStatus.poll?.voted ?? false }
|
|
|
|
|
|
|
|
var isPollMultipleSelection: Bool { statusService.status.displayStatus.poll?.multiple ?? false }
|
|
|
|
|
|
|
|
var pollOptions: [Poll.Option] { statusService.status.displayStatus.poll?.options ?? [] }
|
|
|
|
|
|
|
|
var pollVotersCount: Int {
|
|
|
|
guard let poll = statusService.status.displayStatus.poll else { return 0 }
|
|
|
|
|
|
|
|
return poll.votersCount ?? poll.votesCount
|
|
|
|
}
|
|
|
|
|
|
|
|
var pollOwnVotes: Set<Int> { Set(statusService.status.displayStatus.poll?.ownVotes ?? []) }
|
|
|
|
|
|
|
|
var pollTimeLeft: String? {
|
|
|
|
guard let expiresAt = statusService.status.displayStatus.poll?.expiresAt,
|
|
|
|
expiresAt > Date()
|
|
|
|
else { return nil }
|
|
|
|
|
|
|
|
return expiresAt.fullUnitTimeUntil
|
|
|
|
}
|
|
|
|
|
2020-09-29 01:14:43 +00:00
|
|
|
var cardViewModel: CardViewModel? {
|
|
|
|
if let card = statusService.status.displayStatus.card {
|
|
|
|
return CardViewModel(card: card)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-08-21 02:29:01 +00:00
|
|
|
|
|
|
|
var canBeReblogged: Bool {
|
|
|
|
switch statusService.status.displayStatus.visibility {
|
|
|
|
case .direct, .private:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-08-24 02:50:54 +00:00
|
|
|
|
2020-10-14 00:03:01 +00:00
|
|
|
func toggleShowContent() {
|
|
|
|
eventsSubject.send(
|
|
|
|
statusService.toggleShowContent()
|
2020-10-20 06:41:10 +00:00
|
|
|
.map { _ in .ignorableOutput }
|
2020-10-14 00:03:01 +00:00
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
|
|
|
func toggleShowAttachments() {
|
2020-10-07 21:06:26 +00:00
|
|
|
eventsSubject.send(
|
2020-10-14 00:03:01 +00:00
|
|
|
statusService.toggleShowAttachments()
|
2020-10-20 06:41:10 +00:00
|
|
|
.map { _ in .ignorableOutput }
|
2020-10-07 21:06:26 +00:00
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
2020-09-15 01:39:35 +00:00
|
|
|
func urlSelected(_ url: URL) {
|
|
|
|
eventsSubject.send(
|
2020-09-25 05:39:06 +00:00
|
|
|
statusService.navigationService.item(url: url)
|
2020-10-20 06:41:10 +00:00
|
|
|
.map { .navigation($0) }
|
2020-09-15 01:39:35 +00:00
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
2020-09-22 06:53:11 +00:00
|
|
|
func accountSelected() {
|
|
|
|
eventsSubject.send(
|
2020-10-20 06:41:10 +00:00
|
|
|
Just(.navigation(
|
2020-09-27 02:03:53 +00:00
|
|
|
.profile(
|
|
|
|
statusService.navigationService.profileService(
|
2020-09-27 00:22:15 +00:00
|
|
|
account: statusService.status.displayStatus.account))))
|
2020-09-22 06:53:11 +00:00
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
2020-09-28 23:23:41 +00:00
|
|
|
func rebloggedBySelected() {
|
|
|
|
eventsSubject.send(
|
2020-10-20 06:41:10 +00:00
|
|
|
Just(.navigation(.collection(statusService.rebloggedByService())))
|
2020-09-28 23:23:41 +00:00
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
2020-09-23 01:00:56 +00:00
|
|
|
func favoritedBySelected() {
|
2020-09-25 05:39:06 +00:00
|
|
|
eventsSubject.send(
|
2020-10-20 06:41:10 +00:00
|
|
|
Just(.navigation(.collection(statusService.favoritedByService())))
|
2020-09-25 05:39:06 +00:00
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.eraseToAnyPublisher())
|
2020-09-23 01:00:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 05:56:15 +00:00
|
|
|
func reply() {
|
|
|
|
let replyViewModel = Self(statusService: statusService, identification: identification)
|
|
|
|
|
|
|
|
replyViewModel.configuration = configuration.reply()
|
|
|
|
|
2021-01-11 22:45:30 +00:00
|
|
|
eventsSubject.send(
|
|
|
|
Just(.compose(inReplyTo: replyViewModel, redraft: nil))
|
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.eraseToAnyPublisher())
|
2021-01-10 05:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 01:51:52 +00:00
|
|
|
func toggleReblogged() {
|
|
|
|
eventsSubject.send(
|
|
|
|
statusService.toggleReblogged()
|
|
|
|
.map { _ in .ignorableOutput }
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
2020-08-24 02:50:54 +00:00
|
|
|
func toggleFavorited() {
|
2020-09-25 05:39:06 +00:00
|
|
|
eventsSubject.send(
|
|
|
|
statusService.toggleFavorited()
|
2020-10-20 06:41:10 +00:00
|
|
|
.map { _ in .ignorableOutput }
|
2020-09-25 05:39:06 +00:00
|
|
|
.eraseToAnyPublisher())
|
2020-09-14 23:32:34 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 23:53:14 +00:00
|
|
|
func toggleBookmarked() {
|
|
|
|
eventsSubject.send(
|
|
|
|
statusService.toggleBookmarked()
|
|
|
|
.map { _ in .ignorableOutput }
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
2021-01-11 03:12:06 +00:00
|
|
|
func togglePinned() {
|
|
|
|
eventsSubject.send(
|
|
|
|
statusService.togglePinned()
|
|
|
|
.map { _ in .ignorableOutput }
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
|
|
|
func toggleMuted() {
|
|
|
|
eventsSubject.send(
|
|
|
|
statusService.toggleMuted()
|
|
|
|
.map { _ in .ignorableOutput }
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
2021-01-11 23:40:46 +00:00
|
|
|
func confirmDelete(redraft: Bool) {
|
|
|
|
eventsSubject.send(
|
|
|
|
Just(.confirmDelete(self, redraft: redraft))
|
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
2021-01-11 22:45:30 +00:00
|
|
|
func delete() {
|
|
|
|
eventsSubject.send(
|
|
|
|
statusService.delete()
|
|
|
|
.map { _ in .ignorableOutput }
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteAndRedraft() {
|
|
|
|
let identification = self.identification
|
|
|
|
|
|
|
|
eventsSubject.send(
|
|
|
|
statusService.deleteAndRedraft()
|
|
|
|
.map { redraft, inReplyToStatusService in
|
|
|
|
let inReplyToViewModel: StatusViewModel?
|
|
|
|
|
|
|
|
if let inReplyToStatusService = inReplyToStatusService {
|
|
|
|
inReplyToViewModel = Self(
|
|
|
|
statusService: inReplyToStatusService,
|
|
|
|
identification: identification)
|
|
|
|
inReplyToViewModel?.configuration = CollectionItem.StatusConfiguration.default.reply()
|
|
|
|
} else {
|
|
|
|
inReplyToViewModel = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return .compose(inReplyTo: inReplyToViewModel, redraft: redraft)
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
2020-10-20 06:41:10 +00:00
|
|
|
func attachmentSelected(viewModel: AttachmentViewModel) {
|
|
|
|
eventsSubject.send(Just(.attachment(viewModel, self)).setFailureType(to: Error.self).eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
2020-09-14 23:32:34 +00:00
|
|
|
func shareStatus() {
|
|
|
|
guard let url = statusService.status.displayStatus.url else { return }
|
|
|
|
|
2020-10-20 06:41:10 +00:00
|
|
|
eventsSubject.send(Just(.share(url)).setFailureType(to: Error.self).eraseToAnyPublisher())
|
2020-08-24 02:50:54 +00:00
|
|
|
}
|
2020-10-25 02:31:44 +00:00
|
|
|
|
2020-11-30 02:54:11 +00:00
|
|
|
func reportStatus() {
|
|
|
|
eventsSubject.send(
|
|
|
|
Just(.report(ReportViewModel(
|
|
|
|
accountService: statusService.navigationService.accountService(
|
|
|
|
account: statusService.status.displayStatus.account),
|
|
|
|
statusService: statusService,
|
|
|
|
identification: identification)))
|
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
2020-10-25 02:31:44 +00:00
|
|
|
func vote() {
|
|
|
|
eventsSubject.send(
|
|
|
|
statusService.vote(selectedOptions: pollOptionSelections)
|
|
|
|
.map { _ in .ignorableOutput }
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshPoll() {
|
|
|
|
eventsSubject.send(
|
|
|
|
statusService.refreshPoll()
|
|
|
|
.map { _ in .ignorableOutput }
|
|
|
|
.eraseToAnyPublisher())
|
|
|
|
}
|
2020-08-21 02:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension StatusViewModel {
|
|
|
|
private static let contextParentDateFormatter: DateFormatter = {
|
|
|
|
let dateFormatter = DateFormatter()
|
|
|
|
|
|
|
|
dateFormatter.dateStyle = .short
|
|
|
|
dateFormatter.timeStyle = .short
|
|
|
|
|
|
|
|
return dateFormatter
|
|
|
|
}()
|
|
|
|
}
|