Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch panels instead of subscribe #3275

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions HomeAssistant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@
11E1639B250B1B760076D612 /* OnboardingStateObservation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11E16399250B1B760076D612 /* OnboardingStateObservation.swift */; };
11E5CF8124BBCE1B009AC30F /* ProcessInfo+BackgroundTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11E5CF8024BBCE1B009AC30F /* ProcessInfo+BackgroundTask.swift */; };
11E5CF8224BBCE1B009AC30F /* ProcessInfo+BackgroundTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11E5CF8024BBCE1B009AC30F /* ProcessInfo+BackgroundTask.swift */; };
11E7C4B02702E03000667342 /* WidgetOpenPageIntent+Observation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11E7C4AF2702E03000667342 /* WidgetOpenPageIntent+Observation.swift */; };
11E99A5027156854003C8A65 /* OnboardingTerminalViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11E99A4F27156854003C8A65 /* OnboardingTerminalViewController.swift */; };
11ED43962726599D00B5FD45 /* OnboardingAuthStepModels.test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11ED43952726599D00B5FD45 /* OnboardingAuthStepModels.test.swift */; };
11ED439827265B9C00B5FD45 /* OnboardingAuthStepNotify.test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11ED439727265B9C00B5FD45 /* OnboardingAuthStepNotify.test.swift */; };
Expand Down Expand Up @@ -766,6 +765,8 @@
42CE8FB62B46D14C00C707F9 /* FrontendStrings+Values.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42CE8FB42B46CAFD00C707F9 /* FrontendStrings+Values.swift */; };
42CE8FB72B46D14C00C707F9 /* FrontendStrings+Values.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42CE8FB42B46CAFD00C707F9 /* FrontendStrings+Values.swift */; };
42CE8FBB2B46DB6200C707F9 /* Assets.swift in Sources */ = {isa = PBXBuildFile; fileRef = B65B15042273188300635D5C /* Assets.swift */; };
42D334272D105990008D8E78 /* AppPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D334262D105990008D8E78 /* AppPanel.swift */; };
42D334282D105990008D8E78 /* AppPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D334262D105990008D8E78 /* AppPanel.swift */; };
42D3E49C2C5BB88F00444BE6 /* WatchBatterySensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D3E49B2C5BB88F00444BE6 /* WatchBatterySensor.swift */; };
42D3E49D2C5BB88F00444BE6 /* WatchBatterySensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D3E49B2C5BB88F00444BE6 /* WatchBatterySensor.swift */; };
42D3E4A12C5BCD1100444BE6 /* WatchContext.test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D3E4A02C5BCD1100444BE6 /* WatchContext.test.swift */; };
Expand All @@ -788,6 +789,8 @@
42DD84132B14ACAB00936F16 /* Color+ColorAsset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DD84122B14ACAB00936F16 /* Color+ColorAsset.swift */; };
42DD84162B14D7AC00936F16 /* WebViewExternalBusMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DD84142B14D68C00936F16 /* WebViewExternalBusMessage.swift */; };
42DD84192B14D83B00936F16 /* WebViewExternalBusMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DD84182B14D83B00936F16 /* WebViewExternalBusMessageTests.swift */; };
42DE75D32D1061A600FF379F /* PanelsUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DE75D12D105F3000FF379F /* PanelsUpdater.swift */; };
42DE75D42D1061A600FF379F /* PanelsUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DE75D12D105F3000FF379F /* PanelsUpdater.swift */; };
42DEDA9A2C5B926400E9D29D /* AppVersionSensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DEDA992C5B926400E9D29D /* AppVersionSensor.swift */; };
42DEDA9B2C5B926400E9D29D /* AppVersionSensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DEDA992C5B926400E9D29D /* AppVersionSensor.swift */; };
42DF6B2D2CCF8A2200D7EC14 /* PermissionRequestView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DF6B2C2CCF8A2200D7EC14 /* PermissionRequestView.swift */; };
Expand Down Expand Up @@ -1749,7 +1752,6 @@
11DE9F3925B614EB0081C0ED /* Application.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = Application.xib; sourceTree = "<group>"; };
11E16399250B1B760076D612 /* OnboardingStateObservation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingStateObservation.swift; sourceTree = "<group>"; };
11E5CF8024BBCE1B009AC30F /* ProcessInfo+BackgroundTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ProcessInfo+BackgroundTask.swift"; sourceTree = "<group>"; };
11E7C4AF2702E03000667342 /* WidgetOpenPageIntent+Observation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WidgetOpenPageIntent+Observation.swift"; sourceTree = "<group>"; };
11E99A4F27156854003C8A65 /* OnboardingTerminalViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingTerminalViewController.swift; sourceTree = "<group>"; };
11ED43952726599D00B5FD45 /* OnboardingAuthStepModels.test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingAuthStepModels.test.swift; sourceTree = "<group>"; };
11ED439727265B9C00B5FD45 /* OnboardingAuthStepNotify.test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingAuthStepNotify.test.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2072,6 +2074,7 @@
42CE8FAC2B46C12C00C707F9 /* Domain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Domain.swift; sourceTree = "<group>"; };
42CE8FAE2B46C3D600C707F9 /* CoreStrings+Values.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoreStrings+Values.swift"; sourceTree = "<group>"; };
42CE8FB42B46CAFD00C707F9 /* FrontendStrings+Values.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FrontendStrings+Values.swift"; sourceTree = "<group>"; };
42D334262D105990008D8E78 /* AppPanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppPanel.swift; sourceTree = "<group>"; };
42D3E49B2C5BB88F00444BE6 /* WatchBatterySensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchBatterySensor.swift; sourceTree = "<group>"; };
42D3E4A02C5BCD1100444BE6 /* WatchContext.test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchContext.test.swift; sourceTree = "<group>"; };
42D3E4A72C5D00AD00444BE6 /* ScriptAppIntent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptAppIntent.swift; sourceTree = "<group>"; };
Expand All @@ -2094,6 +2097,7 @@
42DD84372B15DC3F00936F16 /* et */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = et; path = et.lproj/Frontend.strings; sourceTree = "<group>"; };
42DD84382B15DC3F00936F16 /* et */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = et; path = et.lproj/InfoPlist.strings; sourceTree = "<group>"; };
42DD84392B15DC3F00936F16 /* et */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = et; path = et.lproj/Localizable.strings; sourceTree = "<group>"; };
42DE75D12D105F3000FF379F /* PanelsUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PanelsUpdater.swift; sourceTree = "<group>"; };
42DEDA992C5B926400E9D29D /* AppVersionSensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppVersionSensor.swift; sourceTree = "<group>"; };
42DF6B2C2CCF8A2200D7EC14 /* PermissionRequestView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PermissionRequestView.swift; sourceTree = "<group>"; };
42DF6B2E2CCF918D00D7EC14 /* BluetoothPermissionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BluetoothPermissionView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3139,7 +3143,6 @@
children = (
11EE9B5624C68F5700404AF8 /* Action+Observation.swift */,
11A48D8024CA8ADB0021BDD9 /* NotificationCategory+Observation.swift */,
11E7C4AF2702E03000667342 /* WidgetOpenPageIntent+Observation.swift */,
);
path = Observation;
sourceTree = "<group>";
Expand Down Expand Up @@ -4162,6 +4165,15 @@
path = Domain;
sourceTree = "<group>";
};
42D334252D105981008D8E78 /* Panels */ = {
isa = PBXGroup;
children = (
42DE75D12D105F3000FF379F /* PanelsUpdater.swift */,
42D334262D105990008D8E78 /* AppPanel.swift */,
);
path = Panels;
sourceTree = "<group>";
};
42D3E49F2C5BCCF600444BE6 /* Watch */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -4961,6 +4973,7 @@
426740A42B17348700C1DD73 /* Assets */,
42CE8FAB2B46C11E00C707F9 /* Domain */,
42CA28AC2B101D320093B31A /* DesignSystem */,
42D334252D105981008D8E78 /* Panels */,
11B38EE0275C545C00205C7B /* Intents */,
D014EEAA212928EC008EA6F5 /* API */,
D0FF79C920D7787F0034574D /* ClientEvents */,
Expand Down Expand Up @@ -6909,7 +6922,6 @@
11E99A5027156854003C8A65 /* OnboardingTerminalViewController.swift in Sources */,
422F951F2CFDF7C5003B7514 /* HAApplicationShortcutItem.swift in Sources */,
1101568424D770B2009424C9 /* NFCWriter.swift in Sources */,
11E7C4B02702E03000667342 /* WidgetOpenPageIntent+Observation.swift in Sources */,
1187DE4624D7E1BD00F0A6A6 /* SimulatorNFCManager.swift in Sources */,
1185DF96271FBB9800ED7D9A /* OnboardingAuthLogin.swift in Sources */,
425573E62B5838B600145217 /* MaterialDesignIcons+CarPlay.swift in Sources */,
Expand Down Expand Up @@ -7234,6 +7246,7 @@
11CFD78227364F450082D557 /* Identifier.swift in Sources */,
11AF4D17249C8083006C74C0 /* With.swift in Sources */,
42333ADB2D0B1771001E8408 /* EntityRegistryListForDisplay.swift in Sources */,
42DE75D42D1061A600FF379F /* PanelsUpdater.swift in Sources */,
11B38EF7275C54A300205C7B /* UpdateSensorsIntentHandler.swift in Sources */,
1141182B24AFA10900E6525C /* WebhookResponseHandler.swift in Sources */,
426266462C11B02C0081A818 /* InteractiveImmediateMessages.swift in Sources */,
Expand Down Expand Up @@ -7304,6 +7317,7 @@
11F2F27F258725D300F61F7C /* NotificationAttachmentErrorImage.swift in Sources */,
B67CE8A622200F220034C1D0 /* HAAPI.swift in Sources */,
1105CE1D272B9CB300F33BD8 /* ServerManager.swift in Sources */,
42D334272D105990008D8E78 /* AppPanel.swift in Sources */,
1141182724AF9A0500E6525C /* WebhookManager.swift in Sources */,
1104FC9225322C1800B8BE34 /* Dictionary+Additions.swift in Sources */,
118261F824F8D6B0000795C6 /* SensorProviderDependencies.swift in Sources */,
Expand Down Expand Up @@ -7394,6 +7408,7 @@
11C4629124B14E6B00031902 /* XCGLogger+UNNotification.swift in Sources */,
110ED56325A563D600489AF7 /* DisplaySensor.swift in Sources */,
424151FB2CD8EF2200D7A6F9 /* MagicItem+Migration.swift in Sources */,
42DE75D32D1061A600FF379F /* PanelsUpdater.swift in Sources */,
11AF4D16249C8083006C74C0 /* With.swift in Sources */,
11B38EEC275C54A200205C7B /* IntentHandlerFactory.swift in Sources */,
1182620124F9C3F7000795C6 /* HACoreBlahObject.swift in Sources */,
Expand Down Expand Up @@ -7442,6 +7457,7 @@
116C0C2F267EB90F00A992E4 /* UserDefaultsValueSync.swift in Sources */,
11A3BD2D26192210005237E6 /* LocalPushManager.swift in Sources */,
42FCD00C2B9B25D60057783F /* ThreadClientProtocol.swift in Sources */,
42D334282D105990008D8E78 /* AppPanel.swift in Sources */,
11C4628824B109C100031902 /* WebhookResponseLocation.swift in Sources */,
11AF4D19249C8253006C74C0 /* PedometerSensor.swift in Sources */,
B6872E632226841400C475D1 /* MobileAppRegistrationRequest.swift in Sources */,
Expand Down
1 change: 0 additions & 1 deletion Sources/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
_ = Realm.live()
Action.setupObserver()
NotificationCategory.setupObserver()
WidgetOpenPageIntent.setupObserver()
}

