From b3854c9e0d95ba004747923425f8d5d9a11a989d Mon Sep 17 00:00:00 2001
From: Bruno Muniz Azevedo Filho <bruno@elixxir.io>
Date: Fri, 22 Jul 2022 15:55:18 -0300
Subject: [PATCH] Fixed comments on MR 50

---
 Sources/Integration/Client.swift              |   2 -
 Sources/Integration/Session/Session.swift     |   2 -
 Sources/ScanFeature/Views/ScanView.swift      |   4 +-
 .../ViewModels/SearchLeftViewModel.swift      |  14 ++-
 .../SearchFeature/Views/SearchRightView.swift | 118 ++++++++++++------
 Sources/Shared/AutoGenerated/Strings.swift    |  10 +-
 .../Resources/en.lproj/Localizable.strings    |   6 +-
 7 files changed, 105 insertions(+), 51 deletions(-)

diff --git a/Sources/Integration/Client.swift b/Sources/Integration/Client.swift
index 450c674e..8fc201af 100644
--- a/Sources/Integration/Client.swift
+++ b/Sources/Integration/Client.swift
@@ -93,8 +93,6 @@ public class Client {
             fatalError("Trying to add json parameters to backup but no backup manager created yet")
         }
 
-        print("^^^ Set params: \(string) to backup")
-
         backupManager.addJson(string)
     }
 
diff --git a/Sources/Integration/Session/Session.swift b/Sources/Integration/Session/Session.swift
index f85a6c6f..fee3c76c 100644
--- a/Sources/Integration/Session/Session.swift
+++ b/Sources/Integration/Session/Session.swift
@@ -140,8 +140,6 @@ public final class Session: SessionType {
             }
         }
 
-        print("^^^ \(report.parameters)")
-
         guard username!.isEmpty == false else {
             fatalError("Trying to restore an account that has no username")
         }
