Skip to content

Instantly share code, notes, and snippets.

@malhal
Created July 5, 2024 10:04
Show Gist options
  • Save malhal/973f9a812ad1e734fe38b03309e001a3 to your computer and use it in GitHub Desktop.
Save malhal/973f9a812ad1e734fe38b03309e001a3 to your computer and use it in GitHub Desktop.
Alert not continuously showing on iOS 18b2
import SwiftUI
struct ContentView: View {
struct AlertConfig: AlertPresentable {
var isPresented: Bool = false {
didSet {
if !isPresented {
dismissAction?()
dismissAction = nil
}
}
}
var dismissAction: (() -> Void)? = nil
mutating func present(dismissAction: @escaping () -> Void) {
self.dismissAction = dismissAction
self.isPresented = true
}
}
@State private var config = AlertConfig()
var isPresented: Binding<Bool> {
get {
Binding {
config.isPresented
} set: { b in
config.isPresented = b
}
}
}
var body: some View {
Text("Hello world!")
.alert("Hi", isPresented: $config.isPresented) { // works on 17.5, on 18b2 it only appears once.
// .alert("Hi", isPresented: isPresented) { // works on 17.5 and 18b2.
// rely on default button
}
.task {
while(!Task.isCancelled) {
await withCheckedContinuation { continuation in
config.present {
continuation.resume()
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment