metatext/Shared/View Models/StatusesViewModel.swift

75 lines
2.8 KiB
Swift
Raw Normal View History

2020-08-18 05:13:37 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
class StatusesViewModel: ObservableObject {
2020-08-19 22:16:03 +00:00
@Published private(set) var statusSections = [[Status]]()
2020-08-18 05:13:37 +00:00
@Published var alertItem: AlertItem?
2020-08-19 22:16:03 +00:00
@Published private(set) var loading = false
2020-08-21 02:29:01 +00:00
let scrollToStatus: AnyPublisher<Status, Never>
2020-08-18 05:13:37 +00:00
private let statusListService: StatusListService
2020-08-21 02:29:01 +00:00
private let scrollToStatusInput = PassthroughSubject<Status, Never>()
2020-08-19 22:16:03 +00:00
private var hasScrolledToParentAfterContextLoad = false
2020-08-18 05:13:37 +00:00
private var cancellables = Set<AnyCancellable>()
init(statusListService: StatusListService) {
self.statusListService = statusListService
2020-08-21 02:29:01 +00:00
scrollToStatus = scrollToStatusInput.eraseToAnyPublisher()
2020-08-18 05:13:37 +00:00
statusListService.statusSections
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$statusSections)
2020-08-19 22:16:03 +00: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 02:29:01 +00:00
self.scrollToStatusInput.send(contextParent)
2020-08-19 22:16:03 +00:00
}
}
.store(in: &cancellables)
2020-08-18 05:13:37 +00:00
}
}
extension StatusesViewModel {
2020-08-19 22:16:03 +00:00
var contextParent: Status? { statusListService.contextParent }
2020-08-18 05:13:37 +00:00
func request(maxID: String? = nil, minID: String? = nil) {
statusListService.request(maxID: maxID, minID: minID)
.assignErrorsToAlertItem(to: \.alertItem, on: self)
2020-08-19 22:16:03 +00:00
.handleEvents(
receiveSubscription: { [weak self] _ in self?.loading = true },
receiveCompletion: { [weak self] _ in self?.loading = false })
2020-08-18 05:13:37 +00:00
.sink {}
.store(in: &cancellables)
}
2020-08-19 22:16:03 +00:00
2020-08-21 02:29:01 +00: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-19 22:16:03 +00:00
func contextViewModel(status: Status) -> StatusesViewModel {
StatusesViewModel(statusListService: statusListService.contextService(status: status))
}
2020-08-18 05:13:37 +00:00
}
2020-08-21 02:29:01 +00:00
private extension StatusesViewModel {
static var viewModelCache = [Status: StatusViewModel]()
}