Skip to content

Commit 2418261

Browse files
Copilotbgoncal
andauthored
Add frontend cache reset on consecutive pull-to-refresh within 10 seconds (#4235)
## Summary Enables resetting the frontend cache via a double pull-to-refresh gesture (within 10 seconds) in WebViewController, providing a discoverable alternative to navigating through debug settings. **Implementation:** - Tracks pull-to-refresh timestamps to detect consecutive gestures within 10-second window - Triggers `cleanCache()` on second pull, provides haptic feedback - Subsequent pulls within window continue resetting cache - Pulls after 10+ seconds treated as new first pull **Technical Details:** - Added `lastPullToRefreshTimestamp` property to `WebViewController` - Enhanced `pullToRefresh(_:)` method with time-window detection logic - Uses existing `Current.websiteDataStoreHandler.cleanCache()` infrastructure - Comprehensive test coverage with mock `WebsiteDataStoreHandler` ```swift @objc func pullToRefresh(_ sender: UIRefreshControl) { let now = Current.date() if let lastTimestamp = lastPullToRefreshTimestamp, now.timeIntervalSince(lastTimestamp) < 10 { // Cache reset path Current.websiteDataStoreHandler.cleanCache { [weak self] in self?.refresh() self?.updateSensors() } lastPullToRefreshTimestamp = now } else { // Normal refresh path lastPullToRefreshTimestamp = now refresh() updateSensors() } } ``` ## Screenshots N/A - Internal behavior change with haptic feedback only ## Link to pull request in Documentation repository Documentation: home-assistant/companion.home-assistant# ## Any other notes Minimal change (+33 lines in WebViewController), leverages existing cache clearing mechanism, testable via `Current.date()` abstraction. <!-- START COPILOT CODING AGENT SUFFIX --> <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > Reset frontend cache (DebugView) when user pull to refresh more than once in WebViewController under 10 seconds </details> <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/home-assistant/iOS/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: bgoncal <5808343+bgoncal@users.noreply.github.com>
1 parent 007604c commit 2418261

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Sources/App/WebView/WebViewController.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKUIDeleg
4646

4747
private var loadActiveURLIfNeededInProgress = false
4848

49+
/// Track the timestamp of the last pull-to-refresh action
50+
private var lastPullToRefreshTimestamp: Date?
51+
4952
/// Handler for messages sent from the webview to the app
5053
var webViewExternalMessageHandler: WebViewExternalMessageHandlerProtocol = WebViewExternalMessageHandler(
5154
improvManager: ImprovManager.shared
@@ -865,6 +868,32 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKUIDeleg
865868
}
866869

867870
@objc func pullToRefresh(_ sender: UIRefreshControl) {
871+
let now = Current.date()
872+
873+
// Check if this is a consecutive pull-to-refresh within 10 seconds
874+
if let lastTimestamp = lastPullToRefreshTimestamp,
875+
now.timeIntervalSince(lastTimestamp) < 10 {
876+
// Second pull-to-refresh within 10 seconds - reset frontend cache
877+
Current.Log.info("Consecutive pull-to-refresh detected within 10 seconds, resetting frontend cache")
878+
Current.impactFeedback.impactOccurred(style: .medium)
879+
880+
// Reset the cache
881+
Current.websiteDataStoreHandler.cleanCache { [weak self] in
882+
Current.Log.info("Frontend cache reset after consecutive pull-to-refresh")
883+
self?.pullToRefreshActions()
884+
}
885+
886+
// Set the timestamp to now after cache reset to ensure proper timing for next pull
887+
// This prevents immediate re-triggering while still tracking for future pulls
888+
lastPullToRefreshTimestamp = now
889+
} else {
890+
// First pull-to-refresh or outside the 10-second window
891+
lastPullToRefreshTimestamp = now
892+
pullToRefreshActions()
893+
}
894+
}
895+
896+
private func pullToRefreshActions() {
868897
refresh()
869898
updateSensors()
870899
}

0 commit comments

Comments
 (0)