txtodo

[DORMANT] a minimalist todo list app inspired by jeff huang
git clone git://git.figbert.com/txtodo.git
Log | Files | Refs | README

AddTask.swift (5488B)


      1 //
      2 //  AddTask.swift
      3 //  txtodo
      4 //
      5 //  Created by FIGBERT on 8/3/20.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct AddTaskController: View {
     11     @Environment(\.managedObjectContext) var managedObjectContext
     12     @State private var config = AddTaskControllerConfig()
     13     let lessThanThreeFloatingTasks: Bool
     14     
     15     var body: some View {
     16         HStack {
     17             if config.showingDaily {
     18                 AddTaskView(showingOtherAddTaskView: $config.showingFloating, isAddingDailyTask: true)
     19                     .environment(\.managedObjectContext, self.managedObjectContext)
     20             }
     21             if config.showingDaily && lessThanThreeFloatingTasks && config.showingFloating {
     22                 Spacer()
     23             }
     24             if lessThanThreeFloatingTasks && config.showingFloating {
     25                 AddTaskView(showingOtherAddTaskView: $config.showingDaily, isAddingDailyTask: false)
     26                     .environment(\.managedObjectContext, self.managedObjectContext)
     27             }
     28         }
     29         .horizontalPaddingOnMacOS()
     30     }
     31 }
     32 
     33 struct AddTaskView: View {
     34     @Environment(\.managedObjectContext) var managedObjectContext
     35     @State private var config = AddTaskViewConfig()
     36     @Binding var showingOtherAddTaskView: Bool
     37     let isAddingDailyTask: Bool
     38     
     39     var body: some View {
     40         Group {
     41             if !config.addingTask {
     42                 HStack {
     43                     Image(systemName: "plus.square")
     44                     Spacer()
     45                     Text(isAddingDailyTask ? NSLocalizedString("daily", comment: "") : NSLocalizedString("floating", comment: ""))
     46                     Spacer()
     47                     Image(systemName: "plus.square")
     48                 }
     49                     .onTapGesture {
     50                         #if os(iOS)
     51                             let generator = UIImpactFeedbackGenerator(style: .medium)
     52                             generator.prepare()
     53                             self.config.addingTask = true
     54                             self.showingOtherAddTaskView = false
     55                             generator.impactOccurred()
     56                         #else
     57                             self.config.addingTask = true
     58                             self.showingOtherAddTaskView = false
     59                         #endif
     60                     }
     61             } else {
     62                 HStack {
     63                     Image(systemName: "multiply.square")
     64                         .onTapGesture {
     65                             #if os(iOS)
     66                                 UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
     67                                 let generator = UIImpactFeedbackGenerator(style: .medium)
     68                                 generator.prepare()
     69                                 config.clear()
     70                                 self.showingOtherAddTaskView = true
     71                                 generator.impactOccurred()
     72                             #else
     73                                 config.clear()
     74                                 self.showingOtherAddTaskView = true
     75                             #endif
     76                         }
     77                     Spacer()
     78                     TextField("tap", text: $config.newTaskText, onCommit:  {
     79                         addTask()
     80                     })
     81                         .multilineTextAlignment(.center)
     82                     Picker(
     83                         selection: $config.newTaskPriority,
     84                         label: Text("task priority"),
     85                         content: {
     86                             Text("!").tag(1)
     87                             Text("!!").tag(2)
     88                             Text("!!!").tag(3)
     89                     })
     90                         .pickerStyle(SegmentedPickerStyle())
     91                         .labelsHidden()
     92                     Spacer()
     93                     Image(systemName: "plus.square")
     94                         .onTapGesture {
     95                             addTask(forceKeyboardClose: true)
     96                         }
     97                 }
     98             }
     99         }
    100         .font(.system(size: 18, weight: .light, design: .rounded))
    101         .foregroundColor(Color.secondary.opacity(0.5))
    102         .multilineTextAlignment(.center)
    103     }
    104     
    105     func addTask(forceKeyboardClose: Bool = false) {
    106         #if os(iOS)
    107             if forceKeyboardClose { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) }
    108         #endif
    109         guard self.config.newTaskText != "" else {return}
    110         #if os(iOS)
    111             let generator = UIImpactFeedbackGenerator(style: .medium)
    112             generator.prepare()
    113         #endif
    114         let newTask = Task(context: self.managedObjectContext)
    115         newTask.id = UUID()
    116         newTask.date = Date()
    117         newTask.notes = [String]()
    118         newTask.daily = self.isAddingDailyTask
    119         newTask.name = self.config.newTaskText
    120         newTask.priority = Int16(self.config.newTaskPriority)
    121         try? self.managedObjectContext.save()
    122         #if os(iOS)
    123             generator.impactOccurred()
    124         #endif
    125         self.config.clear()
    126         self.showingOtherAddTaskView = true
    127     }
    128 }
    129 
    130 struct AddTaskControllerConfig {
    131     var showingDaily: Bool = true
    132     var showingFloating: Bool = true
    133 }
    134 
    135 struct AddTaskViewConfig {
    136     var addingTask: Bool = false
    137     var newTaskText: String = ""
    138     var newTaskPriority: Int = 1
    139 
    140     mutating func clear() {
    141         addingTask = false
    142         newTaskText = ""
    143         newTaskPriority = 1
    144     }
    145 }