Repository navigation
Use Swift optionals' built-in sugar - #1383
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 tonilby hand. Withtake()the reset can't be forgotten on a new code path.ScreenSaverManager.restoreBrightnessIfNeeded(): the saved brightnessIconReloadCoordinator.didBecomeActive(now:): the time the app went to the backgroundSitemapPageViewModel: the pending linked-page navigationAll three are plain stored properties.
take()always writes back, even when the value is alreadynil, so it isn't used on@State,@Publishedor@Observableproperties, where that would notify observers needlessly.Switching on an optional in the widget's
itemTypeIcon(for:): acase nilarm replaces theguard let, and the switch is now an expression.isNilOrEmptynow works for any optional collection, built on a generalisNil(or:). It replaces theString?-only version, which force-unwrapped. It's in the newOptionalExtension.swift.Not applied
map/flatMapchains: the few nestedif lets use the unwrapped value in more than one statement, whereguard/if letstay clearer.OHBridgeHost,OpenHABItem), andHomeSettingsView's@Stateimage.Tests
OptionalExtensionTestsforisNil(or:)andisNilOrEmptyon strings, arrays and dictionaries.OptionalExtensionTests(4),StringExtensionTests(44) andIconReloadCoordinatorTests(7). The app scheme, including the widget, builds.