Merge branch 'develop' into sharing
This commit is contained in:
@@ -34,7 +34,12 @@ class PersistenceController {
|
||||
return data.first
|
||||
}
|
||||
|
||||
public func add(mood: Mood, forDate date: Date) {
|
||||
public func add(mood: Mood, forDate date: Date, entryType: EntryType) {
|
||||
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)
|
||||
@@ -42,6 +47,7 @@ class PersistenceController {
|
||||
newItem.weekDay = Int16(Calendar.current.component(.weekday, from: date))
|
||||
newItem.canEdit = true
|
||||
newItem.canDelete = true
|
||||
newItem.entryType = Int16(entryType.rawValue)
|
||||
|
||||
do {
|
||||
try viewContext.save()
|
||||
@@ -139,14 +145,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, entryType: .listView)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -202,6 +220,8 @@ class PersistenceController {
|
||||
for description in container.persistentStoreDescriptions {
|
||||
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
|
||||
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
|
||||
description.setOption(true as NSNumber, forKey: NSMigratePersistentStoresAutomaticallyOption)
|
||||
description.setOption(true as NSNumber, forKey: NSInferMappingModelAutomaticallyOption)
|
||||
}
|
||||
|
||||
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
|
||||
|
||||
Reference in New Issue
Block a user