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

83 lines
3.3 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-23 10:38:39 +02:00
private(set) var maintainScrollPositionOfStatusID: String?
2020-08-18 07:13:37 +02:00
private let statusListService: StatusListService
2020-08-24 04:50:54 +02:00
private var statusViewModelCache = [Status: (StatusViewModel, AnyCancellable)]()
2020-08-18 07:13:37 +02:00
private var cancellables = Set<AnyCancellable>()
init(statusListService: StatusListService) {
self.statusListService = statusListService
statusListService.statusSections
2020-08-24 04:50:54 +02:00
.handleEvents(receiveOutput: { [weak self] in
self?.determineIfScrollPositionShouldBeMaintained(newStatusSections: $0)
self?.cleanViewModelCache(newStatusSections: $0)
})
2020-08-18 07:13:37 +02:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$statusSections)
}
}
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 {
2020-08-24 04:50:54 +02:00
var statusViewModel: StatusViewModel
if let cachedViewModel = statusViewModelCache[status]?.0 {
statusViewModel = cachedViewModel
} else {
statusViewModel = StatusViewModel(statusService: statusListService.statusService(status: status))
statusViewModelCache[status] = (statusViewModel, statusViewModel.events
.flatMap { $0 }
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink {})
}
2020-08-21 04:29:01 +02:00
statusViewModel.isContextParent = status == contextParent
statusViewModel.isPinned = statusListService.isPinned(status: status)
statusViewModel.isReplyInContext = statusListService.isReplyInContext(status: status)
statusViewModel.hasReplyFollowing = statusListService.hasReplyFollowing(status: status)
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 {
2020-08-23 10:38:39 +02:00
func determineIfScrollPositionShouldBeMaintained(newStatusSections: [[Status]]) {
maintainScrollPositionOfStatusID = nil // clear old value
// Maintain scroll position of parent after initial load of context
if let contextParent = contextParent, statusSections == [[], [contextParent], []] {
maintainScrollPositionOfStatusID = contextParent.id
}
}
2020-08-24 04:50:54 +02:00
func cleanViewModelCache(newStatusSections: [[Status]]) {
let newStatuses = Set(newStatusSections.reduce([], +))
statusViewModelCache = statusViewModelCache.filter { newStatuses.contains($0.key) }
}
2020-08-21 04:29:01 +02:00
}