Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Last active July 13, 2023 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzyzanowskim/54424342c5288ab25a5fa7b23fafbbc6 to your computer and use it in GitHub Desktop.
Save krzyzanowskim/54424342c5288ab25a5fa7b23fafbbc6 to your computer and use it in GitHub Desktop.
// 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
}
}
)
)
}
}
@krzyzanowskim
Copy link
Author

Screen.Shot.2022-10-30.at.21.53.32.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment