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

241 lines
7.5 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
2021-02-04 23:24:27 +01:00
public final class AccountViewModel: ObservableObject {
public let identityContext: IdentityContext
2021-01-26 07:57:44 +01:00
public internal(set) var configuration = CollectionItem.AccountConfiguration.withNote
2021-02-08 02:46:51 +01:00
public internal(set) var relationship: Relationship?
public internal(set) var identityProofs = [IdentityProof]()
public internal(set) var featuredTags = [FeaturedTag]()
2020-09-25 07:39:06 +02:00
2020-09-23 03:00:56 +02:00
private let accountService: AccountService
2021-02-04 23:24:27 +01:00
private let eventsSubject: PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>
2020-09-23 03:00:56 +02:00
2021-02-04 23:24:27 +01:00
init(accountService: AccountService,
identityContext: IdentityContext,
eventsSubject: PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>) {
2020-09-23 03:00:56 +02:00
self.accountService = accountService
2021-01-26 01:06:35 +01:00
self.identityContext = identityContext
2021-02-04 23:24:27 +01:00
self.eventsSubject = eventsSubject
2020-09-23 03:00:56 +02:00
}
}
public extension AccountViewModel {
2021-01-27 01:15:52 +01:00
var id: Account.Id { accountService.account.id }
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 }
2021-02-11 06:20:42 +01:00
var statusesCount: Int { accountService.account.statusesCount }
var joined: Date { accountService.account.createdAt }
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
2021-02-04 02:50:25 +01:00
|| (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
}
2021-02-11 00:41:41 +01:00
func muteViewModel() -> MuteViewModel {
MuteViewModel(accountService: accountService, identityContext: identityContext)
}
2021-03-03 01:50:22 +01:00
func lists() -> AnyPublisher<[List], Error> {
accountService.lists()
}
func addToList(id: List.Id) -> AnyPublisher<Never, Error> {
accountService.addToList(id: id)
}
func removeFromList(id: List.Id) -> AnyPublisher<Never, Error> {
accountService.removeFromList(id: id)
}
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
}
2021-02-11 04:52:21 +01:00
func confirmUnfollow() {
eventsSubject.send(Just(.confirmUnfollow(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-11-12 09:32:18 +01:00
func unfollow() {
2020-11-17 07:46:48 +01:00
ignorableOutputEvent(accountService.unfollow())
}
2021-02-11 04:52:21 +01:00
func share() {
guard let url = URL(string: accountService.account.url) else { return }
eventsSubject.send(Just(.share(url)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
func confirmHideReblogs() {
eventsSubject.send(Just(.confirmHideReblogs(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-11-17 07:46:48 +01:00
func hideReblogs() {
ignorableOutputEvent(accountService.hideReblogs())
}
2021-02-11 04:52:21 +01:00
func confirmShowReblogs() {
eventsSubject.send(Just(.confirmShowReblogs(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-11-17 07:46:48 +01:00
func showReblogs() {
ignorableOutputEvent(accountService.showReblogs())
}
2021-02-26 07:22:40 +01:00
func notify() {
ignorableOutputEvent(accountService.notify())
}
func unnotify() {
ignorableOutputEvent(accountService.unnotify())
}
2021-02-10 08:46:00 +01:00
func confirmBlock() {
eventsSubject.send(Just(.confirmBlock(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-11-17 07:46:48 +01:00
func block() {
ignorableOutputEvent(accountService.block())
}
2021-02-10 08:46:00 +01:00
func confirmUnblock() {
eventsSubject.send(Just(.confirmUnblock(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-11-17 07:46:48 +01:00
func unblock() {
ignorableOutputEvent(accountService.unblock())
}
2021-02-11 00:41:41 +01:00
func confirmMute() {
eventsSubject.send(Just(.confirmMute(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
func confirmUnmute() {
eventsSubject.send(Just(.confirmUnmute(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
2020-11-17 07:46:48 +01:00
}
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
2021-01-26 07:57:44 +01:00
func acceptFollowRequest() {
2021-01-27 01:15:52 +01:00
accountListEdit(accountService.acceptFollowRequest(), event: .acceptFollowRequest)
2021-01-26 07:57:44 +01:00
}
func rejectFollowRequest() {
2021-01-27 01:15:52 +01:00
accountListEdit(accountService.rejectFollowRequest(), event: .rejectFollowRequest)
2021-01-26 07:57:44 +01:00
}
2021-02-10 08:46:00 +01:00
func confirmDomainBlock(domain: String) {
eventsSubject.send(Just(.confirmDomainBlock(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-12-04 04:13:18 +01:00
func domainBlock() {
ignorableOutputEvent(accountService.domainBlock())
}
2021-02-10 08:46:00 +01:00
func confirmDomainUnblock(domain: String) {
eventsSubject.send(Just(.confirmDomainUnblock(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-12-04 04:13:18 +01:00
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
}
2021-01-27 01:15:52 +01:00
func accountListEdit(_ action: AnyPublisher<Never, Error>, event: CollectionItemEvent.AccountListEdit) {
eventsSubject.send(
action.collect()
.map { [weak self] _ -> CollectionItemEvent in
guard let self = self else { return .ignorableOutput }
return .accountListEdit(self, .acceptFollowRequest)
}
.eraseToAnyPublisher())
}
2020-09-23 03:00:56 +02:00
}