From 6e015c1abac7ecba8086d54ce8cddc1bc659f9ca Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 30 Dec 2025 21:00:07 -0800 Subject: [PATCH] Merge (manually) state restoration changes from 6.2.1. --- iOS/HidingReadArticlesState.swift | 150 ++++++++++++++++++ .../MainTimelineViewController.swift | 9 +- iOS/SceneDelegate.swift | 1 + 3 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 iOS/HidingReadArticlesState.swift diff --git a/iOS/HidingReadArticlesState.swift b/iOS/HidingReadArticlesState.swift new file mode 100644 index 000000000..7917784c8 --- /dev/null +++ b/iOS/HidingReadArticlesState.swift @@ -0,0 +1,150 @@ +// +// HidingReadArticlesState.swift +// NetNewsWire-iOS +// +// Created by Brent Simmons on 12/8/25. +// Copyright © 2025 Ranchero Software. All rights reserved. +// + +import Foundation +import Account + +final class HidingReadArticlesState { + private var smartFeedsHiding = Set() + private var feedsHiding = [String: Set]() // accountID: Set + private var foldersShowing = [String: Set]() // accountID: Set + + func copy(from stateRestorationInfo: StateRestorationInfo) { + smartFeedsHiding = stateRestorationInfo.smartFeedsHidingReadArticles + feedsHiding = stateRestorationInfo.feedsHidingReadArticles + foldersShowing = stateRestorationInfo.foldersShowingReadArticles + } + + func save() { + saveSmartFeedsHiding() + saveFeedsHiding() + saveFoldersShowing() + } + + func toggleHiding(for sidebarItemID: FeedIdentifier) { + assert(canToggleHiding(for: sidebarItemID)) + if (!canToggleHiding(for: sidebarItemID)) { + return + } + + let hidesReadArticles = isHiding(for: sidebarItemID) + let toggledValue = !hidesReadArticles + saveHiding(for: sidebarItemID, hiding: toggledValue) + } + + func isHiding(for sidebarItemID: FeedIdentifier) -> Bool { + switch sidebarItemID { + + case .smartFeed(let id): + if isUnreadSmartFeed(sidebarItemID) { + return true + } + return smartFeedsHiding.contains(id) + + case .webFeed(let accountID, let feedID): + var isHidingReadArticles = false + if let feedIDs = feedsHiding[accountID] { + isHidingReadArticles = feedIDs.contains(feedID) + } + return isHidingReadArticles + + case .folder(let accountID, let folderName): + // Folders hide read articles by default, so we check if not showing read articles. + var isHidingReadArticles = true + if let folderNames = foldersShowing[accountID] { + isHidingReadArticles = !folderNames.contains(folderName) + } + return isHidingReadArticles + } + } + + func canToggleHiding(for sidebarItemID: FeedIdentifier) -> Bool { + // The only item that can't be toggled is the unread smart feed. + !isUnreadSmartFeed(sidebarItemID) + } +} + +private extension HidingReadArticlesState { + + func isUnreadSmartFeed(_ sidebarItemID: FeedIdentifier) -> Bool { + sidebarItemID == SmartFeedsController.shared.unreadFeed.feedID + } + + func saveHiding(for sidebarItemID: FeedIdentifier, hiding: Bool) { + switch sidebarItemID { + + case .smartFeed(let id): + if isUnreadSmartFeed(sidebarItemID) { + return + } + if hiding { + smartFeedsHiding.insert(id) + } else { + smartFeedsHiding.remove(id) + } + saveSmartFeedsHiding() + + case .webFeed(let accountID, let feedID): + if hiding { + var feedIDs = feedsHiding[accountID] ?? Set() + feedIDs.insert(feedID) + feedsHiding[accountID] = feedIDs + } else { + feedsHiding[accountID]?.remove(feedID) + } + saveFeedsHiding() + + case .folder(let accountID, let folderName): + // Folders hide read articles by default, so we store the folder + // only if it's showing read articles. It's the opposite of + // feedsHidingReadArticles. + if hiding { + foldersShowing[accountID]?.remove(folderName) + } else { + var folderNames = foldersShowing[accountID] ?? Set() + folderNames.insert(folderName) + foldersShowing[accountID] = folderNames + } + saveFoldersShowing() + } + } + + func saveFoldersShowing() { + var d = foldersShowing + + // Filter out accounts and folders that no longer exist. + for accountID in Array(d.keys) { + guard let account = AccountManager.shared.existingAccount(with: accountID) else { + d[accountID] = nil + continue + } + d[accountID] = d[accountID]?.filter { account.existingFolder(withDisplayName: $0) != nil } + } + + AppDefaults.shared.foldersShowingReadArticles = d + } + + func saveFeedsHiding() { + var d = feedsHiding + + // Filter out accounts and feeds that no longer exist. + for accountID in Array(d.keys) { + guard let account = AccountManager.shared.existingAccount(with: accountID) else { + d[accountID] = nil + continue + } + d[accountID] = d[accountID]?.filter { account.existingWebFeed(withWebFeedID: $0) != nil } + } + + AppDefaults.shared.feedsHidingReadArticles = d + } + + func saveSmartFeedsHiding() { + AppDefaults.shared.smartFeedsHidingReadArticles = smartFeedsHiding + } +} diff --git a/iOS/MainTimeline/MainTimelineViewController.swift b/iOS/MainTimeline/MainTimelineViewController.swift index d2cf48b31..79dd949f8 100644 --- a/iOS/MainTimeline/MainTimelineViewController.swift +++ b/iOS/MainTimeline/MainTimelineViewController.swift @@ -747,13 +747,8 @@ private extension MainTimelineViewController { } func resetUI(resetScroll: Bool) { - switch timelineDefaultReadFilterType { - case .none, .read: - navigationItem.rightBarButtonItem = filterButton - navigationItem.rightBarButtonItem?.isEnabled = true - case .alwaysRead: - navigationItem.rightBarButtonItem = nil - } + let shouldShowFilterButton = coordinator?.shouldShowFilterButton() ?? false + navigationItem.rightBarButtonItem = shouldShowFilterButton ? filterButton : nil if isReadArticlesFiltered { filterButton.style = .prominent diff --git a/iOS/SceneDelegate.swift b/iOS/SceneDelegate.swift index 89ac9905e..25e2240f4 100644 --- a/iOS/SceneDelegate.swift +++ b/iOS/SceneDelegate.swift @@ -77,6 +77,7 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate { } func sceneDidEnterBackground(_ scene: UIScene) { + coordinator.didEnterBackground() ArticleStringFormatter.emptyCaches() appDelegate.prepareAccountsForBackground() }