From 11b5a8deefbd319ef25c21e8602357cab7bef48d Mon Sep 17 00:00:00 2001
From: Bruno Muniz Azevedo Filho <bruno@elixxir.io>
Date: Wed, 20 Jul 2022 20:51:45 -0300
Subject: [PATCH] Fixed toast localization for sent request

---
 Sources/HUD/HUD.swift                         |  1 +
 .../Session/Session+Contacts.swift            |  7 ++
 .../ViewModels/SearchLeftViewModel.swift      |  6 +-
 Sources/Shared/AutoGenerated/Strings.swift    |  4 +
 .../Resources/en.lproj/Localizable.strings    |  2 +
 Sources/Shared/Views/AvatarCell.swift         | 87 +++++++++++++++++++
 6 files changed, 106 insertions(+), 1 deletion(-)

diff --git a/Sources/HUD/HUD.swift b/Sources/HUD/HUD.swift
index 98099092..207f3473 100644
--- a/Sources/HUD/HUD.swift
+++ b/Sources/HUD/HUD.swift
@@ -66,6 +66,7 @@ public final class HUD {
                 self.errorView = nil
                 self.animation = nil
                 self.window = nil
+                self.actionButton = nil
                 self.titleLabel = nil
 
                 switch status {
diff --git a/Sources/Integration/Session/Session+Contacts.swift b/Sources/Integration/Session/Session+Contacts.swift
index 3f6a2771..2b507bdb 100644
--- a/Sources/Integration/Session/Session+Contacts.swift
+++ b/Sources/Integration/Session/Session+Contacts.swift
@@ -170,6 +170,13 @@ extension Session {
                     contact.authStatus = success ? .requested : .requestFailed
                     contact = try self.dbManager.saveContact(contact)
 
+                    let name = contact.nickname ?? contact.username
+
+                    self.toastController.enqueueToast(model: .init(
+                        title: Localized.Requests.Sent.Toast.sent(name ?? ""),
+                        leftImage: Asset.sharedSuccess.image
+                    ))
+
                 case .failure:
                     contact.createdAt = Date()
                     contact.authStatus = .requestFailed
diff --git a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
index 92336ef7..76be7aa9 100644
--- a/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
+++ b/Sources/SearchFeature/ViewModels/SearchLeftViewModel.swift
@@ -107,7 +107,11 @@ final class SearchLeftViewModel {
             snapshot.appendItems([.stranger(contact)], toSection: .stranger)
         }
 
-        if let locals = try? session.dbManager.fetchContacts(Contact.Query(username: stateSubject.value.input)), locals.count > 0 {
+        let localsQuery = Contact.Query(text: stateSubject.value.input)
+
+        if let locals = try? session.dbManager.fetchContacts(localsQuery), locals.count > 0 {
+            // TODO: Remove myself
+            //
             snapshot.appendSections([.connections])
             snapshot.appendItems(locals.map(SearchItem.connection), toSection: .connections)
         }
diff --git a/Sources/Shared/AutoGenerated/Strings.swift b/Sources/Shared/AutoGenerated/Strings.swift
index 4cb0e5a1..fdc5717f 100644
--- a/Sources/Shared/AutoGenerated/Strings.swift
+++ b/Sources/Shared/AutoGenerated/Strings.swift
@@ -965,6 +965,10 @@ public enum Localized {
         public static func resent(_ p1: Any) -> String {
           return Localized.tr("Localizable", "requests.sent.toast.resent", String(describing: p1))
         }
+        /// Request successfully sent to %@
+        public static func sent(_ p1: Any) -> String {
+          return Localized.tr("Localizable", "requests.sent.toast.sent", String(describing: p1))
+        }
       }
     }
   }
diff --git a/Sources/Shared/Resources/en.lproj/Localizable.strings b/Sources/Shared/Resources/en.lproj/Localizable.strings
index 5a0f7241..df7ee492 100644
--- a/Sources/Shared/Resources/en.lproj/Localizable.strings
+++ b/Sources/Shared/Resources/en.lproj/Localizable.strings
@@ -353,6 +353,8 @@
 = "Search for connections";
 "requests.sent.empty"
 = "You haven't sent any requests";
+"requests.sent.toast.sent"
+= "Request successfully sent to %@";
 "requests.sent.toast.resent"
 = "Request successfully resent to %@";
 
diff --git a/Sources/Shared/Views/AvatarCell.swift b/Sources/Shared/Views/AvatarCell.swift
index c76721a4..d16e93b4 100644
--- a/Sources/Shared/Views/AvatarCell.swift
+++ b/Sources/Shared/Views/AvatarCell.swift
@@ -1,4 +1,38 @@
 import UIKit
+import Combine
+
+final class AvatarCellButton: UIControl {
+    let titleLabel = UILabel()
+    let imageView = UIImageView()
+
+    init() {
+        super.init(frame: .zero)
+        titleLabel.numberOfLines = 0
+        titleLabel.textAlignment = .right
+        titleLabel.font = Fonts.Mulish.semiBold.font(size: 13.0)
+
+        addSubview(imageView)
+        addSubview(titleLabel)
+
+        imageView.snp.makeConstraints {
+            $0.top.greaterThanOrEqualToSuperview()
+            $0.left.equalToSuperview()
+            $0.centerY.equalToSuperview()
+            $0.bottom.lessThanOrEqualToSuperview()
+        }
+
+        titleLabel.snp.makeConstraints {
+            $0.top.greaterThanOrEqualToSuperview()
+            $0.left.equalTo(imageView.snp.right).offset(5)
+            $0.centerY.equalToSuperview()
+            $0.right.equalToSuperview()
+            $0.width.equalTo(60)
+            $0.bottom.lessThanOrEqualToSuperview()
+        }
+    }
+
+    required init?(coder: NSCoder) { nil }
+}
 
 public final class AvatarCell: UITableViewCell {
     let h1Label = UILabel()
@@ -8,6 +42,10 @@ public final class AvatarCell: UITableViewCell {
     let separatorView = UIView()
     let avatarView = AvatarView()
     let stackView = UIStackView()
+    let stateButton = AvatarCellButton()
+
+    var cancellables = Set<AnyCancellable>()
+    public var didTapStateButton: (() -> Void)!
 
     public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
         super.init(style: style, reuseIdentifier: reuseIdentifier)
@@ -38,6 +76,7 @@ public final class AvatarCell: UITableViewCell {
 
         contentView.addSubview(stackView)
         contentView.addSubview(avatarView)
+        contentView.addSubview(stateButton)
         contentView.addSubview(separatorView)
 
         setupConstraints()
@@ -52,7 +91,11 @@ public final class AvatarCell: UITableViewCell {
         h3Label.text = nil
         h4Label.text = nil
 
+        stateButton.imageView.image = nil
+        stateButton.titleLabel.text = nil
+
         avatarView.prepareForReuse()
+        cancellables.removeAll()
     }
 
     public func setup(
@@ -90,6 +133,45 @@ public final class AvatarCell: UITableViewCell {
         separatorView.alpha = showSeparator ? 1.0 : 0.0
     }
 
+    public func setupForRequestSent(resent: Bool) {
+        cancellables.removeAll()
+
+        var buttonTitle: String? = nil
+        var buttonImage: UIImage? = nil
+        var buttonTitleColor: UIColor? = nil
+
+        if resent {
+            buttonTitle = Localized.Requests.Cell.resent
+            buttonImage = Asset.requestsResent.image
+            buttonTitleColor = Asset.neutralWeak.color
+        } else {
+            buttonTitle = Localized.Requests.Cell.requested
+            buttonImage = Asset.requestsResend.image
+            buttonTitleColor = Asset.brandPrimary.color
+        }
+
+        setupStateButton(
+            image: buttonImage,
+            title: buttonTitle,
+            color: buttonTitleColor
+        )
+    }
+
+    private func setupStateButton(
+        image: UIImage?,
+        title: String?,
+        color: UIColor?
+    ) {
+        stateButton.imageView.image = image
+        stateButton.titleLabel.text = title
+        stateButton.titleLabel.textColor = color
+
+        stateButton
+            .publisher(for: .touchUpInside)
+            .sink { [unowned self] in didTapStateButton() }
+            .store(in: &cancellables)
+    }
+
     private func setupConstraints() {
         avatarView.snp.makeConstraints {
             $0.width.height.equalTo(36)
@@ -112,5 +194,10 @@ public final class AvatarCell: UITableViewCell {
             $0.right.equalToSuperview()
             $0.bottom.equalToSuperview()
         }
+
+        stateButton.snp.makeConstraints {
+            $0.centerY.equalTo(stackView)
+            $0.right.equalToSuperview().offset(-24)
+        }
     }
 }
-- 
GitLab