From 4f4acb49757f81c823492902e30c53516d596cc0 Mon Sep 17 00:00:00 2001
From: Oliver Wiese <oliver.wiese@fu-berlin.de>
Date: Fri, 20 Mar 2020 18:35:52 +0100
Subject: [PATCH] fix new mails bug: No update in inbox view

---
 .../PersistentMail +CoreDataProperties.swift  |  2 +-
 enzevalos_iphone/SwiftUI/Inbox/Inbox.swift    |  2 +-
 .../SwiftUI/Inbox/InboxCoordinator.swift      |  2 +-
 .../SwiftUI/Inbox/KeyRecordRow.swift          | 34 ++++++++++++++++---
 4 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/enzevalos_iphone/PersistentMail +CoreDataProperties.swift b/enzevalos_iphone/PersistentMail +CoreDataProperties.swift
index e032c064..16127680 100644
--- a/enzevalos_iphone/PersistentMail +CoreDataProperties.swift	
+++ b/enzevalos_iphone/PersistentMail +CoreDataProperties.swift	
@@ -23,7 +23,7 @@ import CoreData
 
 extension PersistentMail {
 
-    @nonobjc open override class func fetchRequest() -> NSFetchRequest<NSFetchRequestResult> {
+    @nonobjc public class func fetchRequest() -> NSFetchRequest<PersistentMail> {
         return NSFetchRequest(entityName: "PersistentMail");
     }
 
diff --git a/enzevalos_iphone/SwiftUI/Inbox/Inbox.swift b/enzevalos_iphone/SwiftUI/Inbox/Inbox.swift
index 9a35defc..97a2a50f 100644
--- a/enzevalos_iphone/SwiftUI/Inbox/Inbox.swift
+++ b/enzevalos_iphone/SwiftUI/Inbox/Inbox.swift
@@ -45,7 +45,7 @@ struct Inbox: View {
     private var mailList: some View {
         List (self.keyrecords.filter(filterKeyRecord), id: \.self){
             record in
-                KeyRecordRow(keyrecord: record, coord: self.coord)
+            KeyRecordRow(keyrecord: record, coord: self.coord).environment(\.managedObjectContext, self.managedObjectContext)
        }
        .resignKeyboardOnDragGesture() // hide keyboard when dragging
     }
diff --git a/enzevalos_iphone/SwiftUI/Inbox/InboxCoordinator.swift b/enzevalos_iphone/SwiftUI/Inbox/InboxCoordinator.swift
index af075479..f1e3b56b 100644
--- a/enzevalos_iphone/SwiftUI/Inbox/InboxCoordinator.swift
+++ b/enzevalos_iphone/SwiftUI/Inbox/InboxCoordinator.swift
@@ -40,7 +40,7 @@ class InboxCoordinator {
         }
         else {
             let context = DataHandler.handler.managedObjectContext
-            let vc = UIHostingController(rootView: Inbox(coord: self).environment(\.managedObjectContext,context))
+            let vc = UIHostingController(rootView: Inbox(coord: self).environment(\.managedObjectContext, context))
             inbox = vc
             root.pushViewController(vc, animated: true)
         }
diff --git a/enzevalos_iphone/SwiftUI/Inbox/KeyRecordRow.swift b/enzevalos_iphone/SwiftUI/Inbox/KeyRecordRow.swift
index e8bc192f..4c852518 100644
--- a/enzevalos_iphone/SwiftUI/Inbox/KeyRecordRow.swift
+++ b/enzevalos_iphone/SwiftUI/Inbox/KeyRecordRow.swift
@@ -9,9 +9,27 @@
 import SwiftUI
 
 struct KeyRecordRow: View {
-    @ObservedObject var keyrecord: KeyRecord
+    @Environment(\.managedObjectContext) var managedObjectContext
+
+    var keyrecord: KeyRecord
     let coord: InboxCoordinator
     
+    var fetchRequest: FetchRequest<PersistentMail>
+    var mails: FetchedResults<PersistentMail> {
+        fetchRequest.wrappedValue
+    }
+
+    init(keyrecord: KeyRecord, coord: InboxCoordinator) {
+        self.keyrecord = keyrecord
+        self.coord = coord
+        let pred = NSPredicate(format: "record == %@", keyrecord)
+        let folderPredicate = NSPredicate(format: "folder == %@", Folder.inbox)
+        let predicates = NSCompoundPredicate(andPredicateWithSubpredicates: [pred, folderPredicate])
+        let sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]
+        fetchRequest = FetchRequest<PersistentMail>(entity: PersistentMail.entity(), sortDescriptors: sortDescriptors, predicate: predicates, animation: nil)
+    }
+    
+    
     var body: some View {
         VStack {
             HStack(alignment: .lastTextBaseline) {
@@ -28,6 +46,13 @@ struct KeyRecordRow: View {
         }
     }
     
+    var secondMail: PersistentMail?{
+        if mails.count > 1 {
+            return mails[2]
+        }
+        return nil
+    }
+    
     /*
      Displays the first (and second) mail
      */
@@ -35,11 +60,11 @@ struct KeyRecordRow: View {
         let spacing = CGFloat(10)
         let offset = -(spacing / 2)
         
-        if let first = self.keyrecord.firstMail {
-            if let second = self.keyrecord.secondMail {
+        if let first = mails.first {
+            if mails.count > 1 {
                 return AnyView(VStack(alignment: .leading, spacing: spacing) {
                         MailView(mail: first, coord: coord)
-                        MailView(mail: second, coord: coord)
+                        MailView(mail: mails[1], coord: coord)
                         .background(Stroke(offsetY: offset))
                 })
             } else {
@@ -85,6 +110,7 @@ struct KeyRecordRow: View {
 
 
 extension KeyRecord {
+    
     public var firstMail: PersistentMail? {
         get {
             return mails.first
-- 
GitLab