1
0
mirror of https://github.com/metabolist/metatext synced 2024-12-12 09:26:13 +01:00
metatext-app-ios-iphone-ipad/Shared/View Models/StatusesViewModel.swift

68 lines
2.6 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
private var cancellables = Set<AnyCancellable>()
init(statusListService: StatusListService) {
self.statusListService = statusListService
statusListService.statusSections
2020-08-23 10:38:39 +02:00
.handleEvents(receiveOutput: determineIfScrollPositionShouldBeMaintained(newStatusSections:))
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 {
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]()
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-21 04:29:01 +02:00
}