metatext/ViewModels/Sources/ViewModels/StatusListViewModel.swift

182 lines
7.4 KiB
Swift
Raw Normal View History

2020-08-18 05:13:37 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
2020-09-05 02:31:43 +00:00
import Foundation
2020-08-30 23:33:11 +00:00
import Mastodon
2020-08-31 18:57:02 +00:00
import ServiceLayer
2020-08-18 05:13:37 +00:00
2020-10-01 02:35:06 +00:00
final public class StatusListViewModel: ObservableObject {
2020-09-23 01:00:56 +00:00
@Published public private(set) var items = [[CollectionItem]]()
2020-09-01 07:33:49 +00:00
@Published public var alertItem: AlertItem?
2020-09-24 01:33:13 +00:00
public private(set) var nextPageMaxID: String?
2020-09-23 01:00:56 +00:00
public private(set) var maintainScrollPositionOfItem: CollectionItem?
2020-08-30 05:31:30 +00:00
2020-08-26 08:25:34 +00:00
private var statuses = [String: Status]()
2020-09-23 01:00:56 +00:00
private var flatStatusIDs = [String]()
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-09-23 01:00:56 +00:00
private let navigationEventsSubject = PassthroughSubject<NavigationEvent, Never>()
private let loadingSubject = PassthroughSubject<Bool, Never>()
2020-08-18 05:13:37 +00:00
private var cancellables = Set<AnyCancellable>()
init(statusListService: StatusListService) {
self.statusListService = statusListService
statusListService.statusSections
2020-08-30 06:02:00 +00:00
.combineLatest(statusListService.filters.map { $0.regularExpression() })
.map(Self.filter(statusSections:regularExpression:))
2020-08-24 02:50:54 +00:00
.handleEvents(receiveOutput: { [weak self] in
self?.determineIfScrollPositionShouldBeMaintained(newStatusSections: $0)
self?.cleanViewModelCache(newStatusSections: $0)
2020-09-22 06:53:11 +00:00
self?.statuses = Dictionary(uniqueKeysWithValues: Set($0.reduce([], +)).map { ($0.id, $0) })
2020-09-23 01:00:56 +00:00
self?.flatStatusIDs = $0.reduce([], +).map(\.id)
2020-08-24 02:50:54 +00:00
})
2020-09-01 07:33:49 +00:00
.receive(on: DispatchQueue.main)
2020-08-18 05:13:37 +00:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
2020-09-23 01:00:56 +00:00
.map { $0.map { $0.map { CollectionItem(id: $0.id, kind: .status) } } }
.assign(to: &$items)
2020-09-24 01:33:13 +00:00
statusListService.nextPageMaxIDs
.sink { [weak self] in self?.nextPageMaxID = $0 }
.store(in: &cancellables)
2020-08-18 05:13:37 +00:00
}
2020-10-01 02:35:06 +00:00
}
2020-09-18 00:16:41 +00:00
2020-10-01 02:35:06 +00:00
extension StatusListViewModel: CollectionViewModel {
2020-09-27 01:23:56 +00:00
public var collectionItems: AnyPublisher<[[CollectionItem]], Never> { $items.eraseToAnyPublisher() }
2020-09-23 01:43:06 +00:00
public var title: AnyPublisher<String?, Never> { Just(statusListService.title).eraseToAnyPublisher() }
2020-10-01 02:35:06 +00:00
public var alertItems: AnyPublisher<AlertItem, Never> { $alertItem.compactMap { $0 }.eraseToAnyPublisher() }
public var loading: AnyPublisher<Bool, Never> { loadingSubject.eraseToAnyPublisher() }
public var navigationEvents: AnyPublisher<NavigationEvent, Never> { navigationEventsSubject.eraseToAnyPublisher() }
2020-09-18 00:16:41 +00:00
public func request(maxID: String? = nil, minID: String? = nil) {
statusListService.request(maxID: maxID, minID: minID)
.receive(on: DispatchQueue.main)
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.handleEvents(
2020-09-23 01:00:56 +00:00
receiveSubscription: { [weak self] _ in self?.loadingSubject.send(true) },
receiveCompletion: { [weak self] _ in self?.loadingSubject.send(false) })
2020-09-18 00:16:41 +00:00
.sink { _ in }
.store(in: &cancellables)
}
2020-09-23 01:00:56 +00:00
public func itemSelected(_ item: CollectionItem) {
switch item.kind {
case .status:
let displayStatusID = statuses[item.id]?.displayStatus.id ?? item.id
navigationEventsSubject.send(
.collectionNavigation(
StatusListViewModel(
2020-09-25 05:39:06 +00:00
statusListService: statusListService
.navigationService
.contextStatusListService(id: displayStatusID))))
2020-09-23 01:00:56 +00:00
default:
break
}
}
public func canSelect(item: CollectionItem) -> Bool {
if case .status = item.kind, item.id == statusListService.contextParentID {
return false
}
return true
}
public func viewModel(item: CollectionItem) -> Any? {
switch item.kind {
case .status:
2020-09-27 01:23:56 +00:00
return statusViewModel(item: item)
2020-09-23 01:00:56 +00:00
default:
return nil
}
2020-09-15 01:39:35 +00:00
}
}
2020-09-25 05:39:06 +00:00
private extension StatusListViewModel {
static func filter(statusSections: [[Status]], regularExpression: String?) -> [[Status]] {
guard let regEx = regularExpression else { return statusSections }
return statusSections.map {
$0.filter { $0.filterableContent.range(of: regEx, options: [.regularExpression, .caseInsensitive]) == nil }
}
}
2020-08-24 04:34:19 +00:00
var contextParentID: String? { statusListService.contextParentID }
2020-08-19 22:16:03 +00:00
2020-09-27 01:23:56 +00:00
func statusViewModel(item: CollectionItem) -> StatusViewModel? {
guard let status = statuses[item.id] else { return nil }
2020-08-26 08:25:34 +00:00
2020-08-24 02:50:54 +00:00
var statusViewModel: StatusViewModel
2020-09-15 01:39:35 +00:00
2020-08-24 02:50:54 +00:00
if let cachedViewModel = statusViewModelCache[status]?.0 {
statusViewModel = cachedViewModel
} else {
2020-09-25 05:39:06 +00:00
statusViewModel = StatusViewModel(
statusService: statusListService.navigationService.statusService(status: status))
2020-09-14 23:32:34 +00:00
statusViewModelCache[status] = (statusViewModel,
statusViewModel.events
.flatMap { $0 }
.assignErrorsToAlertItem(to: \.alertItem, on: self)
2020-09-15 01:39:35 +00:00
.sink { [weak self] in
2020-09-25 05:39:06 +00:00
guard
let self = self,
let event = NavigationEvent($0)
2020-09-15 01:39:35 +00:00
else { return }
2020-09-25 05:39:06 +00:00
2020-09-23 01:00:56 +00:00
self.navigationEventsSubject.send(event)
2020-09-15 01:39:35 +00:00
})
2020-08-24 02:50:54 +00:00
}
2020-08-21 02:29:01 +00:00
2020-09-02 09:07:09 +00:00
statusViewModel.isContextParent = status.id == statusListService.contextParentID
2020-09-27 01:23:56 +00:00
statusViewModel.isPinned = item.info[.pinned] != nil
2020-09-02 09:07:09 +00:00
statusViewModel.isReplyInContext = isReplyInContext(status: status)
statusViewModel.hasReplyFollowing = hasReplyFollowing(status: status)
2020-08-21 02:29:01 +00:00
return statusViewModel
}
2020-09-15 01:39:35 +00:00
2020-08-23 08:38:39 +00:00
func determineIfScrollPositionShouldBeMaintained(newStatusSections: [[Status]]) {
2020-09-23 01:00:56 +00:00
maintainScrollPositionOfItem = nil // clear old value
2020-09-02 07:39:42 +00:00
2020-08-23 08:38:39 +00:00
// Maintain scroll position of parent after initial load of context
2020-09-02 07:39:42 +00:00
if let contextParentID = contextParentID, flatStatusIDs == [contextParentID] || flatStatusIDs == [] {
2020-09-23 01:00:56 +00:00
maintainScrollPositionOfItem = CollectionItem(id: contextParentID, kind: .status)
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-09-02 09:07:09 +00:00
func isReplyInContext(status: Status) -> Bool {
guard
let contextParentID = contextParentID,
2020-09-02 09:07:09 +00:00
let index = flatStatusIDs.firstIndex(where: { $0 == status.id }),
index > 0
else { return false }
let previousStatusID = flatStatusIDs[index - 1]
return previousStatusID != contextParentID && status.inReplyToId == previousStatusID
}
func hasReplyFollowing(status: Status) -> Bool {
guard
let contextParentID = contextParentID,
2020-09-02 09:07:09 +00:00
let index = flatStatusIDs.firstIndex(where: { $0 == status.id }),
flatStatusIDs.count > index + 1,
let nextStatus = statuses[flatStatusIDs[index + 1]]
else { return false }
return status.id != contextParentID && nextStatus.inReplyToId == status.id
}
2020-08-21 02:29:01 +00:00
}