2020-09-18 00:16:41 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
import Mastodon
|
|
|
|
import ServiceLayer
|
|
|
|
|
2020-10-01 02:35:06 +00:00
|
|
|
final public class ProfileViewModel {
|
2020-09-26 07:13:50 +00:00
|
|
|
@Published public private(set) var accountViewModel: AccountViewModel?
|
2020-09-27 02:03:53 +00:00
|
|
|
@Published public var collection = ProfileCollection.statuses
|
2020-10-01 02:35:06 +00:00
|
|
|
@Published public var alertItem: AlertItem?
|
2020-10-22 22:16:06 +00:00
|
|
|
public let imagePresentations: AnyPublisher<URL, Never>
|
2020-10-01 02:35:06 +00:00
|
|
|
|
2020-09-27 02:03:53 +00:00
|
|
|
private let profileService: ProfileService
|
2020-10-05 20:06:50 +00:00
|
|
|
private let collectionViewModel: CurrentValueSubject<CollectionItemsViewModel, Never>
|
2021-02-04 22:24:27 +00:00
|
|
|
private let accountEventsSubject: PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>
|
2020-10-22 22:16:06 +00:00
|
|
|
private let imagePresentationsSubject = PassthroughSubject<URL, Never>()
|
2020-09-18 00:16:41 +00:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2021-01-26 00:06:35 +00:00
|
|
|
public init(profileService: ProfileService, identityContext: IdentityContext) {
|
2020-09-27 02:03:53 +00:00
|
|
|
self.profileService = profileService
|
2020-10-22 22:16:06 +00:00
|
|
|
imagePresentations = imagePresentationsSubject.eraseToAnyPublisher()
|
2020-09-18 00:16:41 +00:00
|
|
|
|
2020-10-01 02:35:06 +00:00
|
|
|
collectionViewModel = CurrentValueSubject(
|
2020-10-07 00:31:29 +00:00
|
|
|
CollectionItemsViewModel(
|
|
|
|
collectionService: profileService.timelineService(profileCollection: .statuses),
|
2021-01-26 00:06:35 +00:00
|
|
|
identityContext: identityContext))
|
2020-09-22 06:53:11 +00:00
|
|
|
|
2021-02-04 22:24:27 +00:00
|
|
|
let accountEventsSubject = PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, Never>()
|
|
|
|
|
|
|
|
self.accountEventsSubject = accountEventsSubject
|
|
|
|
|
2021-02-08 01:46:51 +00:00
|
|
|
profileService.profilePublisher
|
2021-02-04 22:24:27 +00:00
|
|
|
.map {
|
2021-02-08 01:46:51 +00:00
|
|
|
let vm = AccountViewModel(accountService: identityContext.service
|
|
|
|
.navigationService
|
|
|
|
.accountService(account: $0.account),
|
2021-02-04 22:24:27 +00:00
|
|
|
identityContext: identityContext,
|
|
|
|
eventsSubject: accountEventsSubject)
|
2021-02-08 01:46:51 +00:00
|
|
|
|
|
|
|
vm.relationship = $0.relationship
|
|
|
|
vm.identityProofs = $0.identityProofs
|
|
|
|
vm.featuredTags = $0.featuredTags
|
|
|
|
|
|
|
|
return vm
|
2021-02-04 22:24:27 +00:00
|
|
|
}
|
2020-09-22 06:53:11 +00:00
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
2020-09-26 07:13:50 +00:00
|
|
|
.assign(to: &$accountViewModel)
|
2020-10-01 02:35:06 +00:00
|
|
|
|
|
|
|
$collection.dropFirst()
|
2020-10-05 07:04:15 +00:00
|
|
|
.map(profileService.timelineService(profileCollection:))
|
2021-01-26 00:06:35 +00:00
|
|
|
.map { CollectionItemsViewModel(collectionService: $0, identityContext: identityContext) }
|
2020-10-01 02:35:06 +00:00
|
|
|
.sink { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
|
|
|
self.collectionViewModel.send($0)
|
|
|
|
$0.$alertItem.assign(to: &self.$alertItem)
|
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
2020-09-18 00:16:41 +00:00
|
|
|
}
|
2020-10-01 02:35:06 +00:00
|
|
|
}
|
2020-09-18 00:16:41 +00:00
|
|
|
|
2020-10-22 22:16:06 +00:00
|
|
|
public extension ProfileViewModel {
|
|
|
|
func presentHeader() {
|
|
|
|
guard let accountViewModel = accountViewModel else { return }
|
|
|
|
|
|
|
|
imagePresentationsSubject.send(accountViewModel.headerURL)
|
|
|
|
}
|
2020-11-10 06:27:08 +00:00
|
|
|
|
|
|
|
func presentAvatar() {
|
|
|
|
guard let accountViewModel = accountViewModel else { return }
|
|
|
|
|
|
|
|
imagePresentationsSubject.send(accountViewModel.avatarURL(profile: true))
|
|
|
|
}
|
2020-11-11 07:31:56 +00:00
|
|
|
|
|
|
|
func fetchProfile() -> AnyPublisher<Never, Never> {
|
|
|
|
profileService.fetchProfile().assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
|
|
}
|
2020-10-22 22:16:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 02:35:06 +00:00
|
|
|
extension ProfileViewModel: CollectionViewModel {
|
2021-01-26 00:45:18 +00:00
|
|
|
public var identityContext: IdentityContext {
|
|
|
|
collectionViewModel.value.identityContext
|
|
|
|
}
|
|
|
|
|
2020-10-07 21:06:26 +00:00
|
|
|
public var updates: AnyPublisher<CollectionUpdate, Never> {
|
2021-01-31 00:38:56 +00:00
|
|
|
collectionViewModel.flatMap(\.updates)
|
|
|
|
.combineLatest($accountViewModel.map { $0?.relationship })
|
|
|
|
.map {
|
|
|
|
let (updates, relationship) = $0
|
|
|
|
|
|
|
|
if let relationship = relationship, relationship.blockedBy {
|
|
|
|
return .empty
|
|
|
|
} else {
|
|
|
|
return updates
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
2020-10-01 02:35:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 20:21:06 +00:00
|
|
|
public var title: AnyPublisher<String, Never> {
|
|
|
|
$accountViewModel.compactMap { $0?.accountName }.eraseToAnyPublisher()
|
2020-10-01 02:35:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 01:41:22 +00:00
|
|
|
public var titleLocalizationComponents: AnyPublisher<[String], Never> {
|
|
|
|
collectionViewModel.flatMap(\.titleLocalizationComponents).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-10-14 00:03:01 +00:00
|
|
|
public var expandAll: AnyPublisher<ExpandAllState, Never> {
|
2020-11-17 06:46:48 +00:00
|
|
|
Empty().eraseToAnyPublisher()
|
2020-10-07 21:06:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 02:35:06 +00:00
|
|
|
public var alertItems: AnyPublisher<AlertItem, Never> {
|
|
|
|
collectionViewModel.flatMap(\.alertItems).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
public var loading: AnyPublisher<Bool, Never> {
|
|
|
|
collectionViewModel.flatMap(\.loading).eraseToAnyPublisher()
|
2020-09-27 01:23:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 00:31:29 +00:00
|
|
|
public var events: AnyPublisher<CollectionItemEvent, Never> {
|
2021-02-04 22:24:27 +00:00
|
|
|
accountEventsSubject
|
|
|
|
.flatMap { [weak self] eventPublisher -> AnyPublisher<CollectionItemEvent, Never> in
|
|
|
|
guard let self = self else { return Empty().eraseToAnyPublisher() }
|
|
|
|
|
|
|
|
return eventPublisher.assignErrorsToAlertItem(to: \.alertItem, on: self).eraseToAnyPublisher()
|
|
|
|
}
|
2020-10-07 00:31:29 +00:00
|
|
|
.merge(with: collectionViewModel.flatMap(\.events))
|
2020-09-26 07:45:39 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2021-02-09 01:19:25 +00:00
|
|
|
public var searchScopeChanges: AnyPublisher<SearchScope, Never> {
|
|
|
|
collectionViewModel.flatMap(\.searchScopeChanges).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-10-05 22:50:05 +00:00
|
|
|
public var nextPageMaxId: String? {
|
|
|
|
collectionViewModel.value.nextPageMaxId
|
2021-01-16 19:41:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 20:06:35 +00:00
|
|
|
public var canRefresh: Bool { collectionViewModel.value.canRefresh }
|
|
|
|
|
2021-01-23 03:48:33 +00:00
|
|
|
public func request(maxId: String?, minId: String?, search: Search?) {
|
2020-10-05 22:50:05 +00:00
|
|
|
if case .statuses = collection, maxId == nil {
|
2020-09-27 02:03:53 +00:00
|
|
|
profileService.fetchPinnedStatuses()
|
2020-09-18 00:16:41 +00:00
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
|
|
.sink { _ in }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
|
2021-01-23 03:48:33 +00:00
|
|
|
collectionViewModel.value.request(maxId: maxId, minId: minId, search: nil)
|
2020-09-18 00:16:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 02:10:41 +00:00
|
|
|
public func requestNextPage(fromIndexPath indexPath: IndexPath) {
|
|
|
|
collectionViewModel.value.requestNextPage(fromIndexPath: indexPath)
|
|
|
|
}
|
|
|
|
|
2020-10-06 23:12:11 +00:00
|
|
|
public func viewedAtTop(indexPath: IndexPath) {
|
|
|
|
collectionViewModel.value.viewedAtTop(indexPath: indexPath)
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
public func select(indexPath: IndexPath) {
|
|
|
|
collectionViewModel.value.select(indexPath: indexPath)
|
2020-10-01 02:35:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
public func canSelect(indexPath: IndexPath) -> Bool {
|
|
|
|
collectionViewModel.value.canSelect(indexPath: indexPath)
|
2020-10-01 02:35:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 22:24:27 +00:00
|
|
|
public func viewModel(indexPath: IndexPath) -> Any {
|
2020-10-05 07:50:59 +00:00
|
|
|
collectionViewModel.value.viewModel(indexPath: indexPath)
|
2020-09-23 01:43:06 +00:00
|
|
|
}
|
2020-10-07 21:06:26 +00:00
|
|
|
|
2020-10-14 00:03:01 +00:00
|
|
|
public func toggleExpandAll() {
|
|
|
|
collectionViewModel.value.toggleExpandAll()
|
2020-10-07 21:06:26 +00:00
|
|
|
}
|
2021-01-27 00:15:52 +00:00
|
|
|
|
|
|
|
public func applyAccountListEdit(viewModel: AccountViewModel, edit: CollectionItemEvent.AccountListEdit) {
|
|
|
|
collectionViewModel.value.applyAccountListEdit(viewModel: viewModel, edit: edit)
|
|
|
|
}
|
2020-09-18 00:16:41 +00:00
|
|
|
}
|