Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created July 30, 2020 02:29
Show Gist options
  • Save azamsharp/2e0de04bfc87a4fd4dfd125d39039bba to your computer and use it in GitHub Desktop.
Save azamsharp/2e0de04bfc87a4fd4dfd125d39039bba to your computer and use it in GitHub Desktop.
//
// AZAlert.swift
// HelloAlertSwiftUI
//
// Created by Mohammad Azam on 7/29/20.
// Copyright © 2020 Mohammad Azam. All rights reserved.
//
import SwiftUI
struct AZAlert: View {
let screenSize = UIScreen.main.bounds
var title: String = ""
@Binding var isShown: Bool
@Binding var text: String
var onDone: (String) -> Void = { _ in }
var onCancel: () -> Void = { }
var body: some View {
VStack(spacing: 20) {
Text(title)
.font(.headline)
Text("Message")
TextField("", text: $text)
.textFieldStyle(RoundedBorderTextFieldStyle())
HStack(spacing: 20) {
Button("Done") {
self.isShown = false
self.onDone(self.text)
}
Button("Cancel") {
self.isShown = false
self.onCancel()
}
}
}
.padding()
.frame(width: screenSize.width * 0.7, height: screenSize.height * 0.3)
.background(Color(#colorLiteral(red: 0.9268686175, green: 0.9416290522, blue: 0.9456014037, alpha: 1)))
.clipShape(RoundedRectangle(cornerRadius: 20.0, style: .continuous))
.offset(y: isShown ? 0 : screenSize.height)
.animation(.spring())
.shadow(color: Color(#colorLiteral(red: 0.8596749902, green: 0.854565084, blue: 0.8636032343, alpha: 1)), radius: 6, x: -9, y: -9)
}
}
struct AZAlert_Previews: PreviewProvider {
static var previews: some View {
AZAlert(title: "Add Item", isShown: .constant(true), text: .constant(""))
}
}
//
// ContentView.swift
// HelloAlertSwiftUI
//
// Created by Mohammad Azam on 7/29/20.
// Copyright © 2020 Mohammad Azam. All rights reserved.
//
import SwiftUI
struct ContentView: View {
@State private var isPresented: Bool = false
@State private var text: String = ""
@State private var items = (1...5).map { "\($0)" }
var body: some View {
NavigationView {
ZStack(alignment: .center) {
VStack {
List(self.items, id: \.self) { index in
Text("\(index)")
}
Button("Show Alert") {
self.text = ""
self.isPresented = true
}
Text(text)
}
AZAlert(title: "Add Item", isShown: $isPresented, text: $text, onDone: { text in
self.items.append(text)
})
}
.navigationBarTitle("Add Item")
.navigationBarItems(trailing: Button("Add Item") {
self.isPresented = true
})
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment