Skip to content

Instantly share code, notes, and snippets.

@thatswiftguy
Last active April 11, 2021 16:15
Show Gist options
  • Save thatswiftguy/491b255af0835f324fe612984ba4b387 to your computer and use it in GitHub Desktop.
Save thatswiftguy/491b255af0835f324fe612984ba4b387 to your computer and use it in GitHub Desktop.
//
// AddTaskView.swift
// TaskManager
//
// Created by Mohammad Yasir on 11/04/21.
//
import SwiftUI
struct AddTaskView: View {
@Environment(\.presentationMode) var presentationMode
@ObservedObject var taskVM : TaskViewModel
@State var name = ""
@State var taskName = ""
@State var date = Date()
@State var priority : Priority = .normal
var body: some View {
NavigationView{
Form{
Section(header: Text("Task")) {
TextField("Name ", text : $name)
TextField("Task Name ", text : $taskName)
}
Section {
Picker("Priority", selection: $priority) {
ForEach(Priority.allCases) { priority in
Label(
title: { Text(priority.title) },
icon: { Image(systemName: "exclamationmark.circle") })
.foregroundColor(priority.color)
.tag(priority)
}
}
}
DisclosureGroup("Date") {
DatePicker("", selection: $date)
.datePickerStyle(GraphicalDatePickerStyle())
}
}
.navigationBarTitle("Add", displayMode: .inline)
.navigationBarItems(
leading: Button(action:{
presentationMode.wrappedValue.dismiss() },
label : {
Text("Cancel")
.foregroundColor(.red)
}),
trailing: Button(action:{taskVM.addTask(task: .init(name: name, taskName: taskName, date: date, priority: priority))
presentationMode.wrappedValue.dismiss()},
label:{
Text("Save")
}).disabled(name.isEmpty || taskName.isEmpty)
)
}
}
}
struct AddTaskView_Previews: PreviewProvider {
static var previews: some View {
AddTaskView(taskVM: TaskViewModel())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment