metatext-app-ios-iphone-ipad/ViewModels/Sources/ViewModels/AccountStatusesViewModel.swift

70 lines
2.4 KiB
Swift
Raw Normal View History

2020-09-18 02:16:41 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import Foundation
import Mastodon
import ServiceLayer
public class AccountStatusesViewModel: StatusListViewModel {
2020-09-26 09:13:50 +02:00
@Published public private(set) var accountViewModel: AccountViewModel?
2020-09-22 08:53:11 +02:00
@Published public var collection = AccountStatusCollection.statuses
2020-09-18 02:16:41 +02:00
private let accountStatusesService: AccountStatusesService
private var cancellables = Set<AnyCancellable>()
init(accountStatusesService: AccountStatusesService) {
self.accountStatusesService = accountStatusesService
2020-09-22 08:53:11 +02:00
let collectionSubject = CurrentValueSubject<AccountStatusCollection, Never>(.statuses)
2020-09-18 02:16:41 +02:00
super.init(
statusListService: accountStatusesService.statusListService(
2020-09-22 08:53:11 +02:00
collectionPublisher: collectionSubject))
$collection.sink(receiveValue: collectionSubject.send).store(in: &cancellables)
accountStatusesService.accountService
2020-09-26 09:13:50 +02:00
.map(AccountViewModel.init(accountService:))
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-09-18 02:16:41 +02:00
}
2020-09-26 09:45:39 +02:00
public override var navigationEvents: AnyPublisher<NavigationEvent, Never> {
$accountViewModel.compactMap { $0 }
.flatMap(\.events)
.flatMap { $0 }
.map(NavigationEvent.init)
.compactMap { $0 }
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.merge(with: super.navigationEvents)
.eraseToAnyPublisher()
}
2020-09-18 02:16:41 +02:00
public override func request(maxID: String? = nil, minID: String? = nil) {
if case .statuses = collection, maxID == nil {
accountStatusesService.fetchPinnedStatuses()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { _ in }
.store(in: &cancellables)
}
super.request(maxID: maxID, minID: minID)
}
override func isPinned(status: Status) -> Bool {
2020-09-23 03:00:56 +02:00
collection == .statuses && items.first?.contains(CollectionItem(id: status.id, kind: .status)) ?? false
2020-09-18 02:16:41 +02:00
}
2020-09-23 03:43:06 +02:00
public override var title: AnyPublisher<String?, Never> {
2020-09-26 09:13:50 +02:00
$accountViewModel.map { $0?.accountName }.eraseToAnyPublisher()
2020-09-23 03:43:06 +02:00
}
2020-09-18 02:16:41 +02:00
}
2020-09-22 08:53:11 +02:00
public extension AccountStatusesViewModel {
func fetchAccount() {
accountStatusesService.fetchAccount()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { _ in }
.store(in: &cancellables)
}
}