Pow icon indicating copy to clipboard operation
Pow copied to clipboard

Avoid reapplying all RepeatingChangeEffectModifier changes in each onAppear

Open jonasrottmann opened this issue 11 months ago • 1 comments

When using an effect as RepeatingChangeEffectModifier inside a lazy stack, it is possible that onAppear is called multiple times when the view comes back to being visible. That would cause the shine effect to repeat RepeatingChangeEffectModifier.timer.count times when becoming visible again. This PR tries to fix this by pausing/resetting the timer count when the view disappears. I hope this is the correct way to solve this 🙂

ScrollView {
    LazyVStack {
        ForEach(0...50, id: \.self) { _ in
            Button { } label: {
                Label("Shine", systemImage: "sun.horizon.fill")
            }
            .buttonStyle(.borderedProminent)
            .controlSize(.large)
            .conditionalEffect(.repeat(.shine, every: .seconds(1)), condition: true)
        }
    }
}

https://github.com/EmergeTools/Pow/assets/9310224/67531f90-04d9-476d-a283-ada50ec78ebb

jonasrottmann avatar Mar 17 '24 16:03 jonasrottmann