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

141 lines
4.2 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) }
2020-11-11 01:53:31 +01:00
var isLocked: Bool { accountService.account.locked }
2020-11-12 09:32:18 +01:00
var relationship: Relationship? { accountService.relationship }
var identityProofs: [IdentityProof] { accountService.identityProofs }
2020-11-10 07:27:08 +01:00
var fields: [Account.Field] { accountService.account.fields }
2020-09-26 02:52:59 +02:00
var note: NSAttributedString { accountService.account.note.attributed }
var emoji: [Emoji] { accountService.account.emojis }
2020-09-26 02:57:35 +02:00
2020-12-03 02:06:46 +01:00
var followingCount: Int { accountService.account.followingCount }
var followersCount: Int { accountService.account.followersCount }
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-11-12 09:32:18 +01:00
2020-12-03 02:06:46 +01: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 03:54:11 +01:00
func reportViewModel() -> ReportViewModel {
ReportViewModel(accountService: accountService, identification: identification)
}
2020-11-12 09:32:18 +01:00
func follow() {
2020-11-17 07:46:48 +01:00
ignorableOutputEvent(accountService.follow())
2020-11-12 09:32:18 +01:00
}
func unfollow() {
2020-11-17 07:46:48 +01: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))
}
}
private extension AccountViewModel {
func ignorableOutputEvent(_ action: AnyPublisher<Never, Error>) {
eventsSubject.send(action.map { _ in .ignorableOutput }.eraseToAnyPublisher())
2020-11-12 09:32:18 +01:00
}
2020-09-23 03:00:56 +02:00
}