From 8e5376803372ddf89a7103f91bf2e1af45047f6d Mon Sep 17 00:00:00 2001 From: everhardt Date: Sat, 30 Oct 2021 11:17:59 +0200 Subject: [PATCH] Mark bottom items in feed as read after 2 seconds on iOS In case markArticlesAsReadOnScroll is set --- .../MasterTimelineViewController.swift | 22 +++++++++++++++++++ iOS/SceneCoordinator.swift | 2 ++ 2 files changed, 24 insertions(+) diff --git a/iOS/MasterTimeline/MasterTimelineViewController.swift b/iOS/MasterTimeline/MasterTimelineViewController.swift index 195530545..0553fa58b 100644 --- a/iOS/MasterTimeline/MasterTimelineViewController.swift +++ b/iOS/MasterTimeline/MasterTimelineViewController.swift @@ -521,6 +521,28 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner return } + // Mark all articles as read when the bottom of the feed is reached + if let lastVisibleRowIndexPath = tableView.indexPathsForVisibleRows?.last { + let atBottom = dataSource.itemIdentifier(for: lastVisibleRowIndexPath) == coordinator.articles.last + + if atBottom && coordinator.markBottomArticlesAsReadWorkItem == nil { + let task = DispatchWorkItem { + let articlesToMarkAsRead = self.coordinator.articles.filter { !$0.status.read && !self.coordinator.articlesWithManuallyChangedReadStatus.contains($0) } + + if articlesToMarkAsRead.isEmpty { return } + self.coordinator.markAllAsRead(articlesToMarkAsRead) + self.coordinator.markBottomArticlesAsReadWorkItem = nil + } + + coordinator.markBottomArticlesAsReadWorkItem = task + DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: task) + } else if !atBottom, let task = coordinator.markBottomArticlesAsReadWorkItem { + task.cancel() + coordinator.markBottomArticlesAsReadWorkItem = nil + } + } + + // Mark articles scrolled out of sight at the top as read guard let firstVisibleRowIndexPath = tableView.indexPathsForVisibleRows?[0] else { return } guard let firstVisibleArticle = dataSource.itemIdentifier(for: firstVisibleRowIndexPath) else { diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index 0e9ab2237..c03018039 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -183,6 +183,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner { private(set) var showIcons = false var articlesWithManuallyChangedReadStatus: Set
= Set() + var markBottomArticlesAsReadWorkItem: DispatchWorkItem? var prevFeedIndexPath: IndexPath? { guard let indexPath = currentFeedIndexPath else { @@ -786,6 +787,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner { } articlesWithManuallyChangedReadStatus.removeAll() + markBottomArticlesAsReadWorkItem?.cancel() currentFeedIndexPath = indexPath masterFeedViewController.updateFeedSelection(animations: animations)