From b26c12d6a80c786df23dc299adee2741daf0bda8 Mon Sep 17 00:00:00 2001
From: Bruno Muniz Azevedo Filho <bruno@elixxir.io>
Date: Sat, 13 Aug 2022 01:19:53 -0300
Subject: [PATCH] Fix comments on MR

---
 .../Controllers/SingleChatController.swift    | 44 +++++++++++++------
 .../ViewModels/GroupChatViewModel.swift       |  2 +-
 .../ViewModels/SingleChatViewModel.swift      |  4 +-
 .../ViewModel/ChatListViewModel.swift         | 10 +++--
 .../ViewModels/SearchLeftViewModel.swift      | 11 +++--
 .../TermsConditionsController.swift           |  2 +-
 6 files changed, 49 insertions(+), 24 deletions(-)

diff --git a/Sources/ChatFeature/Controllers/SingleChatController.swift b/Sources/ChatFeature/Controllers/SingleChatController.swift
index 81fa1c7c..ebc78095 100644
--- a/Sources/ChatFeature/Controllers/SingleChatController.swift
+++ b/Sources/ChatFeature/Controllers/SingleChatController.swift
@@ -430,7 +430,8 @@ public final class SingleChatController: UIViewController {
                 drawer.dismiss(animated: true) { [weak self] in
                     guard let self = self else { return }
                     self.drawerCancellables.removeAll()
-                    self.didProceedWithReport()
+                    self.viewModel.proceeedWithReport(screenshot: self.takeAppScreenshot())
+                    self.navigationController?.popViewController(animated: true)
                 }
             }.store(in: &drawerCancellables)
 
@@ -445,21 +446,38 @@ public final class SingleChatController: UIViewController {
         coordinator.toDrawer(drawer, from: self)
     }
 
