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-09-18 00:16:41 +00:00
|
|
|
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-23 01:00:56 +00:00
|
|
|
public let navigationEvents: AnyPublisher<NavigationEvent, Never>
|
|
|
|
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
|
2020-09-23 01:00:56 +00:00
|
|
|
navigationEvents = navigationEventsSubject.eraseToAnyPublisher()
|
2020-08-18 05:13:37 +00:00
|
|
|
|
|
|
|
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-08-18 05:13:37 +00:00
|
|
|
}
|
2020-09-18 00:16:41 +00:00
|
|
|
|
2020-09-23 01:43:06 +00:00
|
|
|
public var title: AnyPublisher<String?, Never> { Just(statusListService.title).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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isPinned(status: Status) -> Bool { false }
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 01:00:56 +00:00
|
|
|
extension StatusListViewModel: CollectionViewModel {
|
|
|
|
public var collectionItems: AnyPublisher<[[CollectionItem]], Never> { $items.eraseToAnyPublisher() }
|
|
|
|
|
|
|
|
public var alertItems: AnyPublisher<AlertItem, Never> { $alertItem.compactMap { $0 }.eraseToAnyPublisher() }
|
|
|
|
|
|
|
|
public var loading: AnyPublisher<Bool, Never> {
|
|
|
|
loadingSubject.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
public func itemSelected(_ item: CollectionItem) {
|
|
|
|
switch item.kind {
|
|
|
|
case .status:
|
|
|
|
let displayStatusID = statuses[item.id]?.displayStatus.id ?? item.id
|
|
|
|
|
|
|
|
navigationEventsSubject.send(
|
|
|
|
.collectionNavigation(
|
|
|
|
StatusListViewModel(
|
|
|
|
statusListService: statusListService.contextService(statusID: displayStatusID))))
|
|
|
|
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:
|
|
|
|
return statusViewModel(id: item.id)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-15 01:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:33:49 +00:00
|
|
|
public extension StatusListViewModel {
|
2020-08-28 22:39:17 +00:00
|
|
|
var paginates: Bool { statusListService.paginates }
|
|
|
|
|
2020-08-24 04:34:19 +00:00
|
|
|
var contextParentID: String? { statusListService.contextParentID }
|
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
|
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 {
|
|
|
|
statusViewModel = StatusViewModel(statusService: statusListService.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
|
|
|
|
guard let self = self,
|
2020-09-23 01:00:56 +00:00
|
|
|
let event = self.navigationEvent(statusEvent: $0)
|
2020-09-15 01:39:35 +00:00
|
|
|
else { return }
|
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-18 00:16:41 +00:00
|
|
|
statusViewModel.isPinned = isPinned(status: status)
|
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-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-30 06:02:00 +00:00
|
|
|
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-09-23 01:00:56 +00:00
|
|
|
func navigationEvent(statusEvent: StatusViewModel.Event) -> NavigationEvent? {
|
2020-09-15 01:39:35 +00:00
|
|
|
switch statusEvent {
|
|
|
|
case .ignorableOutput:
|
|
|
|
return nil
|
|
|
|
case let .navigation(item):
|
|
|
|
switch item {
|
|
|
|
case let .url(url):
|
|
|
|
return .urlNavigation(url)
|
|
|
|
case let .accountID(id):
|
2020-09-23 01:00:56 +00:00
|
|
|
return .collectionNavigation(
|
2020-09-18 00:16:41 +00:00
|
|
|
AccountStatusesViewModel(accountStatusesService: statusListService.service(accountID: id)))
|
2020-09-15 01:39:35 +00:00
|
|
|
case let .statusID(id):
|
2020-09-23 01:00:56 +00:00
|
|
|
return .collectionNavigation(
|
2020-09-15 01:39:35 +00:00
|
|
|
StatusListViewModel(
|
|
|
|
statusListService: statusListService.contextService(statusID: id)))
|
|
|
|
case let .tag(tag):
|
2020-09-23 01:00:56 +00:00
|
|
|
return .collectionNavigation(
|
2020-09-15 05:29:48 +00:00
|
|
|
StatusListViewModel(
|
|
|
|
statusListService: statusListService.service(timeline: Timeline.tag(tag))))
|
2020-09-15 01:39:35 +00:00
|
|
|
}
|
2020-09-23 01:00:56 +00:00
|
|
|
case let .accountListNavigation(accountListViewModel):
|
|
|
|
// return .collectionNavigation(accountListViewModel)
|
|
|
|
return nil
|
2020-09-15 01:39:35 +00:00
|
|
|
case let .share(url):
|
|
|
|
return .share(url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 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 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
|
|
|
}
|