metatext/ViewModels/Sources/ViewModels/ProfileViewModel.swift

110 lines
3.9 KiB
Swift
Raw Normal View History

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-09-27 02:03:53 +00:00
private let profileService: ProfileService
2020-10-01 02:35:06 +00:00
private let collectionViewModel: CurrentValueSubject<StatusListViewModel, Never>
2020-09-18 00:16:41 +00:00
private var cancellables = Set<AnyCancellable>()
2020-09-27 02:03:53 +00:00
init(profileService: ProfileService) {
self.profileService = profileService
2020-09-18 00:16:41 +00:00
2020-10-01 02:35:06 +00:00
collectionViewModel = CurrentValueSubject(
StatusListViewModel(statusListService: profileService.statusListService(profileCollection: .statuses)))
2020-09-22 06:53:11 +00:00
2020-10-01 02:35:06 +00:00
profileService.accountServicePublisher
2020-09-26 07:13:50 +00:00
.map(AccountViewModel.init(accountService:))
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()
.map(profileService.statusListService(profileCollection:))
.map(StatusListViewModel.init(statusListService:))
.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-01 02:35:06 +00:00
extension ProfileViewModel: CollectionViewModel {
public var collectionItems: AnyPublisher<[[CollectionItem]], Never> {
collectionViewModel.flatMap(\.collectionItems).map {
$0.enumerated().map { [weak self] in
if let self = self, self.collection == .statuses, $0 == 0 {
// The pinned key is added to the info of collection items in the first section
// so a diffable data source can potentially render it in both sections
return $1.map { .init(id: $0.id, kind: $0.kind, info: [.pinned: true]) }
} else {
return $1
2020-09-27 01:23:56 +00:00
}
}
2020-10-01 02:35:06 +00:00
}.eraseToAnyPublisher()
}
public var title: AnyPublisher<String?, Never> {
$accountViewModel.map { $0?.accountName }.eraseToAnyPublisher()
}
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-01 02:35:06 +00:00
public var navigationEvents: AnyPublisher<NavigationEvent, Never> {
2020-09-26 07:45:39 +00:00
$accountViewModel.compactMap { $0 }
.flatMap(\.events)
.flatMap { $0 }
.map(NavigationEvent.init)
.compactMap { $0 }
.assignErrorsToAlertItem(to: \.alertItem, on: self)
2020-10-01 02:35:06 +00:00
.merge(with: collectionViewModel.flatMap(\.navigationEvents))
2020-09-26 07:45:39 +00:00
.eraseToAnyPublisher()
}
2020-10-01 02:35:06 +00:00
public var nextPageMaxID: String? {
collectionViewModel.value.nextPageMaxID
}
public var maintainScrollPositionOfItem: CollectionItem? {
collectionViewModel.value.maintainScrollPositionOfItem
}
public func request(maxID: String?, minID: String?) {
2020-09-18 00:16:41 +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)
}
2020-10-01 02:35:06 +00:00
collectionViewModel.value.request(maxID: maxID, minID: minID)
2020-09-18 00:16:41 +00:00
}
2020-10-01 02:35:06 +00:00
public func itemSelected(_ item: CollectionItem) {
collectionViewModel.value.itemSelected(item)
}
public func canSelect(item: CollectionItem) -> Bool {
collectionViewModel.value.canSelect(item: item)
}
public func viewModel(item: CollectionItem) -> Any? {
collectionViewModel.value.viewModel(item: item)
2020-09-23 01:43:06 +00:00
}
2020-09-18 00:16:41 +00:00
}