diff --git a/Sources/ScanFeature/Views/ScanView.swift b/Sources/ScanFeature/Views/ScanView.swift
index 7aff4cf2..540f68f5 100644
--- a/Sources/ScanFeature/Views/ScanView.swift
+++ b/Sources/ScanFeature/Views/ScanView.swift
@@ -80,12 +80,12 @@ final class ScanView: UIView {
                 actionButton.isHidden = false
 
             case .alreadyFriends(let name):
-                text = Localized.Scan.Error.friends(name)
+                text = Localized.Scan.Error.alreadyFriends(name)
                 actionButton.setTitle(Localized.Scan.contact, for: .normal)
                 actionButton.isHidden = false
 
             case .cameraPermission:
-                text = Localized.Scan.Error.denied
+                text = Localized.Scan.Error.cameraPermissionNeeded
                 actionButton.setTitle(Localized.Scan.settings, for: .normal)
                 actionButton.isHidden = false
 
diff --git a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
index 30f77b19..6c0a8390 100644
--- a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
+++ b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
@@ -126,12 +126,20 @@ final class SearchLeftViewModel {
 
         let localsQuery = Contact.Query(text: stateSubject.value.input, authStatus: [.friend])
 
-        if let locals = try? session.dbManager.fetchContacts(localsQuery), locals.count > 0 {
-            let localsWithoutMe = locals.filter { $0.id != session.myId }
+        if let locals = try? session.dbManager.fetchContacts(localsQuery),
+           let localsWithoutMe = removeMyself(from: locals),
+           localsWithoutMe.isEmpty == false {
             snapshot.appendSections([.connections])
-            snapshot.appendItems(localsWithoutMe.map(SearchItem.connection), toSection: .connections)
+            snapshot.appendItems(
+                localsWithoutMe.map(SearchItem.connection),
+                toSection: .connections
+            )
         }
 
         stateSubject.value.snapshot = snapshot
     }
+
+    private func removeMyself(from collection: [Contact]) -> [Contact]? {
+        collection.filter { $0.id != session.myId }
+    }
 }
diff --git a/Sources/SearchFeature/Views/SearchRightView.swift b/Sources/SearchFeature/Views/SearchRightView.swift
index 36380875..c2fd7476 100644
--- a/Sources/SearchFeature/Views/SearchRightView.swift
+++ b/Sources/SearchFeature/Views/SearchRightView.swift
@@ -39,51 +39,39 @@ final class SearchRightView: UIView {
     required init?(coder: NSCoder) { nil }
 
     func update(status: ScanningStatus) {
-        var text: String
+        setupTitle(for: status)
+        setupImageView(for: status)
+        setupActionButton(for: status)
+        setupCornerColors(for: status)
+        setupAnimationView(for: status)
+    }
 
-        switch status {
-        case .reading, .processing:
-            imageView.isHidden = true
-            actionButton.isHidden = true
-            text = Localized.Scan.Status.reading
-            overlayView.updateCornerColor(Asset.brandPrimary.color)
+    private func setupTitle(for status: ScanningStatus) {
+        let title: String
 
+        switch status {
         case .success:
-            animationView.isHidden = true
-            actionButton.isHidden = true
-            imageView.isHidden = false
-            imageView.image = Asset.sharedSuccess.image
-            text = Localized.Scan.Status.success
-            overlayView.updateCornerColor(Asset.accentSuccess.color)
-
-        case .failed(let error):
-            animationView.isHidden = true
-            imageView.image = Asset.scanError.image
-            imageView.isHidden = false
-            overlayView.updateCornerColor(Asset.accentDanger.color)
+            title = Localized.Scan.Status.success
+        case .reading:
+            title = Localized.Scan.Status.reading
+        case .processing:
+            title = Localized.Scan.Status.processing
+
+        case .failed(let scanningError):
+            switch scanningError {
+            case .unknown(let content):
+                title = content
 
-            switch error {
             case .requestOpened:
-                text = Localized.Scan.Error.requested
-                actionButton.setTitle(Localized.Scan.requests, for: .normal)
-                actionButton.isHidden = false
-
+                title = Localized.Scan.Error.requested
             case .alreadyFriends(let name):
-                text = Localized.Scan.Error.friends(name)
-                actionButton.setTitle(Localized.Scan.contact, for: .normal)
-                actionButton.isHidden = false
-
+                title = Localized.Scan.Error.alreadyFriends(name)
             case .cameraPermission:
-                text = Localized.Scan.Error.denied
-                actionButton.setTitle(Localized.Scan.settings, for: .normal)
-                actionButton.isHidden = false
-
-            case .unknown(let content):
-                text = content
+                title = Localized.Scan.Error.cameraPermissionNeeded
             }
         }
 
-        let attString = NSMutableAttributedString(string: text)
+        let attString = NSMutableAttributedString(string: title)
         let paragraph = NSMutableParagraphStyle()
         paragraph.alignment = .center
         paragraph.lineHeightMultiple = 1.35
@@ -92,13 +80,71 @@ final class SearchRightView: UIView {
         attString.addAttribute(.foregroundColor, value: Asset.neutralWhite.color)
         attString.addAttribute(.font, value: Fonts.Mulish.regular.font(size: 14.0) as Any)
 
-        if text.contains("#") {
+        if title.contains("#") {
             attString.addAttribute(name: .foregroundColor, value: Asset.brandPrimary.color, betweenCharacters: "#")
         }
 
         statusLabel.attributedText = attString
     }
 
+    private func setupImageView(for status: ScanningStatus) {
+        let image: UIImage?
+
+        switch status {
+        case .reading, .processing:
+            image = nil
+        case .success:
+            image = Asset.sharedSuccess.image
+        case .failed(_):
+            image = Asset.scanError.image
+        }
+
+        imageView.image = image
+        imageView.isHidden = image == nil
+    }
+
+    private func setupActionButton(for status: ScanningStatus) {
+        let buttonTitle: String?
+
+        switch status {
+        case .failed(.requestOpened):
+            buttonTitle = Localized.Scan.requests
+        case .failed(.alreadyFriends(_)):
+            buttonTitle = Localized.Scan.contact
+        case .failed(.cameraPermission):
+            buttonTitle = Localized.Scan.settings
+        case .reading, .processing, .success, .failed(.unknown(_)):
+            buttonTitle = nil
+        }
+
+        actionButton.setTitle(buttonTitle, for: .normal)
+        actionButton.isHidden = buttonTitle == nil
+    }
+
+    private func setupCornerColors(for status: ScanningStatus) {
+        let color: UIColor
+
+        switch status {
+        case .reading, .processing:
+            color = Asset.brandPrimary.color
+        case .success:
+            color = Asset.accentSuccess.color
+        case .failed(_):
+            color = Asset.accentDanger.color
+        }
+
+        overlayView.updateCornerColor(color)
+    }
+
+    private func setupAnimationView(for status: ScanningStatus) {
+        switch status {
+        case .reading, .processing:
+            animationView.isHidden = false
+        case .success, .failed(_):
+            animationView.isHidden = true
+        }
+    }
+
     private func setupConstraints() {
         overlayView.snp.makeConstraints {
             $0.top.equalToSuperview()
diff --git a/Sources/Shared/AutoGenerated/Strings.swift b/Sources/Shared/AutoGenerated/Strings.swift
index ed861800..315babf8 100644
--- a/Sources/Shared/AutoGenerated/Strings.swift
+++ b/Sources/Shared/AutoGenerated/Strings.swift
@@ -1010,13 +1010,13 @@ public enum Localized {
       }
     }
     public enum Error {
-      /// Camera needs permission to be used
-      public static let denied = Localized.tr("Localizable", "scan.error.denied")
       /// You've already added 
       /// #%@#
-      public static func friends(_ p1: Any) -> String {
-        return Localized.tr("Localizable", "scan.error.friends", String(describing: p1))
+      public static func alreadyFriends(_ p1: Any) -> String {
+        return Localized.tr("Localizable", "scan.error.alreadyFriends", String(describing: p1))
       }
+      /// Camera needs permission to be used
+      public static let cameraPermissionNeeded = Localized.tr("Localizable", "scan.error.cameraPermissionNeeded")
       /// Something’s gone wrong. Please try again.
       public static let general = Localized.tr("Localizable", "scan.error.general")
       /// Invalid QR code
@@ -1039,6 +1039,8 @@ public enum Localized {
       public static let `right` = Localized.tr("Localizable", "scan.segmentedControl.right")
     }
     public enum Status {
+      /// Processing...
+      public static let processing = Localized.tr("Localizable", "scan.status.processing")
       /// Place QR code inside frame to scan
       public static let reading = Localized.tr("Localizable", "scan.status.reading")
       /// Success
diff --git a/Sources/Shared/Resources/en.lproj/Localizable.strings b/Sources/Shared/Resources/en.lproj/Localizable.strings
index dd95d400..2b726391 100644
--- a/Sources/Shared/Resources/en.lproj/Localizable.strings
+++ b/Sources/Shared/Resources/en.lproj/Localizable.strings
@@ -172,6 +172,8 @@
 
 "scan.status.reading"
 = "Place QR code inside frame to scan";
+"scan.status.processing"
+= "Processing...";
 "scan.status.success"
 = "Success";
 "scan.display.copied"
@@ -197,7 +199,7 @@
 = "Scan Code";
 "scan.segmentedControl.right"
 = "My Code";
-"scan.error.denied"
+"scan.error.cameraPermissionNeeded"
 = "Camera needs permission to be used";
 "scan.error.invalid"
 = "Invalid QR code";
@@ -205,7 +207,7 @@
 = "Something’s gone wrong. Please try again.";
 "scan.error.requested"
 = "You already have a request open with this contact.";
-"scan.error.friends"
+"scan.error.alreadyFriends"
 = "You've already added \n#%@#";
 "scan.error.pending"
 = "This user is already pending in your contact list";
-- 
GitLab