create contentview view model
add background to content view make contentview list split by year / month test data will add 120 days instead of 25 closed #35 closed #36
This commit is contained in:
@@ -84,7 +84,7 @@ class PersistenceController {
|
||||
}
|
||||
|
||||
func populateTestData() {
|
||||
for idx in 1..<25 {
|
||||
for idx in 1..<120 {
|
||||
let newItem = MoodEntry(context: viewContext)
|
||||
newItem.timestamp = Date()
|
||||
newItem.moodValue = Int16(Mood.allValues.randomElement()!.rawValue)
|
||||
@@ -198,6 +198,49 @@ class PersistenceController {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
public func splitIntoYearMonth() -> [Int: [Int: [MoodEntry]]] {
|
||||
let data = PersistenceController.shared.getData(startDate: Date(timeIntervalSince1970: 0),
|
||||
endDate: Date(),
|
||||
includedDays: [1,2,3,4,5,6,7]).sorted(by: {
|
||||
$0.forDate! < $1.forDate!
|
||||
})
|
||||
var returnData = [Int: [Int: [MoodEntry]]]()
|
||||
|
||||
if let earliestEntry = data.first,
|
||||
let lastEntry = data.last {
|
||||
|
||||
let calendar = Calendar.current
|
||||
let components = calendar.dateComponents([.year], from: earliestEntry.forDate!)
|
||||
let earliestYear = components.year!
|
||||
|
||||
let latestComponents = calendar.dateComponents([.year], from: lastEntry.forDate!)
|
||||
let latestYear = latestComponents.year!
|
||||
|
||||
for year in earliestYear...latestYear {
|
||||
var allMonths = [Int: [MoodEntry]]()
|
||||
|
||||
for month in (1...12) {
|
||||
var components = DateComponents()
|
||||
components.month = month
|
||||
components.year = year
|
||||
let startDateOfMonth = Calendar.current.date(from: components)!
|
||||
|
||||
let items = data.filter({ entry in
|
||||
let components = calendar.dateComponents([.month, .year], from: startDateOfMonth)
|
||||
let entryComponents = calendar.dateComponents([.month, .year], from: entry.forDate!)
|
||||
return (components.month == entryComponents.month && components.year == entryComponents.year)
|
||||
})
|
||||
if !items.isEmpty {
|
||||
allMonths[month] = items
|
||||
}
|
||||
}
|
||||
returnData[year] = allMonths
|
||||
}
|
||||
}
|
||||
return returnData
|
||||
}
|
||||
}
|
||||
|
||||
extension NSManagedObjectContext {
|
||||
|
||||
Reference in New Issue
Block a user