metatext/ViewModels/Sources/ViewModels/View Models/AccountViewModel.swift

240 lines
7.4 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
2021-02-04 22:24:27 +00:00
public final class AccountViewModel: ObservableObject {
public let identityContext: IdentityContext
2021-01-26 06:57:44 +00:00
public internal(set) var configuration = CollectionItem.AccountConfiguration.withNote
2021-02-08 01:46:51 +00:00
public internal(set) var relationship: Relationship?
public internal(set) var identityProofs = [IdentityProof]()
public internal(set) var featuredTags = [FeaturedTag]()
2020-09-25 05:39:06 +00:00
2020-09-23 01:00:56 +00:00
private let accountService: AccountService
2021-02-04 22:24:27 +00:00
private let eventsSubject: PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>
2020-09-23 01:00:56 +00:00
2021-02-04 22:24:27 +00:00
init(accountService: AccountService,
identityContext: IdentityContext,
eventsSubject: PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>) {
2020-09-23 01:00:56 +00:00
self.accountService = accountService
2021-01-26 00:06:35 +00:00
self.identityContext = identityContext
2021-02-04 22:24:27 +00:00
self.eventsSubject = eventsSubject
2020-09-23 01:00:56 +00:00
}
}
public extension AccountViewModel {
2021-01-27 00:15:52 +00:00
var id: Account.Id { accountService.account.id }
2020-10-22 22:16:06 +00:00
var headerURL: URL {
2021-03-06 02:25:18 +00:00
if identityContext.appPreferences.animateHeaders {
2020-10-22 22:16:06 +00:00
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 }
2021-02-11 05:20:42 +00:00
var statusesCount: Int { accountService.account.statusesCount }
var joined: Date { accountService.account.createdAt }
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 }
2021-01-12 07:33:35 +00:00
var emojis: [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 }
2021-01-26 00:06:35 +00:00
var isSelf: Bool { accountService.account.id == identityContext.identity.account?.id }
2020-10-30 07:11:24 +00:00
2020-10-22 22:16:06 +00:00
func avatarURL(profile: Bool = false) -> URL {
2021-03-06 02:25:18 +00:00
if identityContext.appPreferences.animateAvatars == .everywhere
|| (identityContext.appPreferences.animateAvatars == .profiles && profile) {
2020-10-22 22:16:06 +00:00
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 {
2021-01-26 00:06:35 +00:00
ReportViewModel(accountService: accountService, identityContext: identityContext)
2020-11-30 02:54:11 +00:00
}
2021-02-10 23:41:41 +00:00
func muteViewModel() -> MuteViewModel {
MuteViewModel(accountService: accountService, identityContext: identityContext)
}
2021-03-03 00:50:22 +00: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 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
}
2021-02-11 03:52:21 +00:00
func confirmUnfollow() {
eventsSubject.send(Just(.confirmUnfollow(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-11-12 08:32:18 +00:00
func unfollow() {
2020-11-17 06:46:48 +00:00
ignorableOutputEvent(accountService.unfollow())
}
2021-02-11 03:52:21 +00: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 06:46:48 +00:00
func hideReblogs() {
ignorableOutputEvent(accountService.hideReblogs())
}
2021-02-11 03:52:21 +00:00
func confirmShowReblogs() {
eventsSubject.send(Just(.confirmShowReblogs(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-11-17 06:46:48 +00:00
func showReblogs() {
ignorableOutputEvent(accountService.showReblogs())
}
2021-02-26 06:22:40 +00:00
func notify() {
ignorableOutputEvent(accountService.notify())
}
func unnotify() {
ignorableOutputEvent(accountService.unnotify())
}
2021-02-10 07:46:00 +00:00
func confirmBlock() {
eventsSubject.send(Just(.confirmBlock(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-11-17 06:46:48 +00:00
func block() {
ignorableOutputEvent(accountService.block())
}
2021-02-10 07:46:00 +00:00
func confirmUnblock() {
eventsSubject.send(Just(.confirmUnblock(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-11-17 06:46:48 +00:00
func unblock() {
ignorableOutputEvent(accountService.unblock())
}
2021-02-10 23:41:41 +00: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 06:46:48 +00: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 03:13:18 +00:00
2021-01-26 06:57:44 +00:00
func acceptFollowRequest() {
2021-01-27 00:15:52 +00:00
accountListEdit(accountService.acceptFollowRequest(), event: .acceptFollowRequest)
2021-01-26 06:57:44 +00:00
}
func rejectFollowRequest() {
2021-01-27 00:15:52 +00:00
accountListEdit(accountService.rejectFollowRequest(), event: .rejectFollowRequest)
2021-01-26 06:57:44 +00:00
}
2021-02-10 07:46:00 +00:00
func confirmDomainBlock(domain: String) {
eventsSubject.send(Just(.confirmDomainBlock(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-12-04 03:13:18 +00:00
func domainBlock() {
ignorableOutputEvent(accountService.domainBlock())
}
2021-02-10 07:46:00 +00:00
func confirmDomainUnblock(domain: String) {
eventsSubject.send(Just(.confirmDomainUnblock(self)).setFailureType(to: Error.self).eraseToAnyPublisher())
}
2020-12-04 03:13:18 +00:00
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
}
2021-01-27 00:15:52 +00: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 01:00:56 +00:00
}