-    private func didProceedWithReport() {
-        var screenshotImage: UIImage?
+    func takeAppScreenshot() -> UIImage {
+        let foregroundWindowScene: UIWindowScene? = UIApplication.shared.connectedScenes
+            .filter { $0.activationState == .foregroundActive }
+            .compactMap { $0 as? UIWindowScene }
+            .first
 
-        let layer = UIApplication.shared.keyWindow!.layer
+        guard let foregroundWindowScene = foregroundWindowScene else {
+            fatalError("[takeAppScreenshot]: Unable to get foreground window scene")
+        }
+
+        let keyWindow: UIWindow?
+
+        if #available(iOS 15.0, *) {
+            keyWindow = foregroundWindowScene.keyWindow
+        } else {
+            keyWindow = UIApplication.shared.keyWindow
+        }
 
-        let scale = UIScreen.main.scale
-        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
-        guard let context = UIGraphicsGetCurrentContext() else { return }
-        layer.render(in: context)
+        guard let keyWindow = keyWindow else {
+            fatalError("[takeAppScreenshot]: Unable to get key window")
+        }
+
+        let rendererFormat = UIGraphicsImageRendererFormat()
+        rendererFormat.scale = foregroundWindowScene.screen.scale
+
+        let renderer = UIGraphicsImageRenderer(
+            bounds: keyWindow.bounds,
+            format: rendererFormat
+        )
 
-        if let image = UIGraphicsGetImageFromCurrentImageContext() {
-            UIGraphicsEndImageContext()
-            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
-            viewModel.uploadReport(screenshot: image)
-            navigationController?.popViewController(animated: true)
+        return renderer.image { ctx in
+            keyWindow.layer.render(in: ctx.cgContext)
         }
     }
 
diff --git a/Sources/ChatFeature/ViewModels/GroupChatViewModel.swift b/Sources/ChatFeature/ViewModels/GroupChatViewModel.swift
index fc7a30d4..ece776ae 100644
--- a/Sources/ChatFeature/ViewModels/GroupChatViewModel.swift
+++ b/Sources/ChatFeature/ViewModels/GroupChatViewModel.swift
@@ -30,7 +30,7 @@ final class GroupChatViewModel {
     private let routesSubject = PassthroughSubject<GroupChatNavigationRoutes, Never>()
 
     var messages: AnyPublisher<[ArraySection<ChatSection, Message>], Never> {
-        session.dbManager.fetchMessagesPublisher(.init(chat: .group(info.group.id)))
+        session.dbManager.fetchMessagesPublisher(.init(chat: .group(info.group.id), isSenderBanned: false))
             .assertNoFailure()
             .map { messages -> [ArraySection<ChatSection, Message>] in
                 let groupedByDate = Dictionary(grouping: messages) { domainModel -> Date in
diff --git a/Sources/ChatFeature/ViewModels/SingleChatViewModel.swift b/Sources/ChatFeature/ViewModels/SingleChatViewModel.swift
index bd413702..d4aa517b 100644
--- a/Sources/ChatFeature/ViewModels/SingleChatViewModel.swift
+++ b/Sources/ChatFeature/ViewModels/SingleChatViewModel.swift
@@ -222,9 +222,7 @@ final class SingleChatViewModel {
         return (contactTitle, message.text)
     }
 
-    func uploadReport(screenshot: UIImage) {
-        UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
-
+    func proceeedWithReport(screenshot: UIImage) {
         var contact = contact
         contact.isBlocked = true
         _ = try? session.dbManager.saveContact(contact)
diff --git a/Sources/ChatListFeature/ViewModel/ChatListViewModel.swift b/Sources/ChatListFeature/ViewModel/ChatListViewModel.swift
index 1b094ffa..0e9b4c5e 100644
--- a/Sources/ChatListFeature/ViewModel/ChatListViewModel.swift
+++ b/Sources/ChatListFeature/ViewModel/ChatListViewModel.swift
@@ -50,10 +50,13 @@ final class ChatListViewModel {
     }
 
     var searchPublisher: AnyPublisher<SearchSnapshot, Never> {
-        let contactsQuery = Contact.Query(isBlocked: false, isBanned: false)
+        let contactsStream = session.dbManager
+            .fetchContactsPublisher(.init(isBlocked: false, isBanned: false))
+            .assertNoFailure()
+            .map { $0.filter { $0.id != self.session.myId }}
 
         return Publishers.CombineLatest3(
-            session.dbManager.fetchContactsPublisher(contactsQuery).assertNoFailure(),
+            contactsStream,
             chatsPublisher,
             searchSubject
                 .removeDuplicates()
@@ -137,7 +140,8 @@ final class ChatListViewModel {
                 groupChatInfoQuery: GroupChatInfo.Query(
                     authStatus: [.participating],
                     isLeaderBlocked: false,
-                    isLeaderBanned: false
+                    isLeaderBanned: false,
+                    excludeBannedContactsMessages: true
                 ),
                 groupQuery: Group.Query(
                     withMessages: false,
diff --git a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
index 7d49d19e..4f7aa6ef 100644
--- a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
+++ b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
@@ -144,17 +144,22 @@ final class SearchLeftViewModel {
         var snapshot = SearchSnapshot()
 
         if var user = user {
-            if let contact = try? session.dbManager.fetchContacts(.init(id: [user.id], isBlocked: false, isBanned: false)).first {
+            if let contact = try? session.dbManager.fetchContacts(.init(id: [user.id])).first {
                 user.authStatus = contact.authStatus
             }
 
-            if user.authStatus != .friend {
+            if user.authStatus != .friend, !user.isBanned, !user.isBlocked {
                 snapshot.appendSections([.stranger])
                 snapshot.appendItems([.stranger(user)], toSection: .stranger)
             }
         }
 
-        let localsQuery = Contact.Query(text: stateSubject.value.input, authStatus: [.friend])
+        let localsQuery = Contact.Query(
+            text: stateSubject.value.input,
+            authStatus: [.friend],
+            isBlocked: false,
+            isBanned: false
+        )
 
         if let locals = try? session.dbManager.fetchContacts(localsQuery),
            let localsWithoutMe = removeMyself(from: locals),
diff --git a/Sources/TermsFeature/TermsConditionsController.swift b/Sources/TermsFeature/TermsConditionsController.swift
index 7f7893e3..b06152ee 100644
--- a/Sources/TermsFeature/TermsConditionsController.swift
+++ b/Sources/TermsFeature/TermsConditionsController.swift
@@ -66,7 +66,7 @@ public final class TermsConditionsController: UIViewController {
                 let webController = UIViewController()
                 webController.view.addSubview(webView)
                 webView.snp.makeConstraints { $0.edges.equalToSuperview() }
-                webView.load(URLRequest(url: URL(string: "https://xx.network")!))
+                webView.load(URLRequest(url: URL(string: "https://elixxir.io/eula")!))
                 present(webController, animated: true)
             }.store(in: &cancellables)
     }
-- 
GitLab