txtodo

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

AddNoteView.swift (3323B)


      1 //
      2 //  AddNoteView.swift
      3 //  txtodo
      4 //
      5 //  Created by FIGBERT on 8/6/20.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct AddNoteView: View {
     11     @Environment(\.managedObjectContext) var managedObjectContext
     12     @ObservedObject var task: Task
     13     @State private var config = AddNoteViewConfig()
     14     
     15     var body: some View {
     16         Group {
     17             if !config.addingNote {
     18                 HStack {
     19                     Image(systemName: "plus.square")
     20                     Spacer()
     21                     Text("create a note")
     22                     Spacer()
     23                     Image(systemName: "plus.square")
     24                 }
     25                     .onTapGesture {
     26                         #if os(iOS)
     27                             let generator = UIImpactFeedbackGenerator(style: .medium)
     28                             generator.prepare()
     29                             self.config.addingNote = true
     30                             generator.impactOccurred()
     31                         #else
     32                             self.config.addingNote = true
     33                         #endif
     34                     }
     35             } else {
     36                 HStack {
     37                     Image(systemName: "multiply.square")
     38                         .onTapGesture {
     39                             #if os(iOS)
     40                                 UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
     41                                 let generator = UIImpactFeedbackGenerator(style: .medium)
     42                                 generator.prepare()
     43                                 config.clear()
     44                                 generator.impactOccurred()
     45                             #else
     46                                 config.clear()
     47                             #endif
     48                         }
     49                     Spacer()
     50                     TextField("tap", text: $config.newNoteText, onCommit:  {
     51                         addTask()
     52                     })
     53                         .multilineTextAlignment(.center)
     54                     Spacer()
     55                     Image(systemName: "plus.square")
     56                         .onTapGesture {
     57                             addTask(forceKeyboardClose: true)
     58                         }
     59                 }
     60             }
     61         }
     62             .font(.system(size: 18, weight: .light, design: .rounded))
     63             .foregroundColor(Color.secondary.opacity(0.5))
     64             .multilineTextAlignment(.center)
     65     }
     66     
     67     func addTask(forceKeyboardClose: Bool = false) {
     68         #if os(iOS)
     69             if forceKeyboardClose { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) }
     70         #endif
     71         guard self.config.newNoteText != "" else {return}
     72         #if os(iOS)
     73             let generator = UIImpactFeedbackGenerator(style: .medium)
     74             generator.prepare()
     75         #endif
     76         self.managedObjectContext.performAndWait {
     77             self.task.notes.append(self.config.newNoteText)
     78             try? self.managedObjectContext.save()
     79         }
     80         #if os(iOS)
     81         generator.impactOccurred()
     82         #endif
     83         self.config.clear()
     84     }
     85 }
     86 
     87 struct AddNoteViewConfig {
     88     var addingNote: Bool = false
     89     var newNoteText: String = ""
     90 
     91     mutating func clear() {
     92         addingNote = false
     93         newNoteText = ""
     94     }
     95 }