From 02ab44c3ebd36356388dc841c4ba0cebef1d6799 Mon Sep 17 00:00:00 2001 From: Martin Hartl Date: Tue, 7 Jan 2020 21:54:21 +0100 Subject: [PATCH] Only show mark unread above/below actions if articles above/below contained unread items --- Shared/Timeline/ArticleArray.swift | 25 +++++++++++++++++++++++++ iOS/SceneCoordinator.swift | 28 ++++++++-------------------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/Shared/Timeline/ArticleArray.swift b/Shared/Timeline/ArticleArray.swift index 60394f084..36e53940f 100644 --- a/Shared/Timeline/ArticleArray.swift +++ b/Shared/Timeline/ArticleArray.swift @@ -109,5 +109,30 @@ extension Array where Element == Article { } return true } + + func articlesAbove(article: Article) -> [Article] { + guard let position = firstIndex(of: article) else { + return [] + } + + let articlesAbove = self[.. [Article] { + guard let position = firstIndex(of: article) else { + return [] + } + + var articlesBelow = Array(self[position...]) + + guard !articlesBelow.isEmpty else { + return [] + } + + articlesBelow.removeFirst() + + return articlesBelow + } } diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index 81742e68a..441b3fb05 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -872,7 +872,8 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } func canMarkAboveAsRead(for article: Article) -> Bool { - return articles.first != article + let articlesAboveArray = articles.articlesAbove(article: article) + return articlesAboveArray.canMarkAllAsRead() } func markAboveAsRead() { @@ -884,16 +885,13 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } func markAboveAsRead(_ article: Article) { - guard let position = articles.firstIndex(of: article) else { - return - } - - let articlesAbove = articles[.. Bool { - return articles.last != article + let articleBelowArray = articles.articlesBelow(article: article) + return articleBelowArray.canMarkAllAsRead() } func markBelowAsRead() { @@ -905,18 +903,8 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } func markBelowAsRead(_ article: Article) { - guard let position = articles.firstIndex(of: article) else { - return - } - - var articlesBelow = Array(articles[position...]) - - guard !articlesBelow.isEmpty else { - return - } - - articlesBelow.removeFirst() - markAllAsRead(articlesBelow) + let articleBelowArray = articles.articlesBelow(article: article) + markAllAsRead(articleBelowArray) } func markAsReadForCurrentArticle() {