From deb530fc5587d26da00784d826a16fbe50be39ff Mon Sep 17 00:00:00 2001
From: Oliver Wiese <oliver.wiese@fu-berlin.de>
Date: Sun, 4 Jun 2017 22:50:01 +0200
Subject: [PATCH] add answer icon

---
 PersistentMail +CoreDataClass.swift          | 17 +++++++++++++++++
 enzevalos_iphone/ContactViewController.swift |  2 +-
 enzevalos_iphone/EphemeralMail.swift         |  6 ++++--
 enzevalos_iphone/Mail.swift                  |  1 +
 enzevalos_iphone/ReadViewController.swift    |  2 +-
 enzevalos_iphone/SendViewController.swift    |  7 ++++---
 6 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/PersistentMail +CoreDataClass.swift b/PersistentMail +CoreDataClass.swift
index fea73f0c..1836f6d3 100644
--- a/PersistentMail +CoreDataClass.swift	
+++ b/PersistentMail +CoreDataClass.swift	
@@ -13,6 +13,8 @@ import CoreData
 
 @objc(PersistentMail)
 open class PersistentMail: NSManagedObject, Mail {
+    public var predecessor: PersistentMail? = nil
+
     
     var showMessage: Bool = false
 
@@ -34,6 +36,21 @@ open class PersistentMail: NSManagedObject, Mail {
             _ = DataHandler.handler.save()
         }
     }
+    
+    var isAnwered: Bool{
+        get {
+            let value = flag.contains(MCOMessageFlag.answered)
+            return value
+        }
+        set {
+            if !newValue {
+                flag.remove(MCOMessageFlag.answered)
+            } else {
+                flag.insert(MCOMessageFlag.answered)
+            }
+            _ = DataHandler.handler.save()
+        }
+    }
 
     var timeString: String {
         var returnString = ""
diff --git a/enzevalos_iphone/ContactViewController.swift b/enzevalos_iphone/ContactViewController.swift
index 11004f61..ac850875 100644
--- a/enzevalos_iphone/ContactViewController.swift
+++ b/enzevalos_iphone/ContactViewController.swift
@@ -147,7 +147,7 @@ class ContactViewController: UIViewController {
             tableView.selectRow(at: myPath, animated: false, scrollPosition: .none)
             performSegue(withIdentifier: "otherRecord", sender: nil)
         } else if (sender as? UIButton)?.titleLabel?.text == NSLocalizedString("invite", comment: "invite contact") {
-            let mail = EphemeralMail(to: NSSet.init(array: keyRecord!.addresses), cc: NSSet.init(), bcc: NSSet.init(), date: Date(), subject: NSLocalizedString("inviteSubject", comment: ""), body: NSLocalizedString("inviteText", comment: ""), uid: 0)
+            let mail = EphemeralMail(to: NSSet.init(array: keyRecord!.addresses), cc: NSSet.init(), bcc: NSSet.init(), date: Date(), subject: NSLocalizedString("inviteSubject", comment: ""), body: NSLocalizedString("inviteText", comment: ""), uid: 0,predecessor: nil)
             performSegue(withIdentifier: "newMail", sender: mail)
         }
     }
diff --git a/enzevalos_iphone/EphemeralMail.swift b/enzevalos_iphone/EphemeralMail.swift
index 846994e7..0a8c2554 100644
--- a/enzevalos_iphone/EphemeralMail.swift
+++ b/enzevalos_iphone/EphemeralMail.swift
@@ -9,7 +9,7 @@
 import Foundation
 
 open class EphemeralMail: Mail {
-
+    
     public var cc: NSSet?
     public var bcc: NSSet?
     public var to: NSSet
@@ -17,8 +17,9 @@ open class EphemeralMail: Mail {
     public var subject: String?
     public var body: String?
     public var uid: UInt64
+    public var predecessor: PersistentMail?
 
-    public init(to: NSSet, cc: NSSet, bcc: NSSet, date: Date, subject: String?, body: String?, uid: UInt64) { //TODO: is casting cc and bcc necessary?
+    public init(to: NSSet, cc: NSSet, bcc: NSSet, date: Date, subject: String?, body: String?, uid: UInt64, predecessor: PersistentMail?) { //TODO: is casting cc and bcc necessary?
         self.cc = cc
         self.bcc = bcc
         self.to = to
@@ -26,5 +27,6 @@ open class EphemeralMail: Mail {
         self.date = date
         self.subject = subject
         self.uid = uid
+        self.predecessor = predecessor
     }
 }
diff --git a/enzevalos_iphone/Mail.swift b/enzevalos_iphone/Mail.swift
index cd8dfd7b..90b33b05 100644
--- a/enzevalos_iphone/Mail.swift
+++ b/enzevalos_iphone/Mail.swift
@@ -18,6 +18,7 @@ public protocol Mail: Comparable {
     var subject: String? { get }
     var body: String? { get }
     var uid: UInt64 { get }
+    var predecessor: PersistentMail?{get}
 }
 
 public func == <T: Mail> (lhs: T, rhs: T) -> Bool {
diff --git a/enzevalos_iphone/ReadViewController.swift b/enzevalos_iphone/ReadViewController.swift
index e7236d4d..51e7003a 100644
--- a/enzevalos_iphone/ReadViewController.swift
+++ b/enzevalos_iphone/ReadViewController.swift
@@ -412,7 +412,7 @@ class ReadViewController: UITableViewController {
                     }
                 }
 
-                let answerMail = EphemeralMail(to: NSSet.init(array: answerTo), cc: NSSet.init(array: answerCC), bcc: [], date: mail!.date, subject: subject, body: body, uid: mail!.uid)
+                let answerMail = EphemeralMail(to: NSSet.init(array: answerTo), cc: NSSet.init(array: answerCC), bcc: [], date: mail!.date, subject: subject, body: body, uid: mail!.uid, predecessor: mail)
 
                 controller.prefilledMail = answerMail
             }
diff --git a/enzevalos_iphone/SendViewController.swift b/enzevalos_iphone/SendViewController.swift
index c6a172ce..22050ddc 100644
--- a/enzevalos_iphone/SendViewController.swift
+++ b/enzevalos_iphone/SendViewController.swift
@@ -243,7 +243,7 @@ class SendViewController: UIViewController {
                     }
                 }
 
-                let mail = EphemeralMail(to: NSSet.init(array: to), cc: NSSet.init(array: cc), bcc: NSSet.init(), date: Date(), subject: NSLocalizedString("inviteSubject", comment: "Subject for the invitation mail"), body: NSLocalizedString("inviteText", comment: "Body for the invitation mail"), uid: 0)
+                let mail = EphemeralMail(to: NSSet.init(array: to), cc: NSSet.init(array: cc), bcc: NSSet.init(), date: Date(), subject: NSLocalizedString("inviteSubject", comment: "Subject for the invitation mail"), body: NSLocalizedString("inviteText", comment: "Body for the invitation mail"), uid: 0,predecessor: nil)
 
 
                 controller.prefilledMail = mail
@@ -373,8 +373,9 @@ class SendViewController: UIViewController {
         } else {
             NSLog("Send successful!")
             if (self.prefilledMail != nil) {
-                self.prefilledMail?.isRead = true
-               // AppDelegate.getAppDelegate().mailHandler.addFlag((self.prefilledMail?.uid)!, flags: MCOMessageFlag.answered)
+                if self.prefilledMail?.predecessor != nil {
+                    self.prefilledMail?.predecessor?.isAnwered = true
+                }
             }
             self.sendCompleted()
         }
-- 
GitLab