From 4dd754a49196b53f830c7034dfbdb5e557abe075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Pantale=C3=A3o=20Gon=C3=A7alves?= <5808343+bgoncal@users.noreply.github.com> Date: Mon, 17 Feb 2025 15:37:15 +0100 Subject: [PATCH] Revert changes for database updater and move call to scene become active (#3437) ## Summary ## Screenshots ## Link to pull request in Documentation repository Documentation: home-assistant/companion.home-assistant# ## Any other notes --- Sources/App/Scenes/WebViewSceneDelegate.swift | 11 +++-------- Sources/App/Settings/SettingsViewController.swift | 4 +--- Sources/Shared/Environment/AppDatabaseUpdater.swift | 6 ++---- Sources/Shared/Panels/PanelsUpdater.swift | 5 ++--- 4 files changed, 8 insertions(+), 18 deletions(-) diff --git a/Sources/App/Scenes/WebViewSceneDelegate.swift b/Sources/App/Scenes/WebViewSceneDelegate.swift index 8d12ed653..5ad76325d 100644 --- a/Sources/App/Scenes/WebViewSceneDelegate.swift +++ b/Sources/App/Scenes/WebViewSceneDelegate.swift @@ -119,19 +119,14 @@ final class WebViewSceneDelegate: NSObject, UIWindowSceneDelegate { Current.appDatabaseUpdater.stop() } - func sceneWillEnterForeground(_ scene: UIScene) { - DataWidgetsUpdater.update() + func sceneDidBecomeActive(_ scene: UIScene) { Current.modelManager.cleanup().cauterize() Current.modelManager.subscribe(isAppInForeground: { UIApplication.shared.applicationState == .active }) - Current.appDatabaseUpdater.update { - UIApplication.shared.applicationState - } - Current.panelsUpdater.update { - UIApplication.shared.applicationState - } + Current.appDatabaseUpdater.update() + Current.panelsUpdater.update() let widgetsCacheFile = AppConstants.widgetsCacheURL diff --git a/Sources/App/Settings/SettingsViewController.swift b/Sources/App/Settings/SettingsViewController.swift index d2ae251cc..3b4e778b6 100644 --- a/Sources/App/Settings/SettingsViewController.swift +++ b/Sources/App/Settings/SettingsViewController.swift @@ -165,9 +165,7 @@ class SettingsViewController: HAFormViewController { override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) - Current.appDatabaseUpdater.update { - UIApplication.shared.applicationState - } + Current.appDatabaseUpdater.update() } @objc func openAbout(_ sender: UIButton) { diff --git a/Sources/Shared/Environment/AppDatabaseUpdater.swift b/Sources/Shared/Environment/AppDatabaseUpdater.swift index 2bfffb43b..fba5ea925 100644 --- a/Sources/Shared/Environment/AppDatabaseUpdater.swift +++ b/Sources/Shared/Environment/AppDatabaseUpdater.swift @@ -5,7 +5,7 @@ import UIKit public protocol AppDatabaseUpdaterProtocol { func stop() - func update(uiApplicationState: @escaping () -> UIApplication.State) + func update() } final class AppDatabaseUpdater: AppDatabaseUpdaterProtocol { @@ -18,7 +18,7 @@ final class AppDatabaseUpdater: AppDatabaseUpdaterProtocol { cancelOnGoingRequests() } - func update(uiApplicationState: @escaping () -> UIApplication.State) { + func update() { cancelOnGoingRequests() if let lastUpdate, lastUpdate.timeIntervalSinceNow > -5 { @@ -37,7 +37,6 @@ final class AppDatabaseUpdater: AppDatabaseUpdaterProtocol { completion: { result in switch result { case let .success(entities): - guard uiApplicationState() == .active else { return } Current.appEntitiesModel().updateModel(Set(entities), server: server) case let .failure(error): Current.Log.error("Failed to fetch states: \(error)") @@ -59,7 +58,6 @@ final class AppDatabaseUpdater: AppDatabaseUpdaterProtocol { completion: { [weak self] result in switch result { case let .success(response): - guard uiApplicationState() == .active else { return } self?.saveEntityRegistryListForDisplay(response, serverId: server.identifier.rawValue) case let .failure(error): Current.Log.error("Failed to fetch EntityRegistryListForDisplay: \(error)") diff --git a/Sources/Shared/Panels/PanelsUpdater.swift b/Sources/Shared/Panels/PanelsUpdater.swift index b14b7f83f..a01d15f69 100644 --- a/Sources/Shared/Panels/PanelsUpdater.swift +++ b/Sources/Shared/Panels/PanelsUpdater.swift @@ -4,7 +4,7 @@ import PromiseKit import UIKit public protocol PanelsUpdaterProtocol { - func update(uiApplicationState: () -> UIApplication.State) + func update() } final class PanelsUpdater: PanelsUpdaterProtocol { @@ -13,7 +13,7 @@ final class PanelsUpdater: PanelsUpdaterProtocol { private var tokens: [(promise: Promise, cancel: () -> Void)?] = [] private var lastUpdate: Date? - public func update(uiApplicationState: () -> UIApplication.State) { + public func update() { if let lastUpdate, lastUpdate.timeIntervalSinceNow > -5 { Current.Log.verbose("Skipping panels update, last update was \(lastUpdate)") return @@ -28,7 +28,6 @@ final class PanelsUpdater: PanelsUpdaterProtocol { let request = Current.api(for: server)?.connection.send(.panels()) tokens.append(request) - guard uiApplicationState() == .active else { return } request?.promise.done({ [weak self] panels in self?.saveInDatabase(panels, server: server) }).cauterize()