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-01 07:33:49 +00:00
|
|
|
@Published public private(set) var statusIDs = [[String]]()
|
|
|
|
@Published public var alertItem: AlertItem?
|
|
|
|
@Published public private(set) var loading = false
|
2020-09-15 01:39:35 +00:00
|
|
|
public let events: AnyPublisher<Event, Never>
|
2020-09-01 07:33:49 +00:00
|
|
|
public private(set) var maintainScrollPositionOfStatusID: String?
|
2020-08-30 05:31:30 +00:00
|
|
|
|
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-09-15 01:39:35 +00:00
|
|
|
private let eventsSubject = PassthroughSubject<Event, Never>()
|
2020-08-18 05:13:37 +00:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
init(statusListService: StatusListService) {
|
|
|
|
self.statusListService = statusListService
|
2020-09-15 01:39:35 +00:00
|
|
|
events = eventsSubject.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-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-08-30 06:04:14 +00:00
|
|
|
.map { $0.map { $0.map(\.id) } }
|
2020-08-26 08:25:34 +00:00
|
|
|
.assign(to: &$statusIDs)
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
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(
|
|
|
|
receiveSubscription: { [weak self] _ in self?.loading = true },
|
|
|
|
receiveCompletion: { [weak self] _ in self?.loading = false })
|
|
|
|
.sink { _ in }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isPinned(status: Status) -> Bool { false }
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 01:39:35 +00:00
|
|
|
public extension StatusListViewModel {
|
|
|
|
enum Event {
|
|
|
|
case statusListNavigation(StatusListViewModel)
|
|
|
|
case urlNavigation(URL)
|
|
|
|
case share(URL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:33:49 +00:00
|
|
|
public extension StatusListViewModel {
|
2020-09-15 05:29:48 +00:00
|
|
|
var title: String? { statusListService.title }
|
|
|
|
|
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,
|
|
|
|
let event = self.event(statusEvent: $0)
|
|
|
|
else { return }
|
|
|
|
self.eventsSubject.send(event)
|
|
|
|
})
|
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-09-02 09:07:09 +00:00
|
|
|
func contextViewModel(id: String) -> StatusListViewModel {
|
2020-09-03 02:23:58 +00:00
|
|
|
let displayStatusID = statuses[id]?.displayStatus.id ?? id
|
|
|
|
|
|
|
|
return StatusListViewModel(statusListService: statusListService.contextService(statusID: displayStatusID))
|
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-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-15 01:39:35 +00:00
|
|
|
func event(statusEvent: StatusViewModel.Event) -> Event? {
|
|
|
|
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-18 00:16:41 +00:00
|
|
|
return .statusListNavigation(
|
|
|
|
AccountStatusesViewModel(accountStatusesService: statusListService.service(accountID: id)))
|
2020-09-15 01:39:35 +00:00
|
|
|
case let .statusID(id):
|
|
|
|
return .statusListNavigation(
|
|
|
|
StatusListViewModel(
|
|
|
|
statusListService: statusListService.contextService(statusID: id)))
|
|
|
|
case let .tag(tag):
|
2020-09-15 05:29:48 +00:00
|
|
|
return .statusListNavigation(
|
|
|
|
StatusListViewModel(
|
|
|
|
statusListService: statusListService.service(timeline: Timeline.tag(tag))))
|
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]]) {
|
|
|
|
maintainScrollPositionOfStatusID = nil // clear old value
|
|
|
|
|
2020-09-02 07:39:42 +00:00
|
|
|
let flatStatusIDs = statusIDs.reduce([], +)
|
|
|
|
|
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-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-09-02 09:07:09 +00:00
|
|
|
|
|
|
|
func isReplyInContext(status: Status) -> Bool {
|
|
|
|
let flatStatusIDs = statusIDs.reduce([], +)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
let flatStatusIDs = statusIDs.reduce([], +)
|
|
|
|
|
|
|
|
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
|
|
|
}
|