init commit - bring project over from Mood

This commit is contained in:
Trey t
2022-01-10 09:04:38 -06:00
parent ff5d9bea18
commit 58697bf965
30 changed files with 1840 additions and 121 deletions

View File

@@ -2,19 +2,67 @@
// FeelsApp.swift
// Shared
//
// Created by Trey Tartt on 1/10/22.
// Created by Trey Tartt on 1/5/22.
//
import SwiftUI
import BackgroundTasks
@main
struct FeelsApp: App {
@Environment(\.scenePhase) private var scenePhase
let persistenceController = PersistenceController.shared
init() {
// persistenceController.fillInMissingDates()
BGTaskScheduler.shared.cancelAllTaskRequests()
BGTaskScheduler.shared.register(forTaskWithIdentifier: BGTask.updateDBMissingID, using: nil) { (task) in
BGTask.runFillInMissingDatesTask(task: task as! BGProcessingTask)
}
}
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
}.onChange(of: scenePhase) { phase in
if phase == .background {
BGTask.scheduleBackgroundProcessing()
print("background")
}
}
}
}
class BGTask {
static let updateDBMissingID = "com.88oak.dbUpdateMissing"
class func runFillInMissingDatesTask(task: BGProcessingTask) {
BGTask.scheduleBackgroundProcessing()
task.expirationHandler = {
task.setTaskCompleted(success: false)
}
PersistenceController.shared.fillInMissingDates()
task.setTaskCompleted(success: true)
}
@available(iOS 13.0, *)
class func scheduleBackgroundProcessing() {
let request = BGProcessingTaskRequest(identifier: BGTask.updateDBMissingID)
request.requiresNetworkConnectivity = false
request.requiresExternalPower = false
var runDate = Calendar.current.date(byAdding: .day, value: 1, to: Date())
runDate = Calendar.current.date(bySettingHour: 0, minute: 1, second: 0, of: runDate!)
request.earliestBeginDate = runDate
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule image fetch: (error)")
}
}
}