metatext-app-ios-iphone-ipad/ViewModels/Sources/ViewModels/AccountViewModel.swift

61 lines
2.1 KiB
Swift
Raw Normal View History

2020-09-23 03:00:56 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import Foundation
import Mastodon
import ServiceLayer
2020-10-05 08:36:22 +02:00
public struct AccountViewModel: CollectionItemViewModel {
2020-09-25 07:39:06 +02:00
public let events: AnyPublisher<AnyPublisher<CollectionItemEvent, Error>, Never>
2020-09-23 03:00:56 +02:00
private let accountService: AccountService
2020-10-23 00:16:06 +02:00
private let identification: Identification
2020-09-25 07:39:06 +02:00
private let eventsSubject = PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>()
2020-09-23 03:00:56 +02:00
2020-10-15 09:44:01 +02:00
init(accountService: AccountService, identification: Identification) {
2020-09-23 03:00:56 +02:00
self.accountService = accountService
2020-10-15 09:44:01 +02:00
self.identification = identification
2020-09-25 07:39:06 +02:00
events = eventsSubject.eraseToAnyPublisher()
2020-09-23 03:00:56 +02:00
}
}
public extension AccountViewModel {
2020-10-23 00:16:06 +02:00
var headerURL: URL {
if !identification.appPreferences.shouldReduceMotion, identification.appPreferences.animateHeaders {
return accountService.account.header
} else {
return accountService.account.headerStatic
}
}
2020-10-15 09:44:01 +02:00
2020-10-30 08:11:24 +01:00
var displayName: String {
accountService.account.displayName.isEmpty ? accountService.account.acct : accountService.account.displayName
}
2020-09-26 02:52:59 +02:00
var accountName: String { "@".appending(accountService.account.acct) }
var note: NSAttributedString { accountService.account.note.attributed }
var emoji: [Emoji] { accountService.account.emojis }
2020-09-26 02:57:35 +02:00
2020-10-30 08:11:24 +01:00
var isSelf: Bool { accountService.account.id == identification.identity.account?.id }
2020-10-23 00:16:06 +02:00
func avatarURL(profile: Bool = false) -> URL {
if !identification.appPreferences.shouldReduceMotion,
(identification.appPreferences.animateAvatars == .everywhere
|| identification.appPreferences.animateAvatars == .profiles && profile) {
return accountService.account.avatar
} else {
return accountService.account.avatarStatic
}
}
2020-09-26 02:57:35 +02:00
func urlSelected(_ url: URL) {
eventsSubject.send(
accountService.navigationService.item(url: url)
.map { CollectionItemEvent.navigation($0) }
.setFailureType(to: Error.self)
.eraseToAnyPublisher())
}
2020-09-23 03:00:56 +02:00
}