txtodo

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

ContentView.swift (4321B)


      1 //
      2 //  ContentView.swift
      3 //  Shared
      4 //
      5 //  Created by FIGBERT on 7/27/20.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct ContentView: View {
     11     @Environment(\.managedObjectContext) var managedObjectContext
     12     @StateObject var storeManager: StoreManager
     13     @FetchRequest(
     14         entity: Task.entity(),
     15         sortDescriptors: [
     16             NSSortDescriptor(keyPath: \Task.completed, ascending: true),
     17             NSSortDescriptor(keyPath: \Task.priority, ascending: false),
     18             NSSortDescriptor(keyPath: \Task.name, ascending: true)
     19         ],
     20         predicate: NSPredicate(format: "daily == %d", false)
     21     ) var floatingTasks: FetchedResults<Task>
     22     @FetchRequest(
     23         entity: Task.entity(),
     24         sortDescriptors: [
     25             NSSortDescriptor(keyPath: \Task.completed, ascending: true),
     26             NSSortDescriptor(keyPath: \Task.priority, ascending: false),
     27             NSSortDescriptor(keyPath: \Task.name, ascending: true)
     28         ],
     29         predicate: NSPredicate(
     30             format: "daily == %d AND date < %@",
     31             argumentArray: [
     32                 true,
     33                 Calendar.current.startOfDay(
     34                     for: Calendar.current.date(
     35                         byAdding: .day,
     36                         value: 1,
     37                         to: Date()
     38                     )!
     39                 )
     40             ]
     41         )
     42     ) var dailyTasks: FetchedResults<Task>
     43     let currentDay = Calendar.current.component(.day, from: Date.init())
     44 
     45     var body: some View {
     46         ZStack(alignment: .bottomTrailing) {
     47             #if os(iOS)
     48                 Color.gray.opacity(0.25)
     49                     .edgesIgnoringSafeArea(.all)
     50             #endif
     51             ScrollView(.vertical, showsIndicators: false) {
     52                 VStack(alignment: .leading, spacing: 10) {
     53                     HomeHeaderView().padding(.top)
     54                     if floatingTasks.count > 0 {
     55                         SectionLabel(text: "floating")
     56                         ForEach(self.floatingTasks, id: \.id) { task in
     57                             TaskView(task: task, priority: Int(task.priority))
     58                                 .environment(\.managedObjectContext, self.managedObjectContext)
     59                                 .onAppear(perform: {
     60                                     if task.completed && Calendar.current.component(.day, from: task.date) < self.currentDay {
     61                                         self.managedObjectContext.performAndWait {
     62                                             self.managedObjectContext.delete(task)
     63                                             try? self.managedObjectContext.save()
     64                                         }
     65                                     }
     66                                 })
     67                         }
     68                     }
     69                     if dailyTasks.count > 0 {
     70                         SectionLabel(text: "daily")
     71                         ForEach(self.dailyTasks, id: \.id) { task in
     72                             TaskView(task: task, priority: Int(task.priority))
     73                                 .environment(\.managedObjectContext, self.managedObjectContext)
     74                                 .onAppear(perform: {
     75                                     if Calendar.current.component(.day, from: task.date) < self.currentDay {
     76                                         self.managedObjectContext.performAndWait {
     77                                             self.managedObjectContext.delete(task)
     78                                             try? self.managedObjectContext.save()
     79                                         }
     80                                     }
     81                                 })
     82                         }
     83                     }
     84                     AddTaskController(lessThanThreeFloatingTasks: floatingTasks.count < 3)
     85                         .environment(\.managedObjectContext, self.managedObjectContext)
     86                     Spacer()
     87                 }
     88                 .horizontalPaddingOnIOS()
     89                 .animation(.easeIn(duration: 0.15))
     90             }
     91             #if os(iOS)
     92                 MenuView(storeManager: storeManager)
     93             #endif
     94         }
     95             .modifier(FrameModifier())
     96     }
     97 }
     98 
     99 struct ContentView_Previews: PreviewProvider {
    100     static var previews: some View {
    101         ContentView(storeManager: StoreManager())
    102     }
    103 }