closed #79
This commit is contained in:
@@ -16,9 +16,14 @@ class AppDelegate: NSObject, UIApplicationDelegate {
|
||||
|
||||
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
|
||||
// PersistenceController.shared.clearDB()
|
||||
PersistenceController.shared.fillInMissingDates()
|
||||
UNUserNotificationCenter.current().delegate = self
|
||||
return true
|
||||
}
|
||||
|
||||
func applicationWillEnterForeground(_ application: UIApplication) {
|
||||
PersistenceController.shared.fillInMissingDates()
|
||||
}
|
||||
}
|
||||
|
||||
extension AppDelegate: UNUserNotificationCenterDelegate {
|
||||
|
||||
@@ -36,7 +36,6 @@ struct FeelsApp: App {
|
||||
|
||||
if phase == .active {
|
||||
UIApplication.shared.applicationIconBadgeNumber = 0
|
||||
persistenceController.fillInMissingDates()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,11 @@ class PersistenceController {
|
||||
}
|
||||
|
||||
public func add(mood: Mood, forDate date: Date) {
|
||||
if let existingEntry = getEntry(byDate: date) {
|
||||
viewContext.delete(existingEntry)
|
||||
try? viewContext.save()
|
||||
}
|
||||
|
||||
let newItem = MoodEntry(context: viewContext)
|
||||
newItem.timestamp = Date()
|
||||
newItem.moodValue = Int16(mood.rawValue)
|
||||
@@ -139,14 +144,26 @@ class PersistenceController {
|
||||
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "forDate", ascending: false)]
|
||||
let entries = try! viewContext.fetch(fetchRequest)
|
||||
|
||||
if let earliestDate = entries.last?.forDate,
|
||||
let diffInDays = Calendar.current.dateComponents([.day], from: earliestDate, to: Date()).day,
|
||||
diffInDays > 1 {
|
||||
for idx in 1..<diffInDays {
|
||||
if let searchDay = Calendar.current.date(byAdding: .day, value: -idx, to: Date()),
|
||||
entries.filter({ Calendar.current.isDate($0.forDate!, inSameDayAs:searchDay) }).isEmpty {
|
||||
self.add(mood: .missing, forDate: searchDay)
|
||||
if let firstEntry = entries.last?.forDate {
|
||||
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
|
||||
let allDates: [Date] = Date.dates(from: firstEntry, to: yesterday).map({
|
||||
let zeroDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: $0)!
|
||||
return zeroDate
|
||||
})
|
||||
|
||||
let existingEntries: [Date] = entries.compactMap({
|
||||
if let date = $0.forDate {
|
||||
let zeroDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: date)!
|
||||
return zeroDate
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
let allDatesSet = Set(allDates)
|
||||
let existingEntriesSet = Set(existingEntries)
|
||||
let missing = Array(allDatesSet.subtracting(existingEntriesSet)).sorted(by: >)
|
||||
for date in missing {
|
||||
add(mood: .missing, forDate: date)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,3 +57,17 @@ extension View {
|
||||
clipShape( RoundedCorner(radius: radius, corners: corners) )
|
||||
}
|
||||
}
|
||||
|
||||
extension Date {
|
||||
static func dates(from fromDate: Date, to toDate: Date) -> [Date] {
|
||||
var dates: [Date] = []
|
||||
var date = fromDate
|
||||
|
||||
while date <= toDate {
|
||||
dates.append(date)
|
||||
guard let newDate = Calendar.current.date(byAdding: .day, value: 1, to: date) else { break }
|
||||
date = newDate
|
||||
}
|
||||
return dates
|
||||
}
|
||||
}
|
||||
|
||||
@@ -368,6 +368,10 @@ struct ContentView: View {
|
||||
theme.currentTheme.bg
|
||||
.edgesIgnoringSafeArea(.all)
|
||||
)
|
||||
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
|
||||
PersistenceController.shared.fillInMissingDates()
|
||||
viewModel.updateData()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user