private func setupMenus() {
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/Scenes/WebViewSceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ final class WebViewSceneDelegate: NSObject, UIWindowSceneDelegate {
UIApplication.shared.applicationState == .active
})
Current.periodicAppEntitiesUpdater().setup()
Current.periodicAppEntitiesUpdater().updateAppEntities()
Current.panelsUpdater().update()
}

func windowScene(
Expand Down

This file was deleted.

50 changes: 21 additions & 29 deletions Sources/Extensions/Widgets/OpenPage/PageAppEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ struct PageAppEntityQuery: EntityQuery, EntityStringQuery {
panels.filter({ panel in
identifiers.contains(id(for: panel, server: server))
}).compactMap { panel in
PageAppEntity(id: id(for: panel, server: server), panel: panel, serverId: server.identifier.rawValue)
PageAppEntity(
id: id(for: panel, server: server),
panel: toHAPanel(appPanel: panel),
serverId: server.identifier.rawValue
)
}
}
}
Expand All @@ -42,7 +46,7 @@ struct PageAppEntityQuery: EntityQuery, EntityStringQuery {
}).map({ panel in
PageAppEntity(
id: id(for: panel, server: server),
panel: panel,
panel: toHAPanel(appPanel: panel),
serverId: server.identifier.rawValue
)
}))
Expand All @@ -54,42 +58,30 @@ struct PageAppEntityQuery: EntityQuery, EntityStringQuery {
.init(.init(stringLiteral: server.info.name), items: panels.map({ panel in
PageAppEntity(
id: id(for: panel, server: server),
panel: panel,
panel: toHAPanel(appPanel: panel),
serverId: server.identifier.rawValue
)
}))
}))
}

