Skip to content

Parameter Packs in onChange #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions Sources/SwiftUISupport/Extensions/OnChange.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import SwiftUI

extension View {

public func onChange<each T: Equatable>(each value: repeat each T, action: @escaping () -> Void)
-> some View
{

if #available(iOS 17, *) {

let box = RepeatingBox<repeat each T>(value: (repeat each value))

return self.onChange(
of: box,
perform: { _ in
action()
})

} else {

let box = TupleBox<(repeat each T)>(
value: (repeat each value),
isEqual: { lhs, rhs in

for (left, right) in repeat (each lhs, each rhs) {
guard left == right else { return false }
}
return true

})

return self.onChange(
of: box,
perform: { _ in
action()
})
}

}

}

private struct TupleBox<T>: Equatable {

static func == (lhs: TupleBox<T>, rhs: TupleBox<T>) -> Bool {
lhs.isEqual(lhs.value, rhs.value)
}

let value: T
let isEqual: (T, T) -> Bool

init(
value: T,
isEqual: @escaping (T, T) -> Bool
) {
self.value = value
self.isEqual = isEqual
}

}

@available(iOS 17.0.0, *)
private struct RepeatingBox<each T: Equatable>: Equatable {

static func == (lhs: Self, rhs: Self) -> Bool {
for (left, right) in repeat (each lhs.value, each rhs.value) {
guard left == right else { return false }
}
return true
}

let value: (repeat each T)

}

#if DEBUG

private struct Book: View {

@State var a: Int = 0
@State var b: Int = 0
@State var c: Int = 0

var body: some View {

VStack {

Button("Increment A") {
a += 1
}
Button("Increment B") {
b += 1
}
Button("Increment C") {
c += 1

}
.onChange(each: a, b, c) {
print("a, b, or c changed")
}

}

}

}

#Preview {
Book()
}

#endif
Loading