metatext-app-ios-iphone-ipad/Shared/View Models/StatusesViewModel.swift

75 lines
2.8 KiB
Swift
Raw Normal View History

2020-08-18 07:13:37 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
class StatusesViewModel: ObservableObject {
2020-08-20 00:16:03 +02:00
@Published private(set) var statusSections = [[Status]]()
2020-08-18 07:13:37 +02:00
@Published var alertItem: AlertItem?
2020-08-20 00:16:03 +02:00
@Published private(set) var loading = false
2020-08-21 04:29:01 +02:00
let scrollToStatus: AnyPublisher<Status, Never>
2020-08-18 07:13:37 +02:00
private let statusListService: StatusListService
2020-08-21 04:29:01 +02:00
private let scrollToStatusInput = PassthroughSubject<Status, Never>()
2020-08-20 00:16:03 +02:00
private var hasScrolledToParentAfterContextLoad = false
2020-08-18 07:13:37 +02:00
private var cancellables = Set<AnyCancellable>()
init(statusListService: StatusListService) {
self.statusListService = statusListService
2020-08-21 04:29:01 +02:00
scrollToStatus = scrollToStatusInput.eraseToAnyPublisher()
2020-08-18 07:13:37 +02:00
statusListService.statusSections
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$statusSections)
2020-08-20 00:16:03 +02:00
$statusSections
.sink { [weak self] in
guard let self = self else { return }
if
let contextParent = self.contextParent,
!($0.first ?? []).isEmpty || !(($0.last ?? []).isEmpty),
!self.hasScrolledToParentAfterContextLoad {
self.hasScrolledToParentAfterContextLoad = true
2020-08-21 04:29:01 +02:00
self.scrollToStatusInput.send(contextParent)
2020-08-20 00:16:03 +02:00
}
}
.store(in: &cancellables)
2020-08-18 07:13:37 +02:00
}
}
extension StatusesViewModel {
2020-08-20 00:16:03 +02:00
var contextParent: Status? { statusListService.contextParent }
2020-08-18 07:13:37 +02:00
func request(maxID: String? = nil, minID: String? = nil) {
statusListService.request(maxID: maxID, minID: minID)
.assignErrorsToAlertItem(to: \.alertItem, on: self)
2020-08-20 00:16:03 +02:00
.handleEvents(
receiveSubscription: { [weak self] _ in self?.loading = true },
receiveCompletion: { [weak self] _ in self?.loading = false })
2020-08-18 07:13:37 +02:00
.sink {}
.store(in: &cancellables)
}
2020-08-20 00:16:03 +02:00
2020-08-21 04:29:01 +02:00
func statusViewModel(status: Status) -> StatusViewModel {
var statusViewModel = Self.viewModelCache[status]
?? StatusViewModel(statusService: statusListService.statusService(status: status))
statusViewModel.isContextParent = status == contextParent
statusViewModel.isPinned = statusListService.isPinned(status: status)
statusViewModel.isReplyInContext = statusListService.isReplyInContext(status: status)
statusViewModel.hasReplyFollowing = statusListService.hasReplyFollowing(status: status)
Self.viewModelCache[status] = statusViewModel
return statusViewModel
}
2020-08-20 00:16:03 +02:00
func contextViewModel(status: Status) -> StatusesViewModel {
StatusesViewModel(statusListService: statusListService.contextService(status: status))
}
2020-08-18 07:13:37 +02:00
}
2020-08-21 04:29:01 +02:00
private extension StatusesViewModel {
static var viewModelCache = [Status: StatusViewModel]()
}