work around widgets ... not sure if they work

This commit is contained in:
Trey t
2022-01-15 18:05:48 -06:00
parent 0298b3bf5e
commit 7c2de764b4
23 changed files with 375 additions and 199 deletions

View File

@@ -12,21 +12,15 @@ import WidgetKit
// AppDelegate.swift
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// PersistenceController.shared.clearDB()
NotificationCenter.default.addObserver(self,
selector: #selector(fetchChanges),
name: .NSPersistentStoreRemoteChange,
object: PersistenceController.shared.container.persistentStoreCoordinator)
application.registerForRemoteNotifications()
UNUserNotificationCenter.current().delegate = self
return true
}
@objc func fetchChanges(note: Notification) {
WidgetCenter.shared.reloadAllTimelines()
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {

View File

@@ -1,7 +1,7 @@
{
"images" : [
{
"filename" : "Screen Shot 2022-01-13 at 5.02.34 PM.png",
"filename" : "exclamation-solid.svg",
"idiom" : "universal",
"scale" : "1x"
},

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

View File

@@ -0,0 +1 @@
<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="exclamation" class="svg-inline--fa fa-exclamation fa-w-6" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 192 512"><path fill="currentColor" d="M176 432c0 44.112-35.888 80-80 80s-80-35.888-80-80 35.888-80 80-80 80 35.888 80 80zM25.26 25.199l13.6 272C39.499 309.972 50.041 320 62.83 320h66.34c12.789 0 23.331-10.028 23.97-22.801l13.6-272C167.425 11.49 156.496 0 142.77 0H49.23C35.504 0 24.575 11.49 25.26 25.199z"></path></svg>

After

Width:  |  Height:  |  Size: 508 B

View File

@@ -6,8 +6,14 @@
//
import CoreData
import WidgetKit
import Foundation
import UIKit
struct PersistenceController {
class PersistenceController {
private var dataUpdateCall: (() -> Void)?
private let callDelay = 10
static let shared = PersistenceController.persistenceController
private static var persistenceController: PersistenceController {
@@ -38,28 +44,6 @@ struct PersistenceController {
}
}
public func moodEntries(forStartDate date: Date, count: Int) -> [MoodEntry] {
let fetchRequest = NSFetchRequest<MoodEntry>(entityName: "MoodEntry")
fetchRequest.fetchLimit = count
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "forDate", ascending: false)]
// var calendar = Calendar.current
// calendar.timeZone = NSTimeZone.local
// let dateFrom = calendar.startOfDay(for: Date())
// Set predicate as date being today's date
// let fromPredicate = NSPredicate(format: "date <= %@", dateFrom as NSDate)
// let datePredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [fromPredicate])
// fetchRequest.predicate = datePredicate
let entries = try! viewContext.fetch(fetchRequest)
if entries.count >= count {
return Array(entries)
} else {
return entries
}
}
public var earliestEntry: MoodEntry? {
let fetchRequest = NSFetchRequest<MoodEntry>(entityName: "MoodEntry")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "forDate", ascending: true)]
@@ -75,6 +59,14 @@ struct PersistenceController {
}
public func getData(startDate: Date, endDate: Date, includedDays: [Int]) -> [MoodEntry] {
try! viewContext.setQueryGenerationFrom(.current)
viewContext.refreshAllObjects()
let fakeRequest = NSFetchRequest<MoodEntry>(entityName: "MoodEntry")
let fakeData = try! viewContext.fetch(fakeRequest)
for item in fakeData {
print(item.forDate!)
}
var includedDays16 = [Int16]()
if includedDays.isEmpty {
@@ -84,17 +76,16 @@ struct PersistenceController {
Int16($0)
})
}
let predicate = NSPredicate(format: "%K >= %@ && %K <= %@ && weekDay IN %@",
"forDate",
let predicate = NSPredicate(format: "forDate >= %@ && forDate <= %@ && weekDay IN %@",
startDate as NSDate,
"forDate",
endDate as NSDate,
includedDays16)
let fetchRequest = NSFetchRequest<MoodEntry>(entityName: "MoodEntry")
fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "forDate", ascending: true)]
return try! viewContext.fetch(fetchRequest)
let data = try! viewContext.fetch(fetchRequest)
return data
}
func populateTestData() {

View File

@@ -8,7 +8,7 @@
import Foundation
class Random {
static var widgetUpdateTime: Date {
static var tomorrowMidnightThirty: Date {
let components = DateComponents(hour: 0, minute: 30, second: 0)
var updateTime = Date()
if let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date()),

View File

@@ -26,7 +26,7 @@ struct AddMoodHeaderView: View {
ForEach(Mood.allValues.reversed()) { mood in
VStack {
Button(action: {
addItem(withMoodValue: mood.rawValue)
addItem(withMood: mood)
}, label: {
mood.icon
.resizable()
@@ -47,21 +47,9 @@ struct AddMoodHeaderView: View {
.padding()
}
private func addItem(withMoodValue moodValue: Int) {
private func addItem(withMood mood: Mood) {
withAnimation {
let newItem = MoodEntry(context: viewContext)
newItem.timestamp = Date()
newItem.moodValue = Int16(moodValue)
newItem.forDate = Date()
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
PersistenceController.shared.add(mood: mood, forDate: Date())
}
}
}

View File

@@ -122,7 +122,7 @@ struct FilterView: View {
for day in 1...numDays {
if let item = monthEntries.filter({ entry in
let components = calendar.dateComponents([.day, .weekday], from: entry.forDate!)
let components = calendar.dateComponents([.day], from: entry.forDate!)
let date = components.day
return day == date
}).first {