| @@ -9,10 +9,26 @@ import Combine |
| 9 | 9 | |
| 10 | 10 | struct ContentView: View { |
| 11 | 11 | @ObservedObject var taskStore = TaskDataStore() |
| 12 | | @State var newTask : String = "" |
| 12 | @State private var newTask : String = "" |
| 13 | @State private var taskPriority : String = "" |
| 13 | 14 | @State private var query = "" |
| 14 | | @State var newTaskDate: Date = Date() |
| 15 | @State private var newTaskDate: Date = Date() |
| 15 | 16 | @State private var showingPopover = false |
| 17 | @State private var recurringReminder = false |
| 18 | @State private var recurrenceInteger : Int = 1 |
| 19 | @State private var showAlert: Bool = false |
| 20 | |
| 21 | enum Priority: String, CaseIterable, Identifiable { |
| 22 | case A, B, C |
| 23 | var id: Self { self } |
| 24 | } |
| 25 | @State private var selectedPriority: Priority = .A |
| 26 | |
| 27 | enum Recurrence: String, CaseIterable, Identifiable { |
| 28 | case hours, days, weeks, months |
| 29 | var id: Self { self } |
| 30 | } |
| 31 | @State private var selectedRecurrence: Recurrence = .hours |
| 16 | 32 | |
| 17 | 33 | var filteredTasks: [Task] { |
| 18 | 34 | if query.isEmpty { |
| @@ -25,13 +41,21 @@ struct ContentView: View { |
| 25 | 41 | } |
| 26 | 42 | |
| 27 | 43 | func addNewTask() { |
| 28 | | taskStore.tasks.append(Task( |
| 29 | | id: String(taskStore.tasks.count + 1), |
| 30 | | taskItem: newTask, |
| 31 | | taskDate: newTaskDate |
| 32 | | )) |
| 33 | | self.newTask = "" |
| 34 | | self.showingPopover = false |
| 44 | if newTask.isEmpty { |
| 45 | showAlert = true |
| 46 | } else { |
| 47 | taskStore.tasks.append(Task( |
| 48 | id: (taskStore.tasks.count + 1), |
| 49 | taskItem: newTask, |
| 50 | taskDate: newTaskDate, |
| 51 | taskPriority: selectedPriority.rawValue, |
| 52 | taskRecurrence: recurringReminder, |
| 53 | taskRecurrenceInt: recurrenceInteger, |
| 54 | taskRecurrencePeriod: selectedRecurrence.rawValue |
| 55 | )) |
| 56 | self.newTask = "" |
| 57 | self.showingPopover = false |
| 58 | } |
| 35 | 59 | } |
| 36 | 60 | |
| 37 | 61 | var body: some View { |
| @@ -39,25 +63,51 @@ struct ContentView: View { |
| 39 | 63 | VStack { |
| 40 | 64 | List { |
| 41 | 65 | ForEach(filteredTasks) { task in |
| 42 | | VStack(alignment: .leading) { |
| 43 | | Text(task.taskItem) |
| 44 | | if (Date() > task.taskDate) { |
| 45 | | HStack { |
| 46 | | Text(task.taskDate, style: .date) |
| 47 | | .foregroundColor(.red) |
| 48 | | .font(.caption) |
| 49 | | Text(task.taskDate, style: .time) |
| 50 | | .foregroundColor(.red) |
| 51 | | .font(.caption) |
| 52 | | } |
| 53 | | } else { |
| 54 | | HStack { |
| 55 | | Text(task.taskDate, style: .date) |
| 56 | | .font(.caption) |
| 57 | | Text(task.taskDate, style: .time) |
| 58 | | .font(.caption) |
| 66 | HStack { |
| 67 | VStack(alignment: .leading) { |
| 68 | Text(task.taskItem) |
| 69 | if (Date() > task.taskDate) { |
| 70 | HStack { |
| 71 | Text(task.taskDate, style: .date) |
| 72 | .foregroundColor(.red) |
| 73 | .font(.caption) |
| 74 | Text(task.taskDate, style: .time) |
| 75 | .foregroundColor(.red) |
| 76 | .font(.caption) |
| 77 | if (task.taskRecurrence == true) { |
| 78 | (Text("[+") + Text(String(task.taskRecurrenceInt)) + Text(" ") + Text(task.taskRecurrencePeriod) + Text("]")) |
| 79 | .foregroundColor(.red) |
| 80 | .font(.caption) |
| 81 | } |
| 82 | } |
| 83 | } else { |
| 84 | HStack { |
| 85 | Text(task.taskDate, style: .date) |
| 86 | .font(.caption) |
| 87 | Text(task.taskDate, style: .time) |
| 88 | .font(.caption) |
| 89 | if (task.taskRecurrence == true) { |
| 90 | (Text("[+") + Text(String(task.taskRecurrenceInt)) + Text(" ") + Text(task.taskRecurrencePeriod) + Text("]")) |
| 91 | .font(.caption) |
| 92 | } |
| 93 | } |
| 59 | 94 | } |
| 60 | 95 | } |
| 96 | Spacer() |
| 97 | (Text("[#") + Text(task.taskPriority) + Text("]")) |
| 98 | } |
| 99 | .swipeActions(edge: .trailing) { |
| 100 | Button(role: .destructive) { |
| 101 | print("\(task.taskItem) is being deleted.") |
| 102 | } label: { |
| 103 | Label("Delete", systemImage: "trash") |
| 104 | } |
| 105 | Button() { |
| 106 | print("\(task.taskItem) is being edited.") |
| 107 | // TODO: Add method to edit message in the DataStore |
| 108 | } label: { |
| 109 | Label("Edit", systemImage: "pencil") |
| 110 | } |
| 61 | 111 | } |
| 62 | 112 | } |
| 63 | 113 | .onDelete(perform: self.deleteTask) |
| @@ -85,12 +135,44 @@ struct ContentView: View { |
| 85 | 135 | }) |
| 86 | 136 | } |
| 87 | 137 | .padding() |
| 88 | | HStack { |
| 89 | | VStack { |
| 90 | | TextField("Add Task: ", text: self.$newTask) |
| 91 | | DatePicker("Scheduled: ", selection: $newTaskDate) |
| 138 | Form { |
| 139 | Section(header: Text("Task Details")) { |
| 140 | TextField("Task Title", text: self.$newTask) |
| 141 | .onSubmit { |
| 142 | if newTask.isEmpty { |
| 143 | showAlert = true |
| 144 | } |
| 145 | } |
| 146 | .alert(isPresented: $showAlert) { |
| 147 | Alert(title: Text("Required Field"), |
| 148 | message: Text("You must enter a task title"), |
| 149 | dismissButton: .default(Text("OK"))) |
| 150 | } |
| 151 | Picker("Priority:", selection: $selectedPriority) { |
| 152 | Text("A").tag(Priority.A) |
| 153 | Text("B").tag(Priority.B) |
| 154 | Text("C").tag(Priority.C) |
| 155 | } |
| 156 | DatePicker("Scheduled:", selection: $newTaskDate) |
| 92 | 157 | .datePickerStyle(CompactDatePickerStyle()) |
| 93 | 158 | } |
| 159 | Section(header: Text("Repeating")) { |
| 160 | Toggle("Repeating", isOn: $recurringReminder) |
| 161 | if (recurringReminder == true) { |
| 162 | HStack { |
| 163 | TextField("", value: $recurrenceInteger, formatter: NumberFormatter()) |
| 164 | Stepper(value: $recurrenceInteger, in: 1...100) { |
| 165 | EmptyView() |
| 166 | } |
| 167 | Picker("", selection: $selectedRecurrence) { |
| 168 | Text("Hours").tag(Recurrence.hours) |
| 169 | Text("Days").tag(Recurrence.days) |
| 170 | Text("Weeks").tag(Recurrence.weeks) |
| 171 | Text("Months").tag(Recurrence.months) |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 94 | 176 | } |
| 95 | 177 | .padding() |
| 96 | 178 | Spacer() |