2020-09-18 02:16:41 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
import Mastodon
|
|
|
|
import ServiceLayer
|
|
|
|
|
2020-10-01 04:35:06 +02:00
|
|
|
final public class ProfileViewModel {
|
2020-09-26 09:13:50 +02:00
|
|
|
@Published public private(set) var accountViewModel: AccountViewModel?
|
2020-09-27 04:03:53 +02:00
|
|
|
@Published public var collection = ProfileCollection.statuses
|
2020-10-01 04:35:06 +02:00
|
|
|
@Published public var alertItem: AlertItem?
|
2020-10-23 00:16:06 +02:00
|
|
|
public let imagePresentations: AnyPublisher<URL, Never>
|
2020-10-01 04:35:06 +02:00
|
|
|
|
2020-09-27 04:03:53 +02:00
|
|
|
private let profileService: ProfileService
|
2020-10-05 22:06:50 +02:00
|
|
|
private let collectionViewModel: CurrentValueSubject<CollectionItemsViewModel, Never>
|
2020-10-23 00:16:06 +02:00
|
|
|
private let imagePresentationsSubject = PassthroughSubject<URL, Never>()
|
2020-09-18 02:16:41 +02:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2020-10-07 02:31:29 +02:00
|
|
|
public init(profileService: ProfileService, identification: Identification) {
|
2020-09-27 04:03:53 +02:00
|
|
|
self.profileService = profileService
|
2020-10-23 00:16:06 +02:00
|
|
|
imagePresentations = imagePresentationsSubject.eraseToAnyPublisher()
|
2020-09-18 02:16:41 +02:00
|
|
|
|
2020-10-01 04:35:06 +02:00
|
|
|
collectionViewModel = CurrentValueSubject(
|
2020-10-07 02:31:29 +02:00
|
|
|
CollectionItemsViewModel(
|
|
|
|
collectionService: profileService.timelineService(profileCollection: .statuses),
|
|
|
|
identification: identification))
|
2020-09-22 08:53:11 +02:00
|
|
|
|
2020-10-01 04:35:06 +02:00
|
|
|
profileService.accountServicePublisher
|
2020-10-15 09:44:01 +02:00
|
|
|
.map { AccountViewModel(accountService: $0, identification: identification) }
|
2020-09-22 08:53:11 +02:00
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
2020-09-26 09:13:50 +02:00
|
|
|
.assign(to: &$accountViewModel)
|
2020-10-01 04:35:06 +02:00
|
|
|
|
|
|
|
$collection.dropFirst()
|
2020-10-05 09:04:15 +02:00
|
|
|
.map(profileService.timelineService(profileCollection:))
|
2020-10-07 02:31:29 +02:00
|
|
|
.map { CollectionItemsViewModel(collectionService: $0, identification: identification) }
|
2020-10-01 04:35:06 +02: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 02:16:41 +02:00
|
|
|
}
|
2020-10-01 04:35:06 +02:00
|
|
|
}
|
2020-09-18 02:16:41 +02:00
|
|
|
|
2020-10-23 00:16:06 +02:00
|
|
|
public extension ProfileViewModel {
|
|
|
|
func presentHeader() {
|
|
|
|
guard let accountViewModel = accountViewModel else { return }
|
|
|
|
|
|
|
|
imagePresentationsSubject.send(accountViewModel.headerURL)
|
|
|
|
}
|
2020-11-10 07:27:08 +01:00
|
|
|
|
|
|
|
func presentAvatar() {
|
|
|
|
guard let accountViewModel = accountViewModel else { return }
|
|
|
|
|
|
|
|
imagePresentationsSubject.send(accountViewModel.avatarURL(profile: true))
|
|
|
|
}
|
2020-11-11 08:31:56 +01:00
|
|
|
|
|
|
|
func fetchProfile() -> AnyPublisher<Never, Never> {
|
|
|
|
profileService.fetchProfile().assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
|
|
}
|
2020-10-23 00:16:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 04:35:06 +02:00
|
|
|
extension ProfileViewModel: CollectionViewModel {
|
2020-10-07 23:06:26 +02:00
|
|
|
public var updates: AnyPublisher<CollectionUpdate, Never> {
|
|
|
|
collectionViewModel.flatMap(\.updates).eraseToAnyPublisher()
|
2020-10-01 04:35:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-05 22:21:06 +02:00
|
|
|
public var title: AnyPublisher<String, Never> {
|
|
|
|
$accountViewModel.compactMap { $0?.accountName }.eraseToAnyPublisher()
|
2020-10-01 04:35:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 02:41:22 +01:00
|
|
|
public var titleLocalizationComponents: AnyPublisher<[String], Never> {
|
|
|
|
collectionViewModel.flatMap(\.titleLocalizationComponents).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-10-14 02:03:01 +02:00
|
|
|
public var expandAll: AnyPublisher<ExpandAllState, Never> {
|
2020-11-17 07:46:48 +01:00
|
|
|
Empty().eraseToAnyPublisher()
|
2020-10-07 23:06:26 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 04:35:06 +02:00
|
|
|
public var alertItems: AnyPublisher<AlertItem, Never> {
|
|
|
|
collectionViewModel.flatMap(\.alertItems).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
public var loading: AnyPublisher<Bool, Never> {
|
|
|
|
collectionViewModel.flatMap(\.loading).eraseToAnyPublisher()
|
2020-09-27 03:23:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 02:31:29 +02:00
|
|
|
public var events: AnyPublisher<CollectionItemEvent, Never> {
|
2020-09-26 09:45:39 +02:00
|
|
|
$accountViewModel.compactMap { $0 }
|
|
|
|
.flatMap(\.events)
|
|
|
|
.flatMap { $0 }
|
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
2020-10-07 02:31:29 +02:00
|
|
|
.merge(with: collectionViewModel.flatMap(\.events))
|
2020-09-26 09:45:39 +02:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-10-06 00:50:05 +02:00
|
|
|
public var nextPageMaxId: String? {
|
|
|
|
collectionViewModel.value.nextPageMaxId
|
2021-01-16 20:41:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public var preferLastPresentIdOverNextPageMaxId: Bool {
|
|
|
|
collectionViewModel.value.preferLastPresentIdOverNextPageMaxId
|
2020-10-01 04:35:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-16 21:06:35 +01:00
|
|
|
public var canRefresh: Bool { collectionViewModel.value.canRefresh }
|
|
|
|
|
2020-10-06 00:50:05 +02:00
|
|
|
public func request(maxId: String?, minId: String?) {
|
|
|
|
if case .statuses = collection, maxId == nil {
|
2020-09-27 04:03:53 +02:00
|
|
|
profileService.fetchPinnedStatuses()
|
2020-09-18 02:16:41 +02:00
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
|
|
.sink { _ in }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
|
2020-10-06 00:50:05 +02:00
|
|
|
collectionViewModel.value.request(maxId: maxId, minId: minId)
|
2020-09-18 02:16:41 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 01:12:11 +02:00
|
|
|
public func viewedAtTop(indexPath: IndexPath) {
|
|
|
|
collectionViewModel.value.viewedAtTop(indexPath: indexPath)
|
|
|
|
}
|
|
|
|
|
2020-10-05 09:50:59 +02:00
|
|
|
public func select(indexPath: IndexPath) {
|
|
|
|
collectionViewModel.value.select(indexPath: indexPath)
|
2020-10-01 04:35:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-05 09:50:59 +02:00
|
|
|
public func canSelect(indexPath: IndexPath) -> Bool {
|
|
|
|
collectionViewModel.value.canSelect(indexPath: indexPath)
|
2020-10-01 04:35:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-05 09:50:59 +02:00
|
|
|
public func viewModel(indexPath: IndexPath) -> CollectionItemViewModel {
|
|
|
|
collectionViewModel.value.viewModel(indexPath: indexPath)
|
2020-09-23 03:43:06 +02:00
|
|
|
}
|
2020-10-07 23:06:26 +02:00
|
|
|
|
2020-10-14 02:03:01 +02:00
|
|
|
public func toggleExpandAll() {
|
|
|
|
collectionViewModel.value.toggleExpandAll()
|
2020-10-07 23:06:26 +02:00
|
|
|
}
|
2020-09-18 02:16:41 +02:00
|
|
|
}
|