Skip to content

Use Swift optionals' built-in sugar - #1383

Merged
timbms merged 2 commits into
developfrom
refactor/optional-sugar
Nov 14, 2026
Merged

timbms merged 2 commits into
developfrom
refactor/optional-sugar

Conversation

@timbms

@timbms timbms commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Applies some of the syntactic sugar from John Sundell's Interesting ways of using Swift optionals. Behaviour doesn't change.

Changes

  • take() where a value is read and then set back to nil by hand. With take() the reset can't be forgotten on a new code path.

    • ScreenSaverManager.restoreBrightnessIfNeeded(): the saved brightness
    • IconReloadCoordinator.didBecomeActive(now:): the time the app went to the background
    • SitemapPageViewModel: the pending linked-page navigation

    All three are plain stored properties. take() always writes back, even when the value is already nil, so it isn't used on @State, @Published or @Observable properties, where that would notify observers needlessly.

  • Switching on an optional in the widget's itemTypeIcon(for:): a case nil arm replaces the guard let, and the switch is now an expression.

  • isNilOrEmpty now works for any optional collection, built on a general isNil(or:). It replaces the String?-only version, which force-unwrapped. It's in the new OptionalExtension.swift.

Not applied

  • map/flatMap chains: the few nested if lets use the unwrapped value in more than one statement, where guard/if let stay clearer.
  • Places that reset a property only under a condition (OHBridgeHost, OpenHABItem), and HomeSettingsView's @State image.

Tests

  • New OptionalExtensionTests for isNil(or:) and isNilOrEmpty on strings, arrays and dictionaries.
  • Passed: OptionalExtensionTests (4), StringExtensionTests (44) and IconReloadCoordinatorTests (7). The app scheme, including the widget, builds.

timbms and others added 2 commits November 13, 2026 11:45
Following "Interesting ways of using Swift optionals" by John Sundell
(https://swiftbysundell.com/articles/interesting-ways-of-using-swift-optionals/):

- take() where a value is read and then reset to nil by hand: the screen
  saver's saved brightness, the icon reload timestamp and the pending
  linked-page navigation. These are plain stored properties, so take()
  writing nil back has no observers to notify.
- switch on the optional item type with a nil case in the widget's
  itemTypeIcon, instead of guard-unwrapping first.
- isNilOrEmpty now works for any optional collection, built on a general
  isNil(or:), and no longer force-unwraps. It replaces the String-only
  version.

Signed-off-by: Tim Mueller-Seydlitz <timbms@gmail.com>
@timbms
timbms merged commit e9315f1 into develop Nov 14, 2026
8 checks passed
@timbms
timbms deleted the refactor/optional-sugar branch November 14, 2026 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant