import SwiftUI struct CollectionAlertsModifier: ViewModifier { @Binding var isNewCollectionAlertPresented: Bool @Binding var newCollectionName: String @Binding var isCollectionErrorAlertPresented: Bool let onCreate: (String) -> Void func body(content: Content) -> some View { content .alert( "New Collection", isPresented: $isNewCollectionAlertPresented ) { TextField("Collection Name", text: $newCollectionName) Button("Cancel") { newCollectionName = "" isNewCollectionAlertPresented = false } Button("Create") { if newCollectionName.isEmpty { isCollectionErrorAlertPresented = true } else { onCreate(newCollectionName) newCollectionName = "" isNewCollectionAlertPresented = false } } } .alert( "Error", isPresented: $isCollectionErrorAlertPresented ) { Button("OK", role: .cancel) { () } } message: { Text("Collection name cannot be empty.") } } } extension View { func collectionAlerts( isNewCollectionAlertPresented: Binding, newCollectionName: Binding, isCollectionErrorAlertPresented: Binding, onCreate: @escaping (String) -> Void ) -> some View { modifier( CollectionAlertsModifier( isNewCollectionAlertPresented: isNewCollectionAlertPresented, newCollectionName: newCollectionName, isCollectionErrorAlertPresented: isCollectionErrorAlertPresented, onCreate: onCreate ) ) } }