Skip to content

Commit

Permalink
Revert changes for database updater and move call to scene become act…
Browse files Browse the repository at this point in the history
…ive (#3437)

<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
  • Loading branch information
bgoncal authored Feb 17, 2025
1 parent 4cbae34 commit 4dd754a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
11 changes: 3 additions & 8 deletions Sources/App/Scenes/WebViewSceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions Sources/App/Settings/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions Sources/Shared/Environment/AppDatabaseUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import UIKit

public protocol AppDatabaseUpdaterProtocol {
func stop()
func update(uiApplicationState: @escaping () -> UIApplication.State)
func update()
}

final class AppDatabaseUpdater: AppDatabaseUpdaterProtocol {
Expand All @@ -18,7 +18,7 @@ final class AppDatabaseUpdater: AppDatabaseUpdaterProtocol {
cancelOnGoingRequests()
}

func update(uiApplicationState: @escaping () -> UIApplication.State) {
func update() {
cancelOnGoingRequests()

if let lastUpdate, lastUpdate.timeIntervalSinceNow > -5 {
Expand All @@ -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)")
Expand All @@ -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)")
Expand Down
5 changes: 2 additions & 3 deletions Sources/Shared/Panels/PanelsUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PromiseKit
import UIKit

public protocol PanelsUpdaterProtocol {
func update(uiApplicationState: () -> UIApplication.State)
func update()
}

final class PanelsUpdater: PanelsUpdaterProtocol {
Expand All @@ -13,7 +13,7 @@ final class PanelsUpdater: PanelsUpdaterProtocol {
private var tokens: [(promise: Promise<HAPanels>, 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
Expand All @@ -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()
Expand Down

0 comments on commit 4dd754a

Please sign in to comment.