2020-08-18 05:13:37 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
|
2020-08-26 21:20:44 +00:00
|
|
|
class StatusListViewModel: ObservableObject {
|
2020-08-26 08:25:34 +00:00
|
|
|
@Published private(set) var statusIDs = [[String]]()
|
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-23 08:38:39 +00:00
|
|
|
private(set) var maintainScrollPositionOfStatusID: String?
|
2020-08-26 08:25:34 +00:00
|
|
|
private var statuses = [String: Status]()
|
2020-08-18 05:13:37 +00:00
|
|
|
private let statusListService: StatusListService
|
2020-08-24 02:50:54 +00:00
|
|
|
private var statusViewModelCache = [Status: (StatusViewModel, AnyCancellable)]()
|
2020-08-18 05:13:37 +00:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
init(statusListService: StatusListService) {
|
|
|
|
self.statusListService = statusListService
|
|
|
|
|
|
|
|
statusListService.statusSections
|
2020-08-24 02:50:54 +00:00
|
|
|
.handleEvents(receiveOutput: { [weak self] in
|
|
|
|
self?.determineIfScrollPositionShouldBeMaintained(newStatusSections: $0)
|
|
|
|
self?.cleanViewModelCache(newStatusSections: $0)
|
2020-08-26 08:25:34 +00:00
|
|
|
self?.statuses = Dictionary(uniqueKeysWithValues: $0.reduce([], +).map { ($0.id, $0) })
|
2020-08-24 02:50:54 +00:00
|
|
|
})
|
2020-08-18 05:13:37 +00:00
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
2020-08-26 08:25:34 +00:00
|
|
|
.map { $0.map { section in section.map(\.id) } }
|
|
|
|
.assign(to: &$statusIDs)
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 21:20:44 +00:00
|
|
|
extension StatusListViewModel {
|
2020-08-24 04:34:19 +00:00
|
|
|
var contextParentID: String? { statusListService.contextParentID }
|
2020-08-19 22:16:03 +00:00
|
|
|
|
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-26 09:19:38 +00:00
|
|
|
.sink { _ in }
|
2020-08-18 05:13:37 +00:00
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
2020-08-19 22:16:03 +00:00
|
|
|
|
2020-08-26 08:25:34 +00:00
|
|
|
func statusViewModel(id: String) -> StatusViewModel? {
|
|
|
|
guard let status = statuses[id] else { return nil }
|
|
|
|
|
2020-08-24 02:50:54 +00: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)
|
2020-08-26 09:19:38 +00:00
|
|
|
.sink { _ in })
|
2020-08-24 02:50:54 +00:00
|
|
|
}
|
2020-08-21 02:29:01 +00:00
|
|
|
|
2020-08-24 04:34:19 +00:00
|
|
|
statusViewModel.isContextParent = status.id == contextParentID
|
2020-08-21 02:29:01 +00:00
|
|
|
statusViewModel.isPinned = statusListService.isPinned(status: status)
|
|
|
|
statusViewModel.isReplyInContext = statusListService.isReplyInContext(status: status)
|
|
|
|
statusViewModel.hasReplyFollowing = statusListService.hasReplyFollowing(status: status)
|
|
|
|
|
|
|
|
return statusViewModel
|
|
|
|
}
|
|
|
|
|
2020-08-26 21:20:44 +00:00
|
|
|
func contextViewModel(id: String) -> StatusListViewModel? {
|
2020-08-26 08:25:34 +00:00
|
|
|
guard let status = statuses[id] else { return nil }
|
|
|
|
|
2020-08-26 21:20:44 +00:00
|
|
|
return StatusListViewModel(statusListService: statusListService.contextService(status: status))
|
2020-08-19 22:16:03 +00:00
|
|
|
}
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
2020-08-21 02:29:01 +00:00
|
|
|
|
2020-08-26 21:20:44 +00:00
|
|
|
private extension StatusListViewModel {
|
2020-08-23 08:38:39 +00:00
|
|
|
func determineIfScrollPositionShouldBeMaintained(newStatusSections: [[Status]]) {
|
|
|
|
maintainScrollPositionOfStatusID = nil // clear old value
|
|
|
|
|
|
|
|
// Maintain scroll position of parent after initial load of context
|
2020-08-26 08:25:34 +00:00
|
|
|
if let contextParentID = contextParentID, statusIDs.reduce([], +) == [contextParentID] {
|
2020-08-24 04:34:19 +00:00
|
|
|
maintainScrollPositionOfStatusID = contextParentID
|
2020-08-23 08:38:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-24 02:50:54 +00:00
|
|
|
|
|
|
|
func cleanViewModelCache(newStatusSections: [[Status]]) {
|
|
|
|
let newStatuses = Set(newStatusSections.reduce([], +))
|
|
|
|
|
|
|
|
statusViewModelCache = statusViewModelCache.filter { newStatuses.contains($0.key) }
|
|
|
|
}
|
2020-08-21 02:29:01 +00:00
|
|
|
}
|