Skip to content

Commit

Permalink
Merge pull request #52 from OmarJalil/master
Browse files Browse the repository at this point in the history
Trigger to Equatable
  • Loading branch information
simibac authored Jan 12, 2025
2 parents 4e4eab2 + d4f6824 commit 4f27682
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
16 changes: 9 additions & 7 deletions Sources/ConfettiSwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public enum ConfettiType:CaseIterable, Hashable {
}

@available(iOS 14.0, macOS 11.0, watchOS 7, tvOS 14.0, *)
public struct ConfettiCannon: View {
@Binding var counter:Int
public struct ConfettiCannon<T: Equatable>: View {
@Binding var trigger: T
@StateObject private var confettiConfig:ConfettiConfig

@State var animate:[Bool] = []
Expand All @@ -74,7 +74,7 @@ public struct ConfettiCannon: View {
/// - repetitionInterval: duration between the repetitions
/// - hapticFeedback: play haptic feedback on explosion

public init(counter:Binding<Int>,
public init(trigger:Binding<T>,
num:Int = 20,
confettis:[ConfettiType] = ConfettiType.allCases,
colors:[Color] = [.blue, .red, .green, .yellow, .pink, .purple, .orange],
Expand All @@ -89,7 +89,7 @@ public struct ConfettiCannon: View {
repetitionInterval:Double = 1.0,
hapticFeedback:Bool = true
) {
self._counter = counter
self._trigger = trigger
var shapes = [AnyView]()

for confetti in confettis{
Expand Down Expand Up @@ -134,13 +134,15 @@ public struct ConfettiCannon: View {
.onAppear(){
firstAppear = true
}
.onChange(of: counter){value in
.onChange(of: trigger){value in
if firstAppear{
for i in 0..<confettiConfig.repetitions{
DispatchQueue.main.asyncAfter(deadline: .now() + confettiConfig.repetitionInterval * Double(i)) {
animate.append(false)
if(value > 0 && value < animate.count){
animate[value-1].toggle()

if confettiConfig.hapticFeedback {
let impactFeedback = UIImpactFeedbackGenerator(style: .heavy)
impactFeedback.impactOccurred()
}

if confettiConfig.hapticFeedback {
Expand Down
8 changes: 4 additions & 4 deletions Sources/View+ConfettiCannon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public extension View {
/// - repetitionInterval: duration between the repetitions
/// - hapticFeedback: enable or disable haptic feedback
///
@ViewBuilder func confettiCannon(
counter: Binding<Int>,
@ViewBuilder func confettiCannon<T>(
trigger: Binding<T>,
num: Int = 20,
confettis: [ConfettiType] = ConfettiType.allCases,
colors: [Color] = [.blue, .red, .green, .yellow, .pink, .purple, .orange],
Expand All @@ -59,11 +59,11 @@ public extension View {
repetitions: Int = 1,
repetitionInterval: Double = 1.0,
hapticFeedback: Bool = true
) -> some View {
) -> some View where T: Equatable {
ZStack {
self
ConfettiCannon(
counter: counter,
trigger: trigger,
num: num,
confettis: confettis,
colors: colors,
Expand Down
8 changes: 4 additions & 4 deletions Tests/ConfettiSwiftUITests/ConfettiSwiftUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import XCTest
import SwiftUI

final class ConfettiSwiftUITests: XCTestCase {
@State var counter = 0
@State var trigger = false

func testExample() {
ConfettiSwiftUI.ConfettiCannon(counter:$counter)
ConfettiSwiftUI.ConfettiCannon(trigger: $trigger)
Button("Animation"){
self.counter += 1
self.trigger.toggle()
}
}

Expand Down

0 comments on commit 4f27682

Please sign in to comment.