From 6b7ccd2d77b50af4d38699bbcb9b8676e7dd5424 Mon Sep 17 00:00:00 2001
From: Bruno Muniz Azevedo Filho <bruno@elixxir.io>
Date: Mon, 1 Aug 2022 17:26:27 -0300
Subject: [PATCH] Fixed comments on MR

---
 Sources/App/AppDelegate.swift                 |  4 +-
 Sources/App/DependencyRegistrator.swift       | 43 ----------------
 Sources/App/PushRouter.swift                  | 51 +++++++++++++++++++
 .../ViewModels/SearchLeftViewModel.swift      |  2 +
 Tests/AppTests/General/InvitationTests.swift  | 22 ++++----
 5 files changed, 66 insertions(+), 56 deletions(-)
 create mode 100644 Sources/App/PushRouter.swift

diff --git a/Sources/App/AppDelegate.swift b/Sources/App/AppDelegate.swift
index e3dadaee..00b8875e 100644
--- a/Sources/App/AppDelegate.swift
+++ b/Sources/App/AppDelegate.swift
@@ -143,8 +143,8 @@ public class AppDelegate: UIResponder, UIApplicationDelegate {
         open url: URL,
         options: [UIApplication.OpenURLOptionsKey : Any] = [:]
     ) -> Bool {
-        if let username = getUsernameFromInvitationDeepLink(url),
-           let router = try? DependencyInjection.Container.shared.resolve() as PushRouter {
+        if let username = getUsernameFromInvitationDeepLink(url) {
+            let router = try! DependencyInjection.Container.shared.resolve() as PushRouter
             invitation = username
             router.navigateTo(.search, {})
 
diff --git a/Sources/App/DependencyRegistrator.swift b/Sources/App/DependencyRegistrator.swift
index 4466aea4..05d03f78 100644
--- a/Sources/App/DependencyRegistrator.swift
+++ b/Sources/App/DependencyRegistrator.swift
@@ -251,46 +251,3 @@ struct DependencyRegistrator {
             ) as ChatListCoordinating)
     }
 }
-
-extension PushRouter {
-    static func live(navigationController: UINavigationController) -> PushRouter {
-        PushRouter { route, completion in
-            if let launchController = navigationController.viewControllers.last as? LaunchController {
-                launchController.pendingPushRoute = route
-            } else {
-                switch route {
-                case .search:
-                    if (navigationController.viewControllers.last as? SearchContainerController) == nil {
-                        navigationController.setViewControllers([
-                            ChatListController(),
-                            SearchContainerController()
-                        ], animated: true)
-                    }
-
-                case .requests:
-                    if (navigationController.viewControllers.last as? RequestsContainerController) == nil {
-                        navigationController.setViewControllers([RequestsContainerController()], animated: true)
-                    }
-                case .contactChat(id: let id):
-                    if let session = try? DependencyInjection.Container.shared.resolve() as SessionType,
-                       let contact = try? session.dbManager.fetchContacts(.init(id: [id])).first {
-                        navigationController.setViewControllers([
-                            ChatListController(),
-                            SingleChatController(contact)
-                        ], animated: true)
-                    }
-                case .groupChat(id: let id):
-                    if let session = try? DependencyInjection.Container.shared.resolve() as SessionType,
-                       let info = try? session.dbManager.fetchGroupInfos(.init(groupId: id)).first {
-                        navigationController.setViewControllers([
-                            ChatListController(),
-                            GroupChatController(info)
-                        ], animated: true)
-                    }
-                }
-            }
-
-            completion()
-        }
-    }
-}
diff --git a/Sources/App/PushRouter.swift b/Sources/App/PushRouter.swift
new file mode 100644
index 00000000..d81f841f
--- /dev/null
+++ b/Sources/App/PushRouter.swift
@@ -0,0 +1,51 @@
+import UIKit
+import PushFeature
+import Integration
+import ChatFeature
+import SearchFeature
+import LaunchFeature
+import ChatListFeature
+import RequestsFeature
+import DependencyInjection
+
+extension PushRouter {
+    static func live(navigationController: UINavigationController) -> PushRouter {
+        PushRouter { route, completion in
+            if let launchController = navigationController.viewControllers.last as? LaunchController {
+                launchController.pendingPushRoute = route
+            } else {
+                switch route {
+                case .search:
+                    if !(navigationController.viewControllers.last is SearchContainerController) {
+                        navigationController.setViewControllers([
+                            ChatListController(),
+                            SearchContainerController()
+                        ], animated: true)
+                    }
+                case .requests:
+                    if !(navigationController.viewControllers.last is RequestsContainerController) {
+                        navigationController.setViewControllers([RequestsContainerController()], animated: true)
+                    }
+                case .contactChat(id: let id):
+                    if let session = try? DependencyInjection.Container.shared.resolve() as SessionType,
+                       let contact = try? session.dbManager.fetchContacts(.init(id: [id])).first {
+                        navigationController.setViewControllers([
+                            ChatListController(),
+                            SingleChatController(contact)
+                        ], animated: true)
+                    }
+                case .groupChat(id: let id):
+                    if let session = try? DependencyInjection.Container.shared.resolve() as SessionType,
+                       let info = try? session.dbManager.fetchGroupInfos(.init(groupId: id)).first {
+                        navigationController.setViewControllers([
+                            ChatListController(),
+                            GroupChatController(info)
+                        ], animated: true)
+                    }
+                }
+            }
+
+            completion()
+        }
+    }
+}
diff --git a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
index cab4842c..8206e136 100644
--- a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
+++ b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
@@ -48,6 +48,8 @@ final class SearchLeftViewModel {
             stateSubject.value.input = pendingInvitation
             hudSubject.send(.onAction(Localized.Ud.Search.cancel))
 
+            networkCancellable.removeAll()
+
             networkMonitor.statusPublisher
                 .first { $0 == .available }
                 .eraseToAnyPublisher()
diff --git a/Tests/AppTests/General/InvitationTests.swift b/Tests/AppTests/General/InvitationTests.swift
index bbd80d6e..146c885a 100644
--- a/Tests/AppTests/General/InvitationTests.swift
+++ b/Tests/AppTests/General/InvitationTests.swift
@@ -4,18 +4,18 @@ import XCTest
 
 final class AppDelegateTests: XCTestCase {
     func test_invitationDeeplink() {
-        let host = "messenger"
-        let query = "invitation"
-        let scheme = "xxnetwork"
-        let username = "john_doe"
+        XCTAssertNil(
+            getUsernameFromInvitationDeepLink(URL(string: "http://messenger?invitation=john_doe")!)
+        )
 
-        let url = URL(string: "\(scheme)://\(host)?\(query)=\(username)")!
-        XCTAssertEqual(getUsernameFromInvitationDeepLink(url), username)
+        XCTAssertNotEqual(
+            getUsernameFromInvitationDeepLink(URL(string: "xxnetwork://messenger?invitation=the_rock")!),
+            "john_doe"
+        )
 
-        let malformedURL = URL(string: "\(scheme)\(host)\(query)\(username)")!
-        XCTAssertNil(getUsernameFromInvitationDeepLink(malformedURL))
-
-        let urlAnotherUsername = URL(string: "\(scheme)://\(host)?\(query)=asdfg")!
-        XCTAssertNotEqual(getUsernameFromInvitationDeepLink(urlAnotherUsername), username)
+        XCTAssertEqual(
+            getUsernameFromInvitationDeepLink(URL(string: "xxnetwork://messenger?invitation=john_doe")!),
+            "john_doe"
+        )
     }
 }
-- 
GitLab