2025-05-08 21:29:58 -07:00
//
// WebViewProvider.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 9/21/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import Foundation
2026-07-26 11:29:04 -07:00
import UIKit
2025-05-08 21:29:58 -07:00
import RSCore
import WebKit
/// WKWebView has an awful behavior of a flash to white on first load when in dark mode.
2025-09-23 10:08:57 -07:00
/// Keep a queue of WebViews where we've already done a trivial load so that by the time we need them in the UI, they're past the flash-to-white part of their lifecycle.
2025-11-17 21:53:40 -08:00
@ MainActor final class WebViewProvider : NSObject {
2025-05-08 21:29:58 -07:00
private let articleIconSchemeHandler : ArticleIconSchemeHandler
private let operationQueue = MainThreadOperationQueue ()
private var queue = NSMutableArray ()
2025-11-05 11:22:10 -08:00
2025-11-17 21:53:40 -08:00
init ( coordinator : SceneCoordinator ) {
2025-05-08 21:29:58 -07:00
articleIconSchemeHandler = ArticleIconSchemeHandler ( coordinator : coordinator )
super . init ()
replenishQueueIfNeeded ()
2026-07-26 11:29:04 -07:00
NotificationCenter . default . addObserver ( self , selector : #selector ( handleWillEnterForeground ( _ :)), name : UIApplication . willEnterForegroundNotification , object : nil )
2025-05-08 21:29:58 -07:00
}
2025-11-17 21:53:40 -08:00
func replenishQueueIfNeeded () {
2025-05-08 21:29:58 -07:00
operationQueue . add ( WebViewProviderReplenishQueueOperation ( queue : queue , articleIconSchemeHandler : articleIconSchemeHandler ))
}
2025-11-05 11:22:10 -08:00
2026-07-26 11:29:04 -07:00
// Pooled web views can lose their content process while the app is in the
// background. A load into a dead web view can fail silently, showing blank —
// so start over with fresh web views.
@objc func handleWillEnterForeground ( _ note : Notification ) {
queue . removeAllObjects ()
replenishQueueIfNeeded ()
}
2026-01-02 17:50:35 -08:00
func dequeueWebView ( completion : @ escaping ( PreloadedWebView ) -> Void ) {
2025-05-08 21:29:58 -07:00
operationQueue . add ( WebViewProviderDequeueOperation ( queue : queue , articleIconSchemeHandler : articleIconSchemeHandler , completion : completion ))
operationQueue . add ( WebViewProviderReplenishQueueOperation ( queue : queue , articleIconSchemeHandler : articleIconSchemeHandler ))
}
}
2025-11-11 19:09:33 -08:00
final class WebViewProviderReplenishQueueOperation : MainThreadOperation , @ unchecked Sendable {
2025-05-08 21:29:58 -07:00
private let minimumQueueDepth = 3
private var queue : NSMutableArray
private var articleIconSchemeHandler : ArticleIconSchemeHandler
2025-11-05 11:22:10 -08:00
2025-11-17 21:53:40 -08:00
init ( queue : NSMutableArray , articleIconSchemeHandler : ArticleIconSchemeHandler ) {
2025-05-08 21:29:58 -07:00
self . queue = queue
self . articleIconSchemeHandler = articleIconSchemeHandler
2025-11-11 19:09:33 -08:00
super . init ( name : "WebViewProviderReplenishQueueOperation" )
2025-05-08 21:29:58 -07:00
}
2025-11-05 11:22:10 -08:00
2025-11-17 21:53:40 -08:00
override func run () {
2025-05-08 21:29:58 -07:00
while queue . count < minimumQueueDepth {
let webView = PreloadedWebView ( articleIconSchemeHandler : articleIconSchemeHandler )
webView . preload ()
queue . insert ( webView , at : 0 )
}
2025-11-11 19:09:33 -08:00
didComplete ()
2025-05-08 21:29:58 -07:00
}
}
2025-11-11 19:09:33 -08:00
final class WebViewProviderDequeueOperation : MainThreadOperation , @ unchecked Sendable {
2025-05-08 21:29:58 -07:00
private var queue : NSMutableArray
private var articleIconSchemeHandler : ArticleIconSchemeHandler
2026-01-02 17:50:35 -08:00
private var completion : ( PreloadedWebView ) -> Void
2025-11-05 11:22:10 -08:00
2026-01-02 17:50:35 -08:00
init ( queue : NSMutableArray , articleIconSchemeHandler : ArticleIconSchemeHandler , completion : @ escaping ( PreloadedWebView ) -> Void ) {
2025-05-08 21:29:58 -07:00
self . queue = queue
self . articleIconSchemeHandler = articleIconSchemeHandler
self . completion = completion
2025-11-11 19:09:33 -08:00
super . init ( name : "WebViewProviderFlushQueueOperation" )
2025-05-08 21:29:58 -07:00
}
2025-11-05 11:22:10 -08:00
2025-11-17 21:53:40 -08:00
override func run () {
2025-05-08 21:29:58 -07:00
if let webView = queue . lastObject as ? PreloadedWebView {
self . completion ( webView )
self . queue . remove ( webView )
2025-11-11 19:09:33 -08:00
didComplete ()
2025-05-08 21:29:58 -07:00
return
}
assertionFailure ( "Creating PreloadedWebView in \( #function ) ; queue has run dry." )
2025-11-05 11:22:10 -08:00
2025-05-08 21:29:58 -07:00
let webView = PreloadedWebView ( articleIconSchemeHandler : articleIconSchemeHandler )
webView . preload ()
self . completion ( webView )
2025-11-11 19:09:33 -08:00
didComplete ()
2025-05-08 21:29:58 -07:00
}
}