diff --git a/OpenHABCore/Sources/OpenHABCore/Util/OptionalExtension.swift b/OpenHABCore/Sources/OpenHABCore/Util/OptionalExtension.swift new file mode 100644 index 000000000..3260441e9 --- /dev/null +++ b/OpenHABCore/Sources/OpenHABCore/Util/OptionalExtension.swift @@ -0,0 +1,25 @@ +// Copyright (c) 2010-2026 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 + +public extension Optional { + /// True when there is no value, or the value matches `expression`, e.g. + /// `loadingState.isNil(or: \.isFinished)`. + func isNil(or expression: (Wrapped) -> Bool) -> Bool { + map(expression) ?? true + } +} + +public extension Optional where Wrapped: Collection { + /// True for `nil` and for an empty string, array or other collection. + var isNilOrEmpty: Bool { + isNil(or: \.isEmpty) + } +} diff --git a/OpenHABCore/Sources/OpenHABCore/Util/StringExtension.swift b/OpenHABCore/Sources/OpenHABCore/Util/StringExtension.swift index 2ed7322e8..0b472cd7e 100644 --- a/OpenHABCore/Sources/OpenHABCore/Util/StringExtension.swift +++ b/OpenHABCore/Sources/OpenHABCore/Util/StringExtension.swift @@ -212,10 +212,6 @@ public extension String? { "" } } - - var isNilOrEmpty: Bool { - self == nil || self!.isEmpty - } } public extension String { diff --git a/OpenHABCore/Tests/OpenHABCoreTests/OptionalExtensionTests.swift b/OpenHABCore/Tests/OpenHABCoreTests/OptionalExtensionTests.swift new file mode 100644 index 000000000..cb480ce14 --- /dev/null +++ b/OpenHABCore/Tests/OpenHABCoreTests/OptionalExtensionTests.swift @@ -0,0 +1,48 @@ +// Copyright (c) 2010-2026 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 + +@testable import OpenHABCore +import Testing + +struct OptionalExtensionTests { + @Test("nil, or a value that matches, is true; a value that doesn't match is false") + func isNilOrMatches() { + let none: Int? = nil + #expect(none.isNil { $0 > 3 }) + #expect(Optional(5).isNil { $0 > 3 }) + #expect(!Optional(2).isNil { $0 > 3 }) + } + + @Test("A key path works as the expression") + func isNilOrKeyPath() { + #expect(Optional([Int]()).isNil(or: \.isEmpty)) + #expect(!Optional([1]).isNil(or: \.isEmpty)) + } + + @Test("nil and empty strings are nil or empty, text is not") + func stringIsNilOrEmpty() { + let none: String? = nil + #expect(none.isNilOrEmpty) + #expect(Optional("").isNilOrEmpty) + #expect(!Optional("ON").isNilOrEmpty) + #expect(!Optional(" ").isNilOrEmpty, "Whitespace is not empty") + } + + @Test("Works for any collection, not only strings") + func collectionIsNilOrEmpty() { + let noArray: [Int]? = nil + #expect(noArray.isNilOrEmpty) + #expect(Optional([Int]()).isNilOrEmpty) + #expect(!Optional([1, 2]).isNilOrEmpty) + #expect(Optional([String: Int]()).isNilOrEmpty) + #expect(!Optional(["a": 1]).isNilOrEmpty) + } +} diff --git a/openHAB/Models/SitemapPageViewModel.swift b/openHAB/Models/SitemapPageViewModel.swift index d7385d8dc..afa61ecde 100644 --- a/openHAB/Models/SitemapPageViewModel.swift +++ b/openHAB/Models/SitemapPageViewModel.swift @@ -665,8 +665,7 @@ extension SitemapPageViewModel { // of origin. Checking only .initialPoll was insufficient because the loader emits // .initialFetch(nil) when the server returns no page, skipping updateUI entirely; // the next update arrives as .longPolling and the destination was never consumed. - if let nav = pendingLinkedPageNavigation { - pendingLinkedPageNavigation = nil + if let nav = pendingLinkedPageNavigation.take() { navigationPath.append(nav) } diff --git a/openHAB/UI/ScreenSaver/ScreenSaverManager.swift b/openHAB/UI/ScreenSaver/ScreenSaverManager.swift index 6d7b3d4cf..2b039a3b7 100644 --- a/openHAB/UI/ScreenSaver/ScreenSaverManager.swift +++ b/openHAB/UI/ScreenSaver/ScreenSaverManager.swift @@ -201,9 +201,8 @@ final class ScreenSaverManager: NSObject { } private func restoreBrightnessIfNeeded() { - guard let original = previousBrightness else { return } + guard let original = previousBrightness.take() else { return } screen?.brightness = original - previousBrightness = nil } /// This is used fo testing the screen saver in the Settings view (before settings are saved) diff --git a/openHAB/UI/SwiftUI/IconView.swift b/openHAB/UI/SwiftUI/IconView.swift index 0dbdbf9f8..e2b038e35 100644 --- a/openHAB/UI/SwiftUI/IconView.swift +++ b/openHAB/UI/SwiftUI/IconView.swift @@ -52,8 +52,7 @@ final class IconReloadCoordinator: ObservableObject { } func didBecomeActive(now: Date = Date()) { - guard let backgroundedAt else { return } - self.backgroundedAt = nil + guard let backgroundedAt = backgroundedAt.take() else { return } if now.timeIntervalSince(backgroundedAt) >= Self.reloadThreshold { reloadEpoch += 1 } diff --git a/openHABWidget/OpenHABIconWatermark.swift b/openHABWidget/OpenHABIconWatermark.swift index 706a19da6..04ec53b82 100644 --- a/openHABWidget/OpenHABIconWatermark.swift +++ b/openHABWidget/OpenHABIconWatermark.swift @@ -28,15 +28,15 @@ struct OpenHABIconWatermark: View { /// Returns an SF Symbol icon for the given openHAB item type func itemTypeIcon(for type: OpenHABItem.ItemType?) -> SFSymbol { - guard let type else { return .circleFill } switch type { - case .switchItem: return .power - case .color: return .paintpalette - case .dimmer: return .lightMax - case .number: return .number - case .stringItem: return .textformat - case .contact: return .doorLeftHandOpen - case .rollershutter: return .blindsVerticalClosed - default: return .circleFill + case nil: .circleFill + case .switchItem: .power + case .color: .paintpalette + case .dimmer: .lightMax + case .number: .number + case .stringItem: .textformat + case .contact: .doorLeftHandOpen + case .rollershutter: .blindsVerticalClosed + default: .circleFill } }