Skip to content

Instantly share code, notes, and snippets.

@garsdle
Last active March 17, 2020 20:23
Show Gist options
  • Save garsdle/2eced5c82e24c1b173d00c01051fef08 to your computer and use it in GitHub Desktop.
Save garsdle/2eced5c82e24c1b173d00c01051fef08 to your computer and use it in GitHub Desktop.
MirleftCard
struct ContentView: View {
@State var showNewCardView = false
@State var mirleftCards: [MirleftCard] = [
MirleftCard(image: Image("beach"), title: "The Beach", description: "The amazing view of the Grande Plage."),
MirleftCard(image: Image("sunset"), title: "Sunset", description: "The beautiful moroccan sunset."),
]
var body: some View {
NavigationView{
ScrollView {
ForEach(mirleftCards) { card in
CardView(card_image: card.image,
card_title: card.title,
card_description: card.description)
}
}.navigationBarTitle("Mirleft")
.navigationBarItems(trailing:
Button(action: { self.showNewCardView.toggle()}) {
Image(systemName: "folder.fill.badge.plus")
.imageScale(.large)
.padding()
})
.sheet(isPresented: $showNewCardView) {
NewCardView(isPresented: self.$showNewCardView,
onComplete: self.add(card: ))
}
}
}
func add(card: MirleftCard) {
self.mirleftCards.append(card)
}
}
struct NewCardView: View {
@Binding var isPresented: Bool
@State var showActionSheet = false
@State var showImagePicker = false
@State var sourceType: UIImagePickerController.SourceType = .camera
@State var image: UIImage? = nil
@State var title: String = ""
@State var description: String = ""
var onComplete: (MirleftCard) -> Void
var body: some View {
NavigationView {
VStack {
if image != nil {
Image(uiImage: image!)
.resizable()
.scaledToFit()
.clipShape(RoundedRectangle(cornerRadius: 20))
.overlay(TextField("Title", text: $title).foregroundColor(.white).font(.system(size: 34, weight: .heavy, design: .default)).padding(), alignment: .topLeading)
.overlay(TextField("Description", text: $description).foregroundColor(.white).textFieldStyle(CustomTextFieldStyle()).padding(), alignment: .bottomLeading)
.padding()
.padding(.bottom, 150)
} else {
Button(action: { self.showActionSheet.toggle() }) {
Text("Select or take an image.")
.foregroundColor(.black)
.background(RoundedRectangle(cornerRadius: 20)
.fill(Color.init(red: 0.95, green: 0.95, blue: 0.95))
.frame(width: 350, height: 300))
.padding(.bottom, 150)
}
}
Spacer().frame(height: 50)
Button(action: completionRequested) {
Text("Done")
.fontWeight(.bold)
.foregroundColor(.white)
.padding()
.background(RoundedRectangle(cornerRadius: 10)
.fill(Color.blue)
.frame(width: 100))
//.padding(.bottom)
}
} .navigationBarTitle("Add new card")
.navigationBarItems(trailing:
Button(action: {self.isPresented = false} ) {
Text("Cancel")
}
)
.actionSheet(isPresented: $showActionSheet) {
ActionSheet(title: Text("Add a picture to your post"), message: nil, buttons: [
//Button1
.default(Text("Camera"), action: {
self.showImagePicker = true
//self.showNewCardView = true
self.sourceType = .camera
}),
//Button2
.default(Text("Photo Library"), action: {
self.showImagePicker = true
//self.showNewCardView = true
self.sourceType = .photoLibrary
}),
//Button3
.cancel()
])
}
.sheet(isPresented: $showImagePicker){
imagePicker(image: self.$image, showImagePicker: self.$showImagePicker, sourceType: self.sourceType)}
}
}
func completionRequested() {
guard let image = image else {
print("No image!")
// Handle error states
return
}
self.isPresented = false
let newCard = MirleftCard(id: .init(), image: Image(uiImage: image), title: title, description: description)
onComplete(newCard)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment