From 52fb51d390eab409a136cfab1c4f4d63b6e12ee6 Mon Sep 17 00:00:00 2001 From: Tim Bert <5411131+timbms@users.noreply.github.com> Date: Sun, 15 Sep 2024 20:47:09 +0200 Subject: [PATCH] Getting rid of ObservableOpenHABSitemapPage for watch --- openHAB.xcodeproj/project.pbxproj | 18 +- .../Views/Utils/MapView.swift | 4 +- .../Model/ObservableOpenHABSitemapPage.swift | 98 ------ .../Model/ObservableOpenHABWidget.swift | 283 ------------------ ...ion.swift => OpenHABWidgetExtension.swift} | 0 .../openHABWatch Extension/UserData.swift | 6 +- 6 files changed, 9 insertions(+), 400 deletions(-) delete mode 100644 openHABWatch Extension/openHABWatch Extension/Model/ObservableOpenHABSitemapPage.swift delete mode 100644 openHABWatch Extension/openHABWatch Extension/Model/ObservableOpenHABWidget.swift rename openHABWatch Extension/openHABWatch Extension/Model/{ObservableOpenHABWidgetExtension.swift => OpenHABWidgetExtension.swift} (100%) diff --git a/openHAB.xcodeproj/project.pbxproj b/openHAB.xcodeproj/project.pbxproj index 390b7423..bfe15fd3 100644 --- a/openHAB.xcodeproj/project.pbxproj +++ b/openHAB.xcodeproj/project.pbxproj @@ -27,8 +27,6 @@ 934E592728F16EBA00162004 /* Kingfisher in Frameworks */ = {isa = PBXBuildFile; productRef = 934E592628F16EBA00162004 /* Kingfisher */; }; 934E592928F16EBA00162004 /* DeviceKit in Frameworks */ = {isa = PBXBuildFile; productRef = 934E592828F16EBA00162004 /* DeviceKit */; }; 934E592B28F16EBA00162004 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 934E592A28F16EBA00162004 /* Alamofire */; }; - 9350F17923814FAC00054BA8 /* ObservableOpenHABWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9350F17723814FAC00054BA8 /* ObservableOpenHABWidget.swift */; }; - 9350F17A23814FAC00054BA8 /* ObservableOpenHABSitemapPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9350F17823814FAC00054BA8 /* ObservableOpenHABSitemapPage.swift */; }; 935B484625342B8E00E44CF0 /* URL+Static.swift in Sources */ = {isa = PBXBuildFile; fileRef = 935B484525342B8E00E44CF0 /* URL+Static.swift */; }; 93685A7A2ADE755C0077A9A6 /* openHABTests.xctestplan in Resources */ = {isa = PBXBuildFile; fileRef = 93685A792ADE755C0077A9A6 /* openHABTests.xctestplan */; }; 937C8B0C2800A738009C055E /* Intents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = 935D340A257B7DC00020A404 /* Intents.intentdefinition */; }; @@ -121,13 +119,12 @@ DAC6608D236F771600F4501E /* PreferencesSwiftUIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC6608C236F771600F4501E /* PreferencesSwiftUIView.swift */; }; DAC6608F236F80BA00F4501E /* PreferencesRowUIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC6608E236F80BA00F4501E /* PreferencesRowUIView.swift */; }; DAC9395522B00E7600C5F423 /* XCTestCaseExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC9395422B00E7600C5F423 /* XCTestCaseExtension.swift */; }; - DAC9AF4724F9669F006DAE93 /* ObservableOpenHABWidgetExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC9AF4624F9669F006DAE93 /* ObservableOpenHABWidgetExtension.swift */; }; + DAC9AF4724F9669F006DAE93 /* OpenHABWidgetExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC9AF4624F9669F006DAE93 /* OpenHABWidgetExtension.swift */; }; DAC9AF4924F966FA006DAE93 /* LazyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC9AF4824F966FA006DAE93 /* LazyView.swift */; }; DACB636227D3FC6500041931 /* error.png in Resources */ = {isa = PBXBuildFile; fileRef = DACB636127D3FC6500041931 /* error.png */; }; DACB636327D3FC6500041931 /* error.png in Resources */ = {isa = PBXBuildFile; fileRef = DACB636127D3FC6500041931 /* error.png */; }; DACE664A2C63B0760069E514 /* OpenAPIURLSession in Frameworks */ = {isa = PBXBuildFile; productRef = DACE66492C63B0760069E514 /* OpenAPIURLSession */; }; DACE664D2C63B0840069E514 /* OpenAPIRuntime in Frameworks */ = {isa = PBXBuildFile; productRef = DACE664C2C63B0840069E514 /* OpenAPIRuntime */; }; - DAEAA89B21E2611000267EA3 /* OpenHABNotificationsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAEAA89A21E2611000267EA3 /* OpenHABNotificationsViewController.swift */; }; DAEAA89D21E6B06400267EA3 /* ReusableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAEAA89C21E6B06300267EA3 /* ReusableView.swift */; }; DAEAA89F21E6B16600267EA3 /* UITableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAEAA89E21E6B16600267EA3 /* UITableView.swift */; }; DAF0A28B2C56E3A300A14A6A /* RollershutterCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAF0A28A2C56E3A300A14A6A /* RollershutterCell.swift */; }; @@ -295,8 +292,6 @@ 933D7F0822E7015100621A03 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 933D7F0E22E7030600621A03 /* SnapshotHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SnapshotHelper.swift; path = fastlane/SnapshotHelper.swift; sourceTree = SOURCE_ROOT; }; 934B610B2348D2F9009112D5 /* Color+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Color+Extension.swift"; sourceTree = ""; }; - 9350F17723814FAC00054BA8 /* ObservableOpenHABWidget.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObservableOpenHABWidget.swift; sourceTree = ""; }; - 9350F17823814FAC00054BA8 /* ObservableOpenHABSitemapPage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObservableOpenHABSitemapPage.swift; sourceTree = ""; }; 935B484525342B8E00E44CF0 /* URL+Static.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+Static.swift"; sourceTree = ""; }; 935D3412257B7E2F0020A404 /* nl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nl; path = Resources/nl.lproj/Intents.strings; sourceTree = ""; }; 935D3419257B7E820020A404 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; name = Base; path = Resources/Base.lproj/Intents.intentdefinition; sourceTree = ""; }; @@ -415,7 +410,7 @@ DAC6608E236F80BA00F4501E /* PreferencesRowUIView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesRowUIView.swift; sourceTree = ""; }; DAC9394322AD4A7A00C5F423 /* OpenHABWatchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenHABWatchTests.swift; sourceTree = ""; }; DAC9395422B00E7600C5F423 /* XCTestCaseExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCTestCaseExtension.swift; sourceTree = ""; }; - DAC9AF4624F9669F006DAE93 /* ObservableOpenHABWidgetExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservableOpenHABWidgetExtension.swift; sourceTree = ""; }; + DAC9AF4624F9669F006DAE93 /* OpenHABWidgetExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenHABWidgetExtension.swift; sourceTree = ""; }; DAC9AF4824F966FA006DAE93 /* LazyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazyView.swift; sourceTree = ""; }; DACB636127D3FC6500041931 /* error.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = error.png; sourceTree = ""; }; DAD488B2287DDDFE00414693 /* nb */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nb; path = nb.lproj/Interface.strings; sourceTree = ""; }; @@ -444,7 +439,6 @@ DAF4581D23DC60020018B495 /* ImageRawRow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageRawRow.swift; sourceTree = ""; }; DAF4F6BF222734D200C24876 /* NewImageUITableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewImageUITableViewCell.swift; sourceTree = ""; }; DAF6F4112C67E83B0083883E /* openapiCorrected.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = openapiCorrected.json; sourceTree = ""; }; - DF05EF111D00696200DD646D /* DrawerUITableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DrawerUITableViewCell.swift; sourceTree = ""; }; DF05FF221896BD2D00FF2F9B /* SelectionUITableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectionUITableViewCell.swift; sourceTree = ""; }; DF06F1FB18FEC2020011E7B9 /* ColorPickerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPickerViewController.swift; sourceTree = ""; }; DF1B302C1CF5C667009C921C /* OpenHABNotification.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenHABNotification.swift; sourceTree = ""; }; @@ -641,10 +635,8 @@ isa = PBXGroup; children = ( DA15BFBC23C6726400BD8ADA /* ObservableOpenHABDataObject.swift */, - 9350F17823814FAC00054BA8 /* ObservableOpenHABSitemapPage.swift */, - 9350F17723814FAC00054BA8 /* ObservableOpenHABWidget.swift */, DA9721C224E29A8F0092CCFD /* UserDefaultsBacked.swift */, - DAC9AF4624F9669F006DAE93 /* ObservableOpenHABWidgetExtension.swift */, + DAC9AF4624F9669F006DAE93 /* OpenHABWidgetExtension.swift */, DAC9AF4824F966FA006DAE93 /* LazyView.swift */, ); name = Model; @@ -1399,7 +1391,7 @@ buildActionMask = 2147483647; files = ( DA7649DE23FC81A20085CE46 /* Unwrap.swift in Sources */, - DAC9AF4724F9669F006DAE93 /* ObservableOpenHABWidgetExtension.swift in Sources */, + DAC9AF4724F9669F006DAE93 /* OpenHABWidgetExtension.swift in Sources */, DAF4581423DC1F5D0018B495 /* AppState.swift in Sources */, DA2E0B0E23DCC153009B0A99 /* MapView.swift in Sources */, DA2E0B1023DCC439009B0A99 /* MapViewRow.swift in Sources */, @@ -1422,7 +1414,6 @@ DA07752B2346705F0086C685 /* ExtensionDelegate.swift in Sources */, DAF4581623DC48400018B495 /* GenericRow.swift in Sources */, DAC6608F236F80BA00F4501E /* PreferencesRowUIView.swift in Sources */, - 9350F17A23814FAC00054BA8 /* ObservableOpenHABSitemapPage.swift in Sources */, DAF457A023DA3E1C0018B495 /* SegmentRow.swift in Sources */, DAF4578923D79AA50018B495 /* DetailTextLabelView.swift in Sources */, DA15BFBD23C6726400BD8ADA /* ObservableOpenHABDataObject.swift in Sources */, @@ -1433,7 +1424,6 @@ DA50C7BD2B0A51BD0009F716 /* SliderWithSwitchSupportRow.swift in Sources */, DAF457A623DB9CE00018B495 /* SetpointRow.swift in Sources */, DAF4581823DC4A050018B495 /* ImageRow.swift in Sources */, - 9350F17923814FAC00054BA8 /* ObservableOpenHABWidget.swift in Sources */, DA07752F2346705F0086C685 /* NotificationView.swift in Sources */, DA0775312346705F0086C685 /* ComplicationController.swift in Sources */, DAF4578523D7807A0018B495 /* Color+Extension.swift in Sources */, diff --git a/openHABWatch Extension/Views/Utils/MapView.swift b/openHABWatch Extension/Views/Utils/MapView.swift index 4d8966a7..12fba262 100644 --- a/openHABWatch Extension/Views/Utils/MapView.swift +++ b/openHABWatch Extension/Views/Utils/MapView.swift @@ -38,10 +38,10 @@ struct MapView: View { } } - struct MapView_Previews: PreviewProvider { +struct MapView_Previews: PreviewProvider { static var previews: some View { let widget = UserData().widgets[9] return MapView(widget: widget) .previewDevice("Apple Watch Series 5 - 44mm") } - } +} diff --git a/openHABWatch Extension/openHABWatch Extension/Model/ObservableOpenHABSitemapPage.swift b/openHABWatch Extension/openHABWatch Extension/Model/ObservableOpenHABSitemapPage.swift deleted file mode 100644 index 39c09267..00000000 --- a/openHABWatch Extension/openHABWatch Extension/Model/ObservableOpenHABSitemapPage.swift +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) 2010-2024 Contributors to the openHAB project -// -// See the NOTICE file(s) distributed with this work for additional -// information. -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0 -// -// SPDX-License-Identifier: EPL-2.0 - -import Foundation -import OpenHABCore -import os.log - -class ObservableOpenHABSitemapPage: NSObject { - var sendCommand: ((_ item: OpenHABItem, _ command: String?) -> Void)? - var widgets: [OpenHABWidget] = [] - var pageId = "" - var title = "" - var link = "" - var leaf = false - - init(pageId: String, title: String, link: String, leaf: Bool, widgets: [OpenHABWidget]) { - super.init() - self.pageId = pageId - self.title = title - self.link = link - self.leaf = leaf - var tempWidgets = [OpenHABWidget]() - tempWidgets.flatten(widgets) - self.widgets = tempWidgets - for widget in self.widgets { - widget.sendCommand = { [weak self] item, command in - self?.sendCommand(item, commandToSend: command) - } - } - } - - init(pageId: String, title: String, link: String, leaf: Bool, expandedWidgets: [OpenHABWidget]) { - super.init() - self.pageId = pageId - self.title = title - self.link = link - self.leaf = leaf - widgets = expandedWidgets - for widget in widgets { - widget.sendCommand = { [weak self] item, command in - self?.sendCommand(item, commandToSend: command) - } - } - } - - private func sendCommand(_ item: OpenHABItem?, commandToSend command: String?) { - guard let item else { return } - - os_log("SitemapPage sending command %{PUBLIC}@ to %{PUBLIC}@", log: OSLog.remoteAccess, type: .info, command ?? "", item.name) - sendCommand?(item, command) - } -} - -extension ObservableOpenHABSitemapPage { - struct CodingData: Decodable { - let pageId: String? - let title: String? - let link: String? - let leaf: Bool? - let widgets: [OpenHABWidget.CodingData]? - - private enum CodingKeys: String, CodingKey { - case pageId = "id" - case title - case link - case leaf - case widgets - } - } -} - -extension ObservableOpenHABSitemapPage.CodingData { - var openHABSitemapPage: ObservableOpenHABSitemapPage { - let mappedWidgets = widgets?.map(\.openHABWidget) ?? [] - return ObservableOpenHABSitemapPage(pageId: pageId ?? "", title: title ?? "", link: link ?? "", leaf: leaf ?? false, widgets: mappedWidgets) - } -} - -extension ObservableOpenHABSitemapPage { - func filter(_ isIncluded: (OpenHABWidget) throws -> Bool) rethrows -> ObservableOpenHABSitemapPage { - let filteredOpenHABSitemapPage = try ObservableOpenHABSitemapPage( - pageId: pageId, - title: title, - link: link, - leaf: leaf, - expandedWidgets: widgets.filter(isIncluded) - ) - return filteredOpenHABSitemapPage - } -} diff --git a/openHABWatch Extension/openHABWatch Extension/Model/ObservableOpenHABWidget.swift b/openHABWatch Extension/openHABWatch Extension/Model/ObservableOpenHABWidget.swift deleted file mode 100644 index 9adf56fe..00000000 --- a/openHABWatch Extension/openHABWatch Extension/Model/ObservableOpenHABWidget.swift +++ /dev/null @@ -1,283 +0,0 @@ -// Copyright (c) 2010-2024 Contributors to the openHAB project -// -// See the NOTICE file(s) distributed with this work for additional -// information. -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0 -// -// SPDX-License-Identifier: EPL-2.0 - -import Alamofire -#if canImport(Combine) -import Combine -#endif -import Foundation -import MapKit -import OpenHABCore -import os.log - -@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *) -class ObservableOpenHABWidget: NSObject, MKAnnotation, Identifiable, ObservableObject { - var id: String = "" - - var sendCommand: ((_ item: OpenHABItem, _ command: String?) -> Void)? - var widgetId = "" - @Published var label = "" - var icon = "" - var type = "" - var url = "" - var period = "" - var minValue = 0.0 - var maxValue = 100.0 - var step = 1.0 - var refresh = 0 - var height = 44.0 - var isLeaf = false - var iconColor = "" - var labelcolor = "" - var valuecolor = "" - var service = "" - @Published var state = "" - var text = "" - var legend: Bool? - var encoding = "" - @Published var item: OpenHABItem? - var linkedPage: OpenHABPage? - var mappings: [OpenHABWidgetMapping] = [] - var image: UIImage? - var widgets: [ObservableOpenHABWidget] = [] - public var visibility = true - public var switchSupport = false - public var forceAsItem: Bool? - - @Published public var stateEnumBinding: WidgetTypeEnum = .unassigned - - // Text prior to "[" - var labelText: String? { - let array = label.components(separatedBy: "[") - return array[0].trimmingCharacters(in: .whitespaces) - } - - // Text between square brackets - public var labelValue: String? { - let pattern = /\[(.*?)\]/.dotMatchesNewlines() - guard let firstMatch = label.firstMatch(of: pattern) else { return nil } - return String(firstMatch.1) - } - - var coordinate: CLLocationCoordinate2D { - item?.stateAsLocation()?.coordinate ?? kCLLocationCoordinate2DInvalid - } - - var mappingsOrItemOptions: [OpenHABWidgetMapping] { - if mappings.isEmpty, let commandOptions = item?.commandDescription?.commandOptions { - commandOptions.map { OpenHABWidgetMapping(command: $0.command, label: $0.label ?? "") } - } else if mappings.isEmpty, let stateOptions = item?.stateDescription?.options { - stateOptions.map { OpenHABWidgetMapping(command: $0.value, label: $0.label) } - } else { - mappings - } - } - - public var stateValueAsBool: Bool? { - item?.state?.parseAsBool() - } - - public var stateValueAsBrightness: Int? { - item?.state?.parseAsBrightness() - } - - public var stateValueAsUIColor: UIColor? { - item?.state?.parseAsUIColor() - } - - public var stateValueAsNumberState: NumberState? { - item?.state?.parseAsNumber(format: item?.stateDescription?.numberPattern) - } - - var adjustedValue: Double { - if let item { - adj(item.stateAsDouble()) - } else { - minValue - } - } - - var stateEnum: WidgetTypeEnum { - switch type { - case "Frame": - .frame - case "Switch": - // Reflecting the discussion held in https://github.com/openhab/openhab-core/issues/952 - if !mappings.isEmpty { - .segmented(Int(mappingIndex(byCommand: item?.state) ?? -1)) - } else if item?.isOfTypeOrGroupType(.switchItem) ?? false { - .switcher(item?.state == "ON" ? true : false) - } else if item?.isOfTypeOrGroupType(.rollershutter) ?? false { - .rollershutter - } else if !mappingsOrItemOptions.isEmpty { - .segmented(Int(mappingIndex(byCommand: item?.state) ?? -1)) - } else { - .switcher(item?.state == "ON" ? true : false) - } - case "Setpoint": - .setpoint - case "Slider": - .slider // (adjustedValue) - case "Selection": - .selection - case "Colorpicker": - .colorpicker - case "Chart": - .chart - case "Image": - .image - case "Video": - .video - case "Webview": - .webview - case "Mapview": - .mapview - default: - .unassigned - } - } - - public func sendItemUpdate(state: NumberState?) { - guard let item, let state else { - os_log("ItemUpdate for Item or State = nil", log: .default, type: .info) - return - } - if item.isOfTypeOrGroupType(.numberWithDimension) { - // For number items, include unit (if present) in command - sendCommand(state.toString(locale: Locale(identifier: "US"))) - } else { - // For all other items, send the plain value - sendCommand(state.stringValue) - } - } - - func sendCommandDouble(_ command: Double) { - sendCommand(String(command)) - } - - func sendCommand(_ command: String?) { - guard let item else { - os_log("Command for Item = nil", log: .default, type: .info) - return - } - guard let sendCommand else { - os_log("sendCommand closure not set", log: .default, type: .info) - return - } - sendCommand(item, command) - } - - func mappingIndex(byCommand command: String?) -> Int? { - mappingsOrItemOptions.firstIndex { $0.command == command } - } - - private func adj(_ raw: Double) -> Double { - var valueAdjustedToStep = floor((raw - minValue) / step) * step - valueAdjustedToStep += minValue - return valueAdjustedToStep.clamped(to: minValue ... maxValue) - } -} - -extension ObservableOpenHABWidget { - // This is an ugly initializer - convenience init(widgetId: String, label: String, icon: String, type: String, url: String?, period: String?, minValue: Double?, maxValue: Double?, step: Double?, refresh: Int?, height: Double?, isLeaf: Bool?, iconColor: String?, labelColor: String?, valueColor: String?, service: String?, state: String?, text: String?, legend: Bool?, encoding: String?, item: OpenHABItem?, linkedPage: OpenHABPage?, mappings: [OpenHABWidgetMapping], widgets: [ObservableOpenHABWidget], forceAsItem: Bool?) { - self.init() - - id = widgetId - - self.widgetId = widgetId - self.label = label - self.type = type - self.icon = icon - self.url = url ?? "" - self.period = period ?? "" - self.minValue = minValue ?? 0.0 - self.maxValue = maxValue ?? 100.0 - self.step = step ?? 1.0 - // Consider a minimal refresh rate of 100 ms, but 0 is special and means 'no refresh' - if let refreshVal = refresh, refreshVal > 0 { - self.refresh = max(100, refreshVal) - } else { - self.refresh = 0 - } - self.height = height ?? 44.0 - self.isLeaf = isLeaf ?? false - self.iconColor = iconColor ?? "" - labelcolor = labelColor ?? "" - valuecolor = valueColor ?? "" - self.service = service ?? "" - self.state = state ?? "" - self.text = text ?? "" - self.legend = legend - self.encoding = encoding ?? "" - self.item = item - self.linkedPage = linkedPage - self.mappings = mappings - self.widgets = widgets - - // Sanitize minValue, maxValue and step: min <= max, step >= 0 - self.maxValue = max(self.minValue, self.maxValue) - self.step = abs(self.step) - - self.forceAsItem = forceAsItem - - stateEnumBinding = stateEnum - } -} - -extension ObservableOpenHABWidget { - public struct CodingData: Decodable { - let widgetId: String - let label: String - let type: String - let icon: String - let url: String? - let period: String? - let minValue: Double? - let maxValue: Double? - let step: Double? - let refresh: Int? - let height: Double? - let isLeaf: Bool? - let iconColor: String? - let labelcolor: String? - let valuecolor: String? - let service: String? - let state: String? - let text: String? - let legend: Bool? - let encoding: String? - let groupType: String? - let item: OpenHABItem.CodingData? - let linkedPage: OpenHABPage.CodingData? - let mappings: [OpenHABWidgetMapping] - let widgets: [ObservableOpenHABWidget.CodingData] - let forceAsItem: Bool? - } -} - -extension ObservableOpenHABWidget.CodingData { - var openHABWidget: ObservableOpenHABWidget { - let mappedWidgets = widgets.map(\.openHABWidget) - // swiftlint:disable:next line_length - return ObservableOpenHABWidget(widgetId: widgetId, label: label, icon: icon, type: type, url: url, period: period, minValue: minValue, maxValue: maxValue, step: step, refresh: refresh, height: height, isLeaf: isLeaf, iconColor: iconColor, labelColor: labelcolor, valueColor: valuecolor, service: service, state: state, text: text, legend: legend, encoding: encoding, item: item?.openHABItem, linkedPage: linkedPage?.openHABSitemapPage, mappings: mappings, widgets: mappedWidgets, forceAsItem: forceAsItem) - } -} - -// Recursive parsing of nested widget structure -extension [OpenHABWidget] { - mutating func flatten(_ widgets: [Element]) { - for widget in widgets { - append(widget) - flatten(widget.widgets) - } - } -} diff --git a/openHABWatch Extension/openHABWatch Extension/Model/ObservableOpenHABWidgetExtension.swift b/openHABWatch Extension/openHABWatch Extension/Model/OpenHABWidgetExtension.swift similarity index 100% rename from openHABWatch Extension/openHABWatch Extension/Model/ObservableOpenHABWidgetExtension.swift rename to openHABWatch Extension/openHABWatch Extension/Model/OpenHABWidgetExtension.swift diff --git a/openHABWatch Extension/openHABWatch Extension/UserData.swift b/openHABWatch Extension/openHABWatch Extension/UserData.swift index 10a64430..78c2d8a5 100644 --- a/openHABWatch Extension/openHABWatch Extension/UserData.swift +++ b/openHABWatch Extension/openHABWatch Extension/UserData.swift @@ -25,7 +25,7 @@ final class UserData: ObservableObject { let decoder = JSONDecoder() - var openHABSitemapPage: ObservableOpenHABSitemapPage? + var openHABSitemapPage: OpenHABPage? private var commandOperation: Alamofire.Request? private var currentPageOperation: Alamofire.Request? @@ -42,7 +42,7 @@ final class UserData: ObservableObject { // Self-executing closure // Inspired by https://www.swiftbysundell.com/posts/inline-types-and-functions-in-swift openHABSitemapPage = try { - let sitemapPageCodingData = try data.decoded(as: ObservableOpenHABSitemapPage.CodingData.self) + let sitemapPageCodingData = try data.decoded(as: OpenHABPage.CodingData.self) return sitemapPageCodingData.openHABSitemapPage }() } catch { @@ -111,7 +111,7 @@ final class UserData: ObservableObject { // Self-executing closure // Inspired by https://www.swiftbysundell.com/posts/inline-types-and-functions-in-swift openHABSitemapPage = try { - let sitemapPageCodingData = try data.decoded(as: ObservableOpenHABSitemapPage.CodingData.self) + let sitemapPageCodingData = try data.decoded(as: OpenHABPage.CodingData.self) return sitemapPageCodingData.openHABSitemapPage }() } catch {