From 65962b8cf48b02bc6100ca458ba58b95b0ff3f29 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 30 Oct 2026 22:28:38 -0700 Subject: [PATCH] =?UTF-8?q?Move=20row=20to=20next=20thumbnail=20source=20w?= =?UTF-8?q?hen=20image=20URL=20returns=20something=20that=20isn=E2=80=99t?= =?UTF-8?q?=20an=20actual=20image.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Images/ArticleThumbnailFinder.swift | 24 ++++++++++++++----- .../ArticleThumbnailFinderTests.swift | 14 +++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift b/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift index fea79056d..8daf439e4 100644 --- a/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift +++ b/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift @@ -14,6 +14,7 @@ import ImageIO import Articles import RSCore import RSParser +import RSWeb import ActivityLog extension Notification.Name { @@ -95,6 +96,7 @@ extension Notification.Name { // Not flushed on appDidGoToBackground, which would make thumbnails flash on resume. NotificationCenter.default.addObserver(self, selector: #selector(handleLowMemory(_:)), name: .lowMemory, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleNetworkStatusDidChange(_:)), name: .networkStatusDidChange, object: nil) } /// False when offline or in Low Data Mode, when thumbnails not already on disk aren’t downloaded. @@ -191,6 +193,16 @@ extension Notification.Name { } } + func thumbnailDidFail(url: String, error: ImageDownloadError) { + if error.decodingFailed || error.rejectedPixelSize != nil { + urlsThatFailedToDecode.insert(url) + } + // A transient decoding failure counts, since the row would otherwise keep waiting for an image that isn’t coming. + if !error.isTransient || error.decodingFailed { + thumbnailURLDidFail(url) + } + } + /// Forgets the chosen thumbnail URL of each article that used `url`, and posts `.articleThumbnailDownloadDidFail`. func thumbnailURLDidFail(_ url: String) { var articleIDs = Set() @@ -209,6 +221,11 @@ extension Notification.Name { @objc func handleLowMemory(_ notification: Notification) { imageCache.removeAllObjects() } + + /// What failed to decode may have been a captive portal’s login page, not the image. + @objc func handleNetworkStatusDidChange(_ notification: Notification) { + urlsThatFailedToDecode.removeAll() + } } // MARK: - Private @@ -279,12 +296,7 @@ private extension ArticleThumbnailFinder { imageCache.setObject(decodedImage.image, forKey: cacheKey, cost: decodedImage.byteCount) NotificationCenter.default.post(name: .articleThumbnailDidBecomeAvailable, object: self, userInfo: [Self.urlKey: url]) } catch { - if error.decodingFailed || error.rejectedPixelSize != nil { - urlsThatFailedToDecode.insert(url) - } - if !error.isTransient { - thumbnailURLDidFail(url) - } + thumbnailDidFail(url: url, error: error) } } } diff --git a/Modules/Images/Tests/ImagesTests/ArticleThumbnailFinderTests.swift b/Modules/Images/Tests/ImagesTests/ArticleThumbnailFinderTests.swift index a662908d6..cc3949a94 100644 --- a/Modules/Images/Tests/ImagesTests/ArticleThumbnailFinderTests.swift +++ b/Modules/Images/Tests/ImagesTests/ArticleThumbnailFinderTests.swift @@ -11,6 +11,7 @@ import CoreGraphics import ImageIO import UniformTypeIdentifiers import Articles +import RSWeb @testable import Images /// Swift Testing also has an `Attachment` type. @@ -93,6 +94,19 @@ private typealias ArticleAttachment = Articles.Attachment #expect(finder.thumbnailURL(for: article, mode: .small) == "https://example.com/uploads/photo.jpg") } + @Test func nonImageResponseMovesToNextSourceUntilNetworkChanges() { + let finder = ArticleThumbnailFinder(imageDatabase: ImageDatabase(databasePath: ":memory:")) + let article = makeArticle(imageURL: "https://example.com/image.jpg", thumbnailURL: "https://example.com/thumbnail.jpg") + #expect(finder.thumbnailURL(for: article, mode: .small) == "https://example.com/thumbnail.jpg") + + finder.thumbnailDidFail(url: "https://example.com/thumbnail.jpg", error: ImageDownloadError(statusCode: 200, decodingFailed: true, isTransient: true)) + #expect(finder.thumbnailURL(for: article, mode: .small) == "https://example.com/image.jpg") + + finder.handleNetworkStatusDidChange(Notification(name: .networkStatusDidChange)) + let updatedArticle = makeArticle(imageURL: "https://example.com/image.jpg", thumbnailURL: "https://example.com/thumbnail.jpg") + #expect(finder.thumbnailURL(for: updatedArticle, mode: .small) == "https://example.com/thumbnail.jpg") + } + @Test func savedHTMLThumbnailIsLoadedWithoutDiscovery() async { let imageDatabase = ImageDatabase(databasePath: ":memory:") let article = makeArticle(contentHTML: #"

"#)