From a9e1582b5e12e332edee051419da9a4a7b7976c5 Mon Sep 17 00:00:00 2001
From: Oliver Wiese <oliver.wiese@fu-berlin.de>
Date: Thu, 8 Apr 2021 12:03:58 +0200
Subject: [PATCH] change encryption to toggle (since we can not encrypt every
 time!)

---
 .../SwiftUI/Compose/ComposeHeaderView.swift   | 79 +++++++++----------
 .../SwiftUI/Compose/ComposeModel.swift        |  9 +++
 .../SwiftUI/Compose/RecipientsModel.swift     |  1 +
 3 files changed, 48 insertions(+), 41 deletions(-)

diff --git a/enzevalos_iphone/SwiftUI/Compose/ComposeHeaderView.swift b/enzevalos_iphone/SwiftUI/Compose/ComposeHeaderView.swift
index 1927a048..81664492 100644
--- a/enzevalos_iphone/SwiftUI/Compose/ComposeHeaderView.swift
+++ b/enzevalos_iphone/SwiftUI/Compose/ComposeHeaderView.swift
@@ -31,53 +31,50 @@ struct ComposeViewHeader: View {
                 .disabled(model.recipientsModel.hasNoRecipients)
             }
             
-            // Encryption toggle
-            Toggle("", isOn: $model.encryptionOn)
-                .toggleStyle(EncryptionToggleStyle())
-                .labelsHidden()
+            // Encryption button
+            encryptionButton
         }
     }
     
-    /// Custom styling for the encryption toggle in ComposeViewHeader.
-    struct EncryptionToggleStyle: ToggleStyle {
-        func makeBody(configuration: Configuration) -> some View {
-            Button {
-                configuration.isOn.toggle()
-            } label: {
-                // Separate labels required for desired scaling transition between states
-                if configuration.isOn {
-                    encryptedButtonLabel
-                } else {
-                    unencryptedButtonLabel
-                }
+    
+    /// Encryption state button view
+    private var encryptionButton: some View {
+        Button {
+            model.toogleEncryption()
+        } label: {
+            // Separate labels required for desired scaling transition between states
+            if model.encryptionOn {
+                encryptedButtonLabel
+            } else {
+                unencryptedButtonLabel
             }
-            .frame(width: 40, height: 40)
         }
-        
-        /// Label style for encryption button when encryption is activated.
-        private var encryptedButtonLabel: some View {
-            ZStack {
-                Circle()
-                    .stroke(Color.blue, lineWidth: 2)
-                
-                Image(systemName: "lock.fill")
-                    .font(Font.system(size: 24, weight: Font.Weight.light))
-                    .foregroundColor(.blue)
-            }
-            .transition(AnyTransition.opacity.combined(with: .scale))
+        .frame(width: 40, height: 40)
+    }
+    
+    /// Label style for encryption button when encryption is activated.
+    private var encryptedButtonLabel: some View {
+        ZStack {
+            Circle()
+                .stroke(Color.blue, lineWidth: 2)
+            
+            Image(systemName: "lock.fill")
+                .font(Font.system(size: 24, weight: Font.Weight.light))
+                .foregroundColor(.blue)
         }
-        
-        /// Label style for encryption button when encryption is deactivated.
-        private var unencryptedButtonLabel: some View {
-            ZStack {
-                Circle()
-                    .fill(Color(UIColor.tertiaryLabel))
-                
-                Image(systemName: "lock.slash.fill")
-                    .font(Font.system(size: 24, weight: Font.Weight.light))
-                    .foregroundColor(.white)
-            }
-            .transition(AnyTransition.opacity.combined(with: .scale))
+        .transition(AnyTransition.opacity.combined(with: .scale))
+    }
+    
+    /// Label style for encryption button when encryption is deactivated.
+    private var unencryptedButtonLabel: some View {
+        ZStack {
+            Circle()
+                .fill(Color(UIColor.tertiaryLabel))
+            
+            Image(systemName: "lock.slash.fill")
+                .font(Font.system(size: 24, weight: Font.Weight.light))
+                .foregroundColor(.white)
         }
+        .transition(AnyTransition.opacity.combined(with: .scale))
     }
 }
diff --git a/enzevalos_iphone/SwiftUI/Compose/ComposeModel.swift b/enzevalos_iphone/SwiftUI/Compose/ComposeModel.swift
index 2de6237a..4818189d 100644
--- a/enzevalos_iphone/SwiftUI/Compose/ComposeModel.swift
+++ b/enzevalos_iphone/SwiftUI/Compose/ComposeModel.swift
@@ -32,6 +32,15 @@ class ComposeModel: ObservableObject {
         generateMail().send()
     }
     
+    /// Checks if encryption state can be toggled.
+    func toogleEncryption() {
+        if encryptionOn {
+            encryptionOn = false
+        } else {
+            recipientsModel.checkEncryption()
+        }
+    }
+    
     /// Adds email addresses to given RecipientFieldModel.
     ///
     /// - Parameters:
diff --git a/enzevalos_iphone/SwiftUI/Compose/RecipientsModel.swift b/enzevalos_iphone/SwiftUI/Compose/RecipientsModel.swift
index ea16a816..e98463da 100644
--- a/enzevalos_iphone/SwiftUI/Compose/RecipientsModel.swift
+++ b/enzevalos_iphone/SwiftUI/Compose/RecipientsModel.swift
@@ -78,6 +78,7 @@ class RecipientsModel: ObservableObject {
         ccModel.type = showBccField ? .cc : .ccBcc
     }
     
+    /// Turnes encryption on if all recipients have a key.
     func checkEncryption() {
         for addr in toModel.selectedContacts {
             if !addr.hasPublicKey {
-- 
GitLab