Fix a crash in visionOS when nesting `.onChange` inside `#available` (#1984)

This commit is contained in:
Mike Schreiber 2024-03-06 16:56:13 -08:00 коммит произвёл GitHub
Родитель e4fa6fa4b5
Коммит 120798e3d9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 16 добавлений и 0 удалений

Просмотреть файл

@ -14,6 +14,13 @@ extension View {
/// - action: A closure to run when the value changes.
/// - Returns: A view that fires an action when the specified value changes.
func onChange_iOS17<V>(of value: V, _ action: @escaping (V) -> Void) -> some View where V: Equatable {
#if os(visionOS)
// Known bug when using #available and self.onChange together in visionOS: it'll crash!
// So for this OS, just use the new .onChange unconditionally.
return self.onChange(of: value) { _, newValue in
return action(newValue)
}
#else
if #available(iOS 17, *) {
return self.onChange(of: value) { _, newValue in
return action(newValue)
@ -21,5 +28,6 @@ extension View {
} else {
return self.onChange(of: value, perform: action)
}
#endif
}
}

Просмотреть файл

@ -67,6 +67,13 @@ extension View {
/// - action: A closure to run when the value changes.
/// - Returns: A view that fires an action when the specified value changes.
func onChange_iOS17<V>(of value: V, _ action: @escaping (V) -> Void) -> some View where V: Equatable {
#if os(visionOS)
// Known bug when using #available and self.onChange together in visionOS: it'll crash!
// So for this OS, just use the new .onChange unconditionally.
return self.onChange(of: value) { _, newValue in
return action(newValue)
}
#else
if #available(iOS 17, *) {
return self.onChange(of: value) { _, newValue in
return action(newValue)
@ -74,6 +81,7 @@ extension View {
} else {
return self.onChange(of: value, perform: action)
}
#endif
}
}