wip
This commit is contained in:
107
Shared/Persisence/PersistenceHelper.swift
Normal file
107
Shared/Persisence/PersistenceHelper.swift
Normal file
@@ -0,0 +1,107 @@
|
||||
//
|
||||
// PersistenceHelper.swift
|
||||
// Feels (iOS)
|
||||
//
|
||||
// Created by Trey Tartt on 2/17/22.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
|
||||
extension PersistenceController {
|
||||
public func randomEntries(count: Int) -> [MoodEntry] {
|
||||
var entries = [MoodEntry]()
|
||||
|
||||
for idx in 0..<count {
|
||||
let newItem = MoodEntry(context: childContext)
|
||||
newItem.timestamp = Calendar.current.date(byAdding: .day, value: -idx, to: Date())
|
||||
newItem.moodValue = Int16(Mood.allValues.randomElement()!.rawValue)
|
||||
let date = Calendar.current.date(byAdding: .day, value: -idx, to: Date())!
|
||||
newItem.forDate = date
|
||||
newItem.weekDay = Int16(Calendar.current.component(.weekday, from: date))
|
||||
newItem.canEdit = true
|
||||
newItem.canDelete = true
|
||||
entries.append(newItem)
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
func populateMemory() {
|
||||
#if debug
|
||||
for idx in 1..<255 {
|
||||
let newItem = MoodEntry(context: viewContext)
|
||||
newItem.timestamp = Calendar.current.date(byAdding: .day, value: -idx, to: Date())
|
||||
newItem.moodValue = Int16.random(in: 2 ... 4)
|
||||
if idx % 5 == 0 {
|
||||
newItem.moodValue = Int16.random(in: 0 ... 4)
|
||||
}
|
||||
newItem.canEdit = true
|
||||
newItem.canDelete = true
|
||||
|
||||
let date = Calendar.current.date(byAdding: .day, value: -idx, to: Date())!
|
||||
newItem.forDate = date
|
||||
newItem.weekDay = Int16(Calendar.current.component(.weekday, from: 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)")
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
func generateObjectNotInArray(forDate date: Date = Date(), withMood mood: Mood = .placeholder) -> MoodEntry {
|
||||
let newItem = MoodEntry(context: childContext)
|
||||
newItem.timestamp = Date()
|
||||
newItem.moodValue = Int16.random(in: 2 ... 4)
|
||||
if Int16.random(in: 0 ... 400) % 5 == 0 {
|
||||
newItem.moodValue = Int16.random(in: 0 ... 4)
|
||||
}
|
||||
newItem.forDate = date
|
||||
newItem.weekDay = Int16(Calendar.current.component(.weekday, from: Date()))
|
||||
newItem.canEdit = false
|
||||
newItem.canDelete = false
|
||||
return newItem
|
||||
}
|
||||
|
||||
func populateTestData() {
|
||||
do {
|
||||
self.clearDB()
|
||||
try viewContext.save()
|
||||
|
||||
for idx in 1..<1000 {
|
||||
let newItem = MoodEntry(context: viewContext)
|
||||
newItem.timestamp = Date()
|
||||
newItem.moodValue = Int16.random(in: 3 ... 4)
|
||||
if Int16.random(in: 0 ... 400) % 5 == 0 {
|
||||
newItem.moodValue = Int16.random(in: 0 ... 4)
|
||||
}
|
||||
newItem.canEdit = true
|
||||
newItem.canDelete = true
|
||||
|
||||
let date = Calendar.current.date(byAdding: .day, value: -idx, to: Date())!
|
||||
newItem.forDate = date
|
||||
newItem.weekDay = Int16(Calendar.current.component(.weekday, from: date))
|
||||
}
|
||||
|
||||
saveAndRunDataListerners()
|
||||
} 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)")
|
||||
}
|
||||
}
|
||||
|
||||
func longestStreak() -> [MoodEntry] {
|
||||
// let predicate = NSPredicate(format: "forDate == %@", date as NSDate)
|
||||
|
||||
let fetchRequest = NSFetchRequest<MoodEntry>(entityName: "MoodEntry")
|
||||
// fetchRequest.predicate = predicate
|
||||
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "forDate", ascending: true)]
|
||||
let data = try! viewContext.fetch(fetchRequest)
|
||||
return data
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user