Implement Select Next Unread using Combine
This commit is contained in:
parent
4c1061cfba
commit
dbe662b7db
|
@ -52,7 +52,7 @@ final class SceneModel: ObservableObject {
|
|||
/// Goes to the next unread item found in Sidebar and Timeline order, top to bottom
|
||||
func goToNextUnread() {
|
||||
if !timelineModel.goToNextUnread() {
|
||||
// timelineModel.isSelectNextUnread = true
|
||||
timelineModel.selectNextUnreadSubject.send(true)
|
||||
sidebarModel.selectNextUnread.send()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,8 +27,15 @@ struct TimelineContainerView: View {
|
|||
.onReceive(sceneModel.timelineModel.readFilterAndFeedsPublisher!) { (_, filtered) in
|
||||
isReadFiltered = filtered
|
||||
}
|
||||
.onReceive(sceneModel.timelineModel.timelineItemsPublisher!) { items in
|
||||
.onReceive(sceneModel.timelineModel.timelineItemsSelectPublisher!) { (items, selectTimelineItemID) in
|
||||
timelineItems = items
|
||||
if let selectID = selectTimelineItemID {
|
||||
#if os(macOS)
|
||||
sceneModel.timelineModel.selectedTimelineItemIDs = Set([selectID])
|
||||
#else
|
||||
sceneModel.timelineModel.selectedTimelineItemID = selectID
|
||||
#endif
|
||||
}
|
||||
}
|
||||
.onReceive(sceneModel.timelineModel.articleStatusChangePublisher!) { articleIDs in
|
||||
articleIDs.forEach { articleID in
|
||||
|
|
|
@ -34,6 +34,7 @@ class TimelineModel: ObservableObject, UndoableCommandRunner {
|
|||
}
|
||||
|
||||
var timelineItemsPublisher: AnyPublisher<TimelineItems, Never>?
|
||||
var timelineItemsSelectPublisher: AnyPublisher<(TimelineItems, String?), Never>?
|
||||
var selectedTimelineItemsPublisher: AnyPublisher<[TimelineItem], Never>?
|
||||
var selectedArticlesPublisher: AnyPublisher<[Article], Never>?
|
||||
var articleStatusChangePublisher: AnyPublisher<Set<String>, Never>?
|
||||
|
@ -46,6 +47,7 @@ class TimelineModel: ObservableObject, UndoableCommandRunner {
|
|||
var toggleStarredStatusForSelectedArticlesSubject = PassthroughSubject<Void, Never>()
|
||||
var openSelectedArticlesInBrowserSubject = PassthroughSubject<Void, Never>()
|
||||
var changeReadFilterSubject = PassthroughSubject<Bool, Never>()
|
||||
var selectNextUnreadSubject = PassthroughSubject<Bool, Never>()
|
||||
|
||||
var readFilterEnabledTable = [FeedIdentifier: Bool]()
|
||||
|
||||
|
@ -76,20 +78,22 @@ class TimelineModel: ObservableObject, UndoableCommandRunner {
|
|||
|
||||
@discardableResult
|
||||
func goToNextUnread() -> Bool {
|
||||
// var startIndex: Int
|
||||
// if let firstArticle = selectedArticles.first, let index = timelineItems.firstIndex(where: { $0.article == firstArticle }) {
|
||||
// startIndex = index
|
||||
// } else {
|
||||
// startIndex = 0
|
||||
// }
|
||||
//
|
||||
// for i in startIndex..<timelineItems.count {
|
||||
// if !timelineItems[i].article.status.read {
|
||||
// select(timelineItems[i].article.articleID)
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
//
|
||||
var startIndex: Int
|
||||
if let index = selectedTimelineItems.sorted(by: { $0.position < $1.position }).first?.position {
|
||||
startIndex = index
|
||||
} else {
|
||||
startIndex = 0
|
||||
}
|
||||
|
||||
for i in startIndex..<timelineItems.items.count {
|
||||
if !timelineItems.items[i].article.status.read {
|
||||
let timelineItemID = timelineItems.items[i].id
|
||||
selectedTimelineItemIDs = Set([timelineItemID])
|
||||
selectedTimelineItemID = timelineItemID
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -348,7 +352,7 @@ private extension TimelineModel {
|
|||
|
||||
timelineItemsPublisher = inputTimelineItemsPublisher
|
||||
.merge(with: downloadTimelineItemsPublisher)
|
||||
.share(replay: 1)
|
||||
.share()
|
||||
.eraseToAnyPublisher()
|
||||
|
||||
timelineItemsPublisher!
|
||||
|
@ -368,6 +372,24 @@ private extension TimelineModel {
|
|||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
// Automatically select the first unread if requested
|
||||
timelineItemsSelectPublisher = timelineItemsPublisher!
|
||||
.withLatestFrom(selectNextUnreadSubject.prepend(false), resultSelector: { ($0, $1) })
|
||||
.map { (timelineItems, selectNextUnread) -> (TimelineItems, String?) in
|
||||
var selectTimelineItemID: String? = nil
|
||||
if selectNextUnread {
|
||||
selectTimelineItemID = timelineItems.items.first(where: { $0.article.status.read == false })?.id
|
||||
}
|
||||
return (timelineItems, selectTimelineItemID)
|
||||
}
|
||||
.share(replay: 1)
|
||||
.eraseToAnyPublisher()
|
||||
|
||||
timelineItemsSelectPublisher!
|
||||
.sink { [weak self] _ in
|
||||
self?.selectNextUnreadSubject.send(false)
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
func subscribeToArticleSelectionChanges() {
|
||||
|
|
Loading…
Reference in New Issue