txtodo

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

txtodoApp.swift (2332B)


      1 //
      2 //  txtodoApp.swift
      3 //  Shared
      4 //
      5 //  Created by FIGBERT on 7/27/20.
      6 //
      7 
      8 import SwiftUI
      9 import CoreData
     10 import StoreKit
     11 import UserNotifications
     12 
     13 @main
     14 struct txtodoApp: App {
     15     @Environment(\.scenePhase) private var scenePhase
     16     @StateObject var storeManager = StoreManager()
     17     
     18     let productIDs = [
     19         "com.figbertind.txtodo.five_usd_tip",
     20         "com.figbertind.txtodo.ten_usd_tip",
     21         "com.figbertind.txtodo.fifteen_usd_tip"
     22     ]
     23     
     24     @SceneBuilder var body: some Scene {
     25         WindowGroup {
     26             ContentView(storeManager: storeManager)
     27                 .environment(\.managedObjectContext, self.persistentContainer.viewContext)
     28                 .onAppear(perform: {
     29                     SKPaymentQueue.default().add(storeManager)
     30                     storeManager.getProducts(productIDs: productIDs)
     31                 })
     32         }
     33             .onChange(of: scenePhase) { phase in
     34                 switch phase {
     35                 case .active:
     36                     UNUserNotificationCenter.current().removeAllDeliveredNotifications()
     37                 case .inactive:
     38                     saveContext()
     39                 case .background:
     40                     saveContext()
     41                 @unknown default:
     42                     saveContext()
     43                 }
     44             }
     45         #if os(macOS)
     46         Settings {
     47             SettingsView(storeManager: storeManager)
     48         }
     49         #endif
     50     }
     51     
     52     var persistentContainer: NSPersistentCloudKitContainer = {
     53         let container = NSPersistentCloudKitContainer(name: "txtodo")
     54         container.loadPersistentStores(completionHandler: { (storeDescription, error) in
     55             if let error = error as NSError? {
     56                 fatalError("Unresolved error \(error), \(error.userInfo)")
     57             }
     58         })
     59         container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
     60         container.viewContext.automaticallyMergesChangesFromParent = true
     61         return container
     62     }()
     63     func saveContext() {
     64         let context = persistentContainer.viewContext
     65         if context.hasChanges {
     66             do {
     67                 try context.save()
     68             } catch {
     69                 let nserror = error as NSError
     70                 fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
     71             }
     72         }
     73     }
     74 }