Last active
July 13, 2023 08:49
-
-
Save krzyzanowskim/54424342c5288ab25a5fa7b23fafbbc6 to your computer and use it in GitHub Desktop.
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
// Backport SwiftUI.Toggle.init(_:sources:isOn:) | |
// https://developer.apple.com/documentation/swiftui/toggle/init(_:ison:)-8qx3l | |
@available(iOS, deprecated: 16.0) | |
@available(macOS, deprecated: 13.0) | |
@available(tvOS, deprecated: 16.0) | |
@available(watchOS, deprecated: 9.0) | |
private extension SwiftUI.Toggle where Label == SwiftUI.Text { | |
init<C>(_ titleKey: LocalizedStringKey, sources: C, isOn: KeyPath<C.Element, Binding<Bool>>) where C : RandomAccessCollection { | |
self.init(titleKey, | |
isOn: Binding( | |
get: { | |
sources.allSatisfy { element in | |
element[keyPath: isOn].wrappedValue == true | |
} | |
}, | |
set: { newValue in | |
sources.map { | |
$0[keyPath: isOn] | |
}.forEach { binding in | |
binding.wrappedValue = newValue | |
} | |
} | |
) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Screen.Shot.2022-10-30.at.21.53.32.mp4