metatext/ViewModels/Sources/ViewModels/AccountViewModel.swift

153 lines
4.5 KiB
Swift
Raw Normal View History

2020-09-23 01:00:56 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import Foundation
import Mastodon
import ServiceLayer
2020-10-05 06:36:22 +00:00
public struct AccountViewModel: CollectionItemViewModel {
2020-09-25 05:39:06 +00:00
public let events: AnyPublisher<AnyPublisher<CollectionItemEvent, Error>, Never>
2020-09-23 01:00:56 +00:00
private let accountService: AccountService
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-09-23 01:00:56 +00:00
2020-10-15 07:44:01 +00:00
init(accountService: AccountService, identification: Identification) {
2020-09-23 01:00:56 +00:00
self.accountService = accountService
2020-10-15 07:44:01 +00:00
self.identification = identification
2020-09-25 05:39:06 +00:00
events = eventsSubject.eraseToAnyPublisher()
2020-09-23 01:00:56 +00:00
}
}
public extension AccountViewModel {
2020-10-22 22:16:06 +00:00
var headerURL: URL {
if !identification.appPreferences.shouldReduceMotion, identification.appPreferences.animateHeaders {
return accountService.account.header
} else {
return accountService.account.headerStatic
}
}
2020-10-15 07:44:01 +00:00
2020-12-04 03:13:18 +00:00
var isLocal: Bool { accountService.isLocal }
var domain: String? { accountService.domain }
2020-10-30 07:11:24 +00:00
var displayName: String {
accountService.account.displayName.isEmpty ? accountService.account.acct : accountService.account.displayName
}
2020-09-26 00:52:59 +00:00
var accountName: String { "@".appending(accountService.account.acct) }
2020-11-11 00:53:31 +00:00
var isLocked: Bool { accountService.account.locked }
2020-11-12 08:32:18 +00:00
var relationship: Relationship? { accountService.relationship }
var identityProofs: [IdentityProof] { accountService.identityProofs }
2020-11-10 06:27:08 +00:00
var fields: [Account.Field] { accountService.account.fields }
2020-09-26 00:52:59 +00:00
var note: NSAttributedString { accountService.account.note.attributed }
var emoji: [Emoji] { accountService.account.emojis }
2020-09-26 00:57:35 +00:00
2020-12-03 01:06:46 +00:00
var followingCount: Int { accountService.account.followingCount }
var followersCount: Int { accountService.account.followersCount }
2020-10-30 07:11:24 +00:00
var isSelf: Bool { accountService.account.id == identification.identity.account?.id }
2020-10-22 22:16:06 +00: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 00:57:35 +00:00
func urlSelected(_ url: URL) {
eventsSubject.send(
accountService.navigationService.item(url: url)
.map { CollectionItemEvent.navigation($0) }
.setFailureType(to: Error.self)
.eraseToAnyPublisher())
}
2020-11-12 08:32:18 +00:00
2020-12-03 01:06:46 +00:00
func followingSelected() {
eventsSubject.send(
Just(.navigation(.collection(accountService.followingService())))
.setFailureType(to: Error.self)
.eraseToAnyPublisher())
}
func followersSelected() {
eventsSubject.send(
Just(.navigation(.collection(accountService.followersService())))
.setFailureType(to: Error.self)
.eraseToAnyPublisher())
}
2020-11-30 02:54:11 +00:00
func reportViewModel() -> ReportViewModel {
ReportViewModel(accountService: accountService, identification: identification)
}
2020-11-12 08:32:18 +00:00
func follow() {
2020-11-17 06:46:48 +00:00
ignorableOutputEvent(accountService.follow())
2020-11-12 08:32:18 +00:00
}
func unfollow() {
2020-11-17 06:46:48 +00:00
ignorableOutputEvent(accountService.unfollow())
}
func hideReblogs() {
ignorableOutputEvent(accountService.hideReblogs())
}
func showReblogs() {
ignorableOutputEvent(accountService.showReblogs())
}
func block() {
ignorableOutputEvent(accountService.block())
}
func unblock() {
ignorableOutputEvent(accountService.unblock())
}
func mute() {
ignorableOutputEvent(accountService.mute())
}
func unmute() {
ignorableOutputEvent(accountService.unmute())
}
func pin() {
ignorableOutputEvent(accountService.pin())
}
func unpin() {
ignorableOutputEvent(accountService.unpin())
}
func set(note: String) {
ignorableOutputEvent(accountService.set(note: note))
}
2020-12-04 03:13:18 +00:00
func domainBlock() {
ignorableOutputEvent(accountService.domainBlock())
}
func domainUnblock() {
ignorableOutputEvent(accountService.domainUnblock())
}
2020-11-17 06:46:48 +00:00
}
private extension AccountViewModel {
func ignorableOutputEvent(_ action: AnyPublisher<Never, Error>) {
eventsSubject.send(action.map { _ in .ignorableOutput }.eraseToAnyPublisher())
2020-11-12 08:32:18 +00:00
}
2020-09-23 01:00:56 +00:00
}