Last active
August 17, 2024 07:35
-
-
Save VAndrJ/3b145a07e16c899ea690db168833baec to your computer and use it in GitHub Desktop.
Alert close problem MRE.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
@main | |
struct ExampleApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
} | |
} | |
} | |
let colors: [Color] = [.red, .green, .blue, .orange, .yellow, .purple, .pink, .brown, .gray, .white] | |
struct ContentView: View { | |
@State private var timer = Timer.publish(every: 0.05, on: .main, in: .common).autoconnect() | |
@State private var isAlertPresented = false | |
@State private var counter = 0 | |
var body: some View { | |
let _ = Self._printChanges() | |
VStack { | |
Button("Show alert") { | |
isAlertPresented = true | |
} | |
Rectangle() | |
.frame(width: 10, height: 10) | |
.foregroundStyle(colors[counter % colors.count]) | |
} | |
.alert( | |
"Alert", | |
isPresented: $isAlertPresented, | |
actions: { | |
Button("OK") {} | |
}, | |
message: { | |
Text("Dismiss me.") | |
} | |
) | |
.onReceive(timer) { _ in | |
counter += 1 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
@main | |
struct ExampleApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
} | |
} | |
} | |
let colors: [Color] = [.red, .green, .blue, .orange, .yellow, .purple, .pink, .brown, .gray, .white] | |
// Alert close problem MRE. | |
struct ContentView: View { | |
@State private var timer = Timer.publish(every: 0.05, on: .main, in: .common).autoconnect() | |
@State private var isAlertPresented = false | |
@State private var counter = 0 | |
var body: some View { | |
let _ = Self._printChanges() | |
VStack { | |
Button("Show alert") { | |
isAlertPresented = true | |
} | |
Rectangle() | |
.frame(width: 10, height: 10) | |
.foregroundStyle(colors[counter % colors.count]) | |
} | |
.alert(isPresented: $isAlertPresented) { | |
Alert( | |
title: Text("Alert"), | |
message: Text("Dismiss me."), | |
dismissButton: .default(Text("OK")) { | |
isAlertPresented = false | |
} | |
) | |
} | |
.onReceive(timer) { _ in | |
counter += 1 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
@main | |
struct ExampleApp: App { | |
@State private var timer = Timer.publish(every: 0.05, on: .main, in: .common).autoconnect() | |
@State private var counter = Counter() | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
.environmentObject(counter) | |
.onReceive(timer) { _ in | |
counter.value += 1 | |
} | |
} | |
} | |
} | |
let colors: [Color] = [.red, .green, .blue, .orange, .yellow, .purple, .pink, .brown, .gray, .white] | |
final class Counter: ObservableObject { | |
@Published var value = 0 | |
} | |
struct ContentView: View { | |
// Even when it is not used in `body`, but only present in `View`, `body` is still constantly called. | |
@EnvironmentObject var counter: Counter // Remove this line and compare result. | |
@State private var isAlertPresented = false | |
var body: some View { | |
let _ = Self._printChanges() | |
Button("Show alert") { | |
isAlertPresented = true | |
} | |
.alert(isPresented: $isAlertPresented) { | |
Alert( | |
title: Text("Alert"), | |
message: Text("Dismiss me."), | |
dismissButton: .default(Text("OK")) { | |
isAlertPresented = false | |
} | |
) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
@main | |
struct ExampleApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
} | |
} | |
} | |
let colors: [Color] = [.red, .green, .blue, .orange, .yellow, .purple, .pink, .brown, .gray, .white] | |
struct ContentView: View { | |
@State private var timer = Timer.publish(every: 0.05, on: .main, in: .common).autoconnect() | |
@State private var isAlertPresented = false | |
@State private var counter = 0 | |
var body: some View { | |
let _ = Self._printChanges() | |
VStack { | |
Button("Show alert") { | |
isAlertPresented = true | |
} | |
// In this case the `IndicatorView` `body` is called constantly, not `ContentView`, like in AlertProblemMRE.swift | |
IndicatorView(counter: $counter) | |
} | |
.alert(isPresented: $isAlertPresented) { | |
Alert( | |
title: Text("Alert"), | |
message: Text("Dismiss me."), | |
dismissButton: .default(Text("OK")) {} | |
) | |
} | |
.onReceive(timer) { _ in | |
counter += 1 | |
} | |
} | |
} | |
struct IndicatorView: View { | |
@Binding var counter: Int | |
var body: some View { | |
Rectangle() | |
.frame(width: 10, height: 10) | |
.foregroundStyle(colors[counter % colors.count]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment