Skip to content
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
25 changes: 25 additions & 0 deletions OpenHABCore/Sources/OpenHABCore/Util/OptionalExtension.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 0 additions & 4 deletions OpenHABCore/Sources/OpenHABCore/Util/StringExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,6 @@ public extension String? {
""
}
}

var isNilOrEmpty: Bool {
self == nil || self!.isEmpty
}
}

public extension String {
Expand Down
48 changes: 48 additions & 0 deletions OpenHABCore/Tests/OpenHABCoreTests/OptionalExtensionTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 1 addition & 2 deletions openHAB/Models/SitemapPageViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions openHAB/UI/ScreenSaver/ScreenSaverManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions openHAB/UI/SwiftUI/IconView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions openHABWidget/OpenHABIconWatermark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading