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

155 lines
4.6 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>
public let identityContext: IdentityContext
2020-09-25 07:39:06 +02:00
2020-09-23 03:00:56 +02:00
private let accountService: AccountService
2020-09-25 07:39:06 +02:00
private let eventsSubject = PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>()
2020-09-23 03:00:56 +02:00
2021-01-26 01:06:35 +01:00
init(accountService: AccountService, identityContext: IdentityContext) {
2020-09-23 03:00:56 +02:00
self.accountService = accountService
2021-01-26 01:06:35 +01:00
self.identityContext = identityContext
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 {
2021-01-26 01:06:35 +01:00
if !identityContext.appPreferences.shouldReduceMotion, identityContext.appPreferences.animateHeaders {
2020-10-23 00:16:06 +02:00
return accountService.account.header
} else {
return accountService.account.headerStatic
}
}
2020-10-15 09:44:01 +02:00
2020-12-04 04:13:18 +01:00
var isLocal: Bool { accountService.isLocal }
var domain: String? { accountService.domain }
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 }
2021-01-18 08:17:45 +01:00
var featuredTags: [FeaturedTag] { accountService.featuredTags }
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 }
2021-01-12 08:33:35 +01:00
var emojis: [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 }
2021-01-26 01:06:35 +01:00
var isSelf: Bool { accountService.account.id == identityContext.identity.account?.id }
2020-10-30 08:11:24 +01:00
2020-10-23 00:16:06 +02:00
func avatarURL(profile: Bool = false) -> URL {
2021-01-26 01:06:35 +01:00
if !identityContext.appPreferences.shouldReduceMotion,
(identityContext.appPreferences.animateAvatars == .everywhere
|| identityContext.appPreferences.animateAvatars == .profiles && profile) {
2020-10-23 00:16:06 +02:00
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 {
2021-01-26 01:06:35 +01:00
ReportViewModel(accountService: accountService, identityContext: identityContext)
2020-11-30 03:54:11 +01:00
}
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))
}
2020-12-04 04:13:18 +01:00
func domainBlock() {
ignorableOutputEvent(accountService.domainBlock())
}
func domainUnblock() {
ignorableOutputEvent(accountService.domainUnblock())
}
2020-11-17 07:46:48 +01:00
}
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
}