func id(for panel: HAPanel, server: Server) -> String {
func id(for panel: AppPanel, server: Server) -> String {
"\(server.identifier.rawValue)-\(panel.path)"
}

private func panels() async throws -> [Server: [HAPanel]] {
await withCheckedContinuation { continuation in
var panelsPerServer: [Server: [HAPanel]] = [:]
var finishedPipesCount = 0
for server in Current.servers.all {
(
Current.diskCache
.value(
for: OpenPageIntentHandler
.cacheKey(serverIdentifier: server.identifier.rawValue)
) as Promise<HAPanels>
).pipe { result in
switch result {
case let .fulfilled(panels):
panelsPerServer[server] = panels.allPanels
case let .rejected(error):
Current.Log.error("Failed to retrieve HAPanels, error: \(error.localizedDescription)")
}
finishedPipesCount += 1
// Since AppPanels came afterwards we need to keep the same
// object as before to not break previously created widgets
private func toHAPanel(appPanel: AppPanel) -> HAPanel {
.init(
icon: appPanel.icon,
title: appPanel.title,
path: appPanel.path,
component: appPanel.component,
showInSidebar: appPanel.showInSidebar
)
}

if finishedPipesCount == Current.servers.all.count {
continuation.resume(returning: panelsPerServer)
}
}
}
}
private func panels() async throws -> [Server: [AppPanel]] {
try AppPanel.panelsPerServer()
}
}
35 changes: 35 additions & 0 deletions Sources/Shared/Common/Extensions/GRDB+Initialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum GRDBDatabaseTable: String {
case carPlayConfig
case clientEvent
case appEntityRegistryListForDisplay
case appPanel
}

public enum DatabaseTables {
Expand Down Expand Up @@ -55,6 +56,16 @@ public enum DatabaseTables {
case entityId
case registry
}

public enum AppPanel: String {
case id
case serverId
case icon
case title
case path
case component
case showInSidebar
}
}

public extension DatabaseQueue {
Expand Down Expand Up @@ -94,6 +105,7 @@ public extension DatabaseQueue {
var shouldCreateCarPlayConfig = false
var shouldCreateClientEvent = false
var shouldCreateAppEntityRegistryListForDisplay = false
var shouldCreateAppPanel = false

do {
try database.read { db in
Expand All @@ -104,6 +116,8 @@ public extension DatabaseQueue {
shouldCreateClientEvent = try !db.tableExists(GRDBDatabaseTable.clientEvent.rawValue)
shouldCreateAppEntityRegistryListForDisplay = try !db
.tableExists(GRDBDatabaseTable.appEntityRegistryListForDisplay.rawValue)
shouldCreateAppPanel = try !db
.tableExists(GRDBDatabaseTable.appPanel.rawValue)
}
} catch {
let errorMessage = "Failed to check if GRDB tables exist, error: \(error.localizedDescription)"
Expand Down Expand Up @@ -210,5 +224,26 @@ public extension DatabaseQueue {
Current.Log.error(errorMessage)
}
}

// AppPanel
if shouldCreateAppPanel {
do {
try database.write { db in
try db.create(table: GRDBDatabaseTable.appPanel.rawValue) { t in
t.primaryKey(DatabaseTables.AppPanel.id.rawValue, .text).notNull()
t.column(DatabaseTables.AppPanel.serverId.rawValue, .text).notNull()
t.column(DatabaseTables.AppPanel.icon.rawValue, .text)
t.column(DatabaseTables.AppPanel.title.rawValue, .text).notNull()
t.column(DatabaseTables.AppPanel.path.rawValue, .text).notNull()
t.column(DatabaseTables.AppPanel.component.rawValue, .text).notNull()
t.column(DatabaseTables.AppPanel.showInSidebar.rawValue, .boolean).notNull()
}
}
} catch {
let errorMessage =
"Failed to create AppPanel GRDB table, error: \(error.localizedDescription)"
Current.Log.error(errorMessage)
}
}
}
}
Loading
Loading