Skip to content

Commit

Permalink
backport onChange(of:perform) to iOS13
Browse files Browse the repository at this point in the history
resolves aheze#77
  • Loading branch information
Sega-Zero committed Mar 14, 2023
1 parent c4df2fc commit 1c58050
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sources/Templates/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#if os(iOS)
import SwiftUI
import Combine

// MARK: - Shadows

Expand Down Expand Up @@ -349,4 +350,20 @@ extension ForEach: DynamicViewContentProvider where Content: View {
return AnyView(content(data[dataIndex]))
}
}

// MARK: Backport

extension View {
/// A backwards compatible wrapper for iOS 14 `onChange`
@ViewBuilder func onChange<T: Equatable>(of value: T, do: @escaping (T) -> Void) -> some View {
if #available(iOS 14.0, *) {
self.onChange(of: value, perform: `do`)
} else {
self.onReceive(Just(value)) { (value) in
`do`(value)
}
}
}
}

#endif

0 comments on commit 1c58050

Please sign in to comment.