Skip to content
Snippets Groups Projects
Commit 063636f8 authored by hannes's avatar hannes
Browse files

added function to persistentdataprovider

parent 79bd4aa5
Branches
No related tags found
1 merge request!85Thesis Hannes Staging
......@@ -53,8 +53,9 @@ struct SimpleMailRowView <M: DisplayMail>: View {
}
}
/*
struct SimpleMailRow_Previews: PreviewProvider {
static var previews: some View {
SimpleMailRowView(mail: ProxyData.PlainMail)
}
}
}*/
......@@ -420,6 +420,53 @@ class PersistentDataProvider {
}
}
func setCategoryOpenability(category:CategoryIDType,filter: @escaping MailFilter) throws{
var performError: Error? = nil
let queue = DispatchQueue(label: "AddCategoryEntriesToDatabase", qos: .userInitiated)
queue.async {
let predicate = NSPredicate(format:
//filter for mails not categorized by this category (no entry in inCategory for this category)
"SUBQUERY(inCategory, $category, $category.categoryName == %@ AND $category.openability > -1).@count == 0"
,category)
let taskContext = self.newTaskContext()
let sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
if let unclassifiedMailRecords = self.generateFetchResultController(
enitityName: MailRecord.entityName,
sortDescriptors: sortDescriptors,
predicate: predicate,
propertiesToFetch: nil,
fetchLimit: PersistentDataProvider.FETCHLIMIT,
inViewContext: false,
context: taskContext
).fetchedObjects as? [MailRecord]{
taskContext.performAndWait {
for mail in unclassifiedMailRecords {
let categoryRec = CategoryRecord(context: taskContext)
categoryRec.categoryName = category
categoryRec.openability = filter(mail).rawValue
mail.addToInCategory(categoryRec)
}
//save
do {try self.save(taskContext: taskContext)}
catch {performError = error}
}
}else{
performError = PersistentDataError.missingData
}
}
if let error = performError {
throw error
}
}
func save(taskContext: NSManagedObjectContext) throws -> () {
// Save all insertions and deletions from the context to the store.
if taskContext.hasChanges {
......@@ -681,6 +728,45 @@ class PersistentDataProvider {
return controller
}
func generateNoMailsController()->NSFetchedResultsController<MailRecord> {
let sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
return generateFetchResultController(enitityName: MailRecord.entityName, sortDescriptors: sortDescriptors, predicate: nil, propertiesToFetch: nil, fetchLimit: PersistentDataProvider.FETCHLIMIT, inViewContext: true, context: nil)
}
func generateFetchedMailsInFolderAndCategoryResultsController(folderpath: String, category:String, andPredicate:NSPredicate=NSPredicate(value: true), important:Bool = false) -> NSFetchedResultsController<MailRecord> {
let sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]
let mustOpenPredicate = NSPredicate(format:
//filter for mails that must be opened by this category
"SUBQUERY(inCategory, $category, $category.categoryName == %@ AND $category.openability == 3).@count > 0"
,category)
let openablePredicate = NSPredicate(format:
//filter for mails beeing openable by this category (at least .canOpen (= 1))
"SUBQUERY(inCategory, $category, $category.categoryName == %@ AND $category.openability > \(important ? 1 : 0)).@count > 0 " +
//where no other categories have a .mustOpen (= 3)
"AND SUBQUERY(inCategory, $category, $category.categoryName != %@ AND $category.openability == 3).@count == 0"
,category)
//an eMail is in this category if it must open it OR it can open it and no other kategory mustOPen
let inCategoryPredicate = NSCompoundPredicate(type: .or, subpredicates: [mustOpenPredicate,openablePredicate])
let inFolderPredicate = NSPredicate(format: "inFolder.path == %@", folderpath)
//it also has to be in this folder (folders have higher rank than categories)
let predicate = NSCompoundPredicate(type: .and, subpredicates: [inCategoryPredicate,inFolderPredicate,andPredicate])
return generateFetchResultController(enitityName: MailRecord.entityName, sortDescriptors: sortDescriptors, predicate: predicate, propertiesToFetch: nil, fetchLimit: PersistentDataProvider.FETCHLIMIT, inViewContext: true, context: nil)
}
func generateFetchedMailsInFolderResultsController(folderpath: String) -> NSFetchedResultsController<MailRecord> {
let sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
let predicate = NSPredicate(format: "inFolder.path == %@", folderpath)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment