From 433999d781633db0cf79192ece524e3ae23f0df3 Mon Sep 17 00:00:00 2001
From: Bruno Muniz Azevedo Filho <bruno@elixxir.io>
Date: Thu, 21 Jul 2022 22:43:09 -0300
Subject: [PATCH] Fixed pending contacts searching cell

---
 Package.swift                                 |  1 +
 .../Controllers/SearchLeftController.swift    | 23 ++++++++++++-------
 .../Utils/SearchDiffableDataSource.swift      |  2 +-
 .../ViewModels/SearchLeftViewModel.swift      | 21 ++++++++++-------
 4 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/Package.swift b/Package.swift
index 765baf34..bcec957c 100644
--- a/Package.swift
+++ b/Package.swift
@@ -224,6 +224,7 @@ let package = Package(
                     "Shared",
                     "Keychain",
                     "InputField",
+                    "Presentation",
                     "DependencyInjection",
                     .product(
                         name: "Shout",
diff --git a/Sources/SearchFeature/Controllers/SearchLeftController.swift b/Sources/SearchFeature/Controllers/SearchLeftController.swift
index d305c614..bbfabd28 100644
--- a/Sources/SearchFeature/Controllers/SearchLeftController.swift
+++ b/Sources/SearchFeature/Controllers/SearchLeftController.swift
@@ -54,20 +54,27 @@ final class SearchLeftController: UIViewController {
             let contact: Contact
             let cell = tableView.dequeueReusableCell(forIndexPath: indexPath, ofType: AvatarCell.self)
 
+            let h1Text: String
+            var h2Text: String?
+
             switch item {
             case .stranger(let stranger):
                 contact = stranger
-            case .connection(let connection):
-                contact = connection
-            }
+                h1Text = stranger.username ?? ""
 
-            let h1Text: String
-            var h2Text: String?
+                if stranger.authStatus == .requested {
+                    h2Text = "Request pending"
+                } else if stranger.authStatus == .requestFailed {
+                    h2Text = "Request failed"
+                }
 
-            h1Text = (contact.nickname ?? contact.username) ?? ""
+            case .connection(let connection):
+                contact = connection
+                h1Text = (connection.nickname ?? contact.username) ?? ""
 
-            if let _ = contact.nickname, let username = contact.username {
-                h2Text = username
+                if connection.nickname != nil {
+                    h2Text = contact.username ?? ""
+                }
             }
 
             cell.setup(
diff --git a/Sources/SearchFeature/Utils/SearchDiffableDataSource.swift b/Sources/SearchFeature/Utils/SearchDiffableDataSource.swift
index 4fef0dfa..10d7bccc 100644
--- a/Sources/SearchFeature/Utils/SearchDiffableDataSource.swift
+++ b/Sources/SearchFeature/Utils/SearchDiffableDataSource.swift
@@ -17,7 +17,7 @@ class SearchDiffableDataSource: UITableViewDiffableDataSource<SearchSection, Sea
         case .stranger:
             return ""
         case .connections:
-            return "CONNECTIONS"
+            return "LOCAL RESULTS"
         }
     }
 }
diff --git a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
index b6d8a5f1..30f77b19 100644
--- a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
+++ b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
@@ -110,21 +110,26 @@ final class SearchLeftViewModel {
         }
     }
 
-    private func appendToLocalSearch(_ contact: Contact?) {
+    private func appendToLocalSearch(_ user: Contact?) {
         var snapshot = SearchSnapshot()
 
-        if let contact = contact {
-            snapshot.appendSections([.stranger])
-            snapshot.appendItems([.stranger(contact)], toSection: .stranger)
+        if var user = user {
+            if let contact = try? session.dbManager.fetchContacts(.init(id: [user.id])).first {
+                user.authStatus = contact.authStatus
+            }
+
+            if user.authStatus != .friend {
+                snapshot.appendSections([.stranger])
+                snapshot.appendItems([.stranger(user)], toSection: .stranger)
+            }
         }
 
-        let localsQuery = Contact.Query(text: stateSubject.value.input)
+        let localsQuery = Contact.Query(text: stateSubject.value.input, authStatus: [.friend])
 
         if let locals = try? session.dbManager.fetchContacts(localsQuery), locals.count > 0 {
-            // TODO: Remove myself
-            //
+            let localsWithoutMe = locals.filter { $0.id != session.myId }
             snapshot.appendSections([.connections])
-            snapshot.appendItems(locals.map(SearchItem.connection), toSection: .connections)
+            snapshot.appendItems(localsWithoutMe.map(SearchItem.connection), toSection: .connections)
         }
 
         stateSubject.value.snapshot = snapshot
-- 
GitLab