Files
Reflect/Shared/Persisence/PersistenceHelper.swift
Trey t 6c239c5e26 MISC
added new icon
put notifications in strings files
started IAPManager
populate data goes to 1000 entires
take out fixweeday button
2022-07-07 18:13:36 -05:00

97 lines
3.9 KiB
Swift

//
// 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() {
for idx in 1..<25 {
let newItem = MoodEntry(context: viewContext)
newItem.timestamp = Calendar.current.date(byAdding: .day, value: -idx, to: Date())
newItem.moodValue = Int16(Mood.allValues.randomElement()!.rawValue)
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)")
}
}
func generateObjectNotInArray(forDate date: Date = Date(), withMood mood: Mood = .placeholder) -> MoodEntry {
let newItem = MoodEntry(context: childContext)
newItem.timestamp = Date()
newItem.moodValue = Int16(mood.rawValue)
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(Mood.allValues.randomElement()!.rawValue)
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
}
}