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 {
|
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
|
||||||
// PersistenceController.shared.clearDB()
|
// PersistenceController.shared.clearDB()
|
||||||
|
PersistenceController.shared.fillInMissingDates()
|
||||||
UNUserNotificationCenter.current().delegate = self
|
UNUserNotificationCenter.current().delegate = self
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func applicationWillEnterForeground(_ application: UIApplication) {
|
||||||
|
PersistenceController.shared.fillInMissingDates()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AppDelegate: UNUserNotificationCenterDelegate {
|
extension AppDelegate: UNUserNotificationCenterDelegate {
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ struct FeelsApp: App {
|
|||||||
|
|
||||||
if phase == .active {
|
if phase == .active {
|
||||||
UIApplication.shared.applicationIconBadgeNumber = 0
|
UIApplication.shared.applicationIconBadgeNumber = 0
|
||||||
persistenceController.fillInMissingDates()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,11 @@ class PersistenceController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func add(mood: Mood, forDate date: Date) {
|
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)
|
let newItem = MoodEntry(context: viewContext)
|
||||||
newItem.timestamp = Date()
|
newItem.timestamp = Date()
|
||||||
newItem.moodValue = Int16(mood.rawValue)
|
newItem.moodValue = Int16(mood.rawValue)
|
||||||
@@ -139,14 +144,26 @@ class PersistenceController {
|
|||||||
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "forDate", ascending: false)]
|
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "forDate", ascending: false)]
|
||||||
let entries = try! viewContext.fetch(fetchRequest)
|
let entries = try! viewContext.fetch(fetchRequest)
|
||||||
|
|
||||||
if let earliestDate = entries.last?.forDate,
|
if let firstEntry = entries.last?.forDate {
|
||||||
let diffInDays = Calendar.current.dateComponents([.day], from: earliestDate, to: Date()).day,
|
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
|
||||||
diffInDays > 1 {
|
let allDates: [Date] = Date.dates(from: firstEntry, to: yesterday).map({
|
||||||
for idx in 1..<diffInDays {
|
let zeroDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: $0)!
|
||||||
if let searchDay = Calendar.current.date(byAdding: .day, value: -idx, to: Date()),
|
return zeroDate
|
||||||
entries.filter({ Calendar.current.isDate($0.forDate!, inSameDayAs:searchDay) }).isEmpty {
|
})
|
||||||
self.add(mood: .missing, forDate: searchDay)
|
|
||||||
|
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) )
|
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
|
theme.currentTheme.bg
|
||||||
.edgesIgnoringSafeArea(.all)
|
.edgesIgnoringSafeArea(.all)
|
||||||
)
|
)
|
||||||
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
|
||||||
|
PersistenceController.shared.fillInMissingDates()
|
||||||
|
viewModel.updateData()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user