From bff1322bff4a8699a06f0acc5428c13c35cdbe80 Mon Sep 17 00:00:00 2001
From: Bruno Muniz Azevedo Filho <bruno@elixxir.io>
Date: Thu, 8 Dec 2022 03:15:27 -0300
Subject: [PATCH] Fix resend code missing implementation for onboarding and
 profile

---
 .../Resources/en.lproj/Localizable.strings       |  4 ++--
 Sources/AppResources/Strings.swift               |  4 ++--
 .../Controllers/OnboardingCodeController.swift   |  5 ++++-
 .../ViewModels/OnboardingCodeViewModel.swift     | 16 ++++++++++++++--
 .../ViewModels/OnboardingEmailViewModel.swift    |  7 +++++--
 .../ViewModels/OnboardingPhoneViewModel.swift    |  7 +++++--
 .../ViewModels/ProfileCodeViewModel.swift        | 16 ++++++++++++++--
 .../ViewModels/ProfileEmailViewModel.swift       |  7 +++++--
 .../ViewModels/ProfilePhoneViewModel.swift       |  8 +++++---
 .../ViewModels/ProfileViewModel.swift            |  2 +-
 10 files changed, 57 insertions(+), 19 deletions(-)

diff --git a/Sources/AppResources/Resources/en.lproj/Localizable.strings b/Sources/AppResources/Resources/en.lproj/Localizable.strings
index 4bf92d9d..6263b324 100644
--- a/Sources/AppResources/Resources/en.lproj/Localizable.strings
+++ b/Sources/AppResources/Resources/en.lproj/Localizable.strings
@@ -812,7 +812,7 @@
 "onboarding.emailConfirmation.title"
 = "Please enter the #code# sent to your email.";
 "onboarding.emailConfirmation.subtitle"
-= "We sent a verification code to %@.";
+= "We sent a verification code to %@";
 "onboarding.emailConfirmation.input"
 = "Code";
 "onboarding.emailConfirmation.next"
@@ -846,7 +846,7 @@
 "onboarding.phoneConfirmation.title"
 = "Please enter the #code# sent to your phone through #SMS#.";
 "onboarding.phoneConfirmation.subtitle"
-= "We sent a verification code to %@.";
+= "We sent a verification code to %@";
 "onboarding.phoneConfirmation.input"
 = "Code";
 "onboarding.phoneConfirmation.next"
diff --git a/Sources/AppResources/Strings.swift b/Sources/AppResources/Strings.swift
index 09255828..a2b35b00 100644
--- a/Sources/AppResources/Strings.swift
+++ b/Sources/AppResources/Strings.swift
@@ -731,7 +731,7 @@ public enum Localized {
       public static func resend(_ p1: Any) -> String {
         return Localized.tr("Localizable", "onboarding.emailConfirmation.resend", String(describing: p1))
       }
-      /// We sent a verification code to %@.
+      /// We sent a verification code to %@
       public static func subtitle(_ p1: Any) -> String {
         return Localized.tr("Localizable", "onboarding.emailConfirmation.subtitle", String(describing: p1))
       }
@@ -771,7 +771,7 @@ public enum Localized {
       public static func resend(_ p1: Any) -> String {
         return Localized.tr("Localizable", "onboarding.phoneConfirmation.resend", String(describing: p1))
       }
-      /// We sent a verification code to %@.
+      /// We sent a verification code to %@
       public static func subtitle(_ p1: Any) -> String {
         return Localized.tr("Localizable", "onboarding.phoneConfirmation.subtitle", String(describing: p1))
       }
diff --git a/Sources/OnboardingFeature/Controllers/OnboardingCodeController.swift b/Sources/OnboardingFeature/Controllers/OnboardingCodeController.swift
index 8fd333cb..4075ad2c 100644
--- a/Sources/OnboardingFeature/Controllers/OnboardingCodeController.swift
+++ b/Sources/OnboardingFeature/Controllers/OnboardingCodeController.swift
@@ -6,6 +6,7 @@ import AppResources
 import Dependencies
 import AppNavigation
 import DrawerFeature
+import CountryListFeature
 import ScrollViewController
 
 public final class OnboardingCodeController: UIViewController {
@@ -53,7 +54,9 @@ public final class OnboardingCodeController: UIViewController {
     screenView.setupSubtitle(
       isEmail ?
       Localized.Onboarding.EmailConfirmation.subtitle(content) :
-      Localized.Onboarding.PhoneConfirmation.subtitle(content)
+      Localized.Onboarding.PhoneConfirmation.subtitle(
+        "\(Country.findFrom(content).prefix)\(content.dropLast(2))"
+      )
     )
 
     screenView.didTapInfo = { [weak self] in
diff --git a/Sources/OnboardingFeature/ViewModels/OnboardingCodeViewModel.swift b/Sources/OnboardingFeature/ViewModels/OnboardingCodeViewModel.swift
index 570edad9..66f497f7 100644
--- a/Sources/OnboardingFeature/ViewModels/OnboardingCodeViewModel.swift
+++ b/Sources/OnboardingFeature/ViewModels/OnboardingCodeViewModel.swift
@@ -31,7 +31,7 @@ final class OnboardingCodeViewModel {
   private var timer: Timer?
   private let isEmail: Bool
   private let content: String
-  private let confirmationId: String
+  private var confirmationId: String
   private let stateSubject = CurrentValueSubject<ViewState, Never>(.init())
 
   init(
@@ -60,6 +60,18 @@ final class OnboardingCodeViewModel {
       }
       self.stateSubject.value.resendDebouncer -= 1
     }
+    bgQueue.schedule { [weak self] in
+      guard let self else { return }
+      do {
+        self.confirmationId = try self.messenger.ud.tryGet()
+          .sendRegisterFact(.init(
+            type: self.isEmail ? .email : .phone,
+            value: self.content
+          ))
+      } catch {
+        self.hudManager.show(.init(error: error))
+      }
+    }
   }
 
   func didTapNext() {
@@ -67,7 +79,7 @@ final class OnboardingCodeViewModel {
     bgQueue.schedule { [weak self] in
       guard let self else { return }
       do {
-        try self.messenger.ud.get()!.confirmFact(
+        try self.messenger.ud.tryGet().confirmFact(
           confirmationId: self.confirmationId,
           code: self.stateSubject.value.input
         )
diff --git a/Sources/OnboardingFeature/ViewModels/OnboardingEmailViewModel.swift b/Sources/OnboardingFeature/ViewModels/OnboardingEmailViewModel.swift
index 0af88c77..bcfdbdaa 100644
--- a/Sources/OnboardingFeature/ViewModels/OnboardingEmailViewModel.swift
+++ b/Sources/OnboardingFeature/ViewModels/OnboardingEmailViewModel.swift
@@ -39,8 +39,11 @@ final class OnboardingEmailViewModel {
     bgQueue.schedule { [weak self] in
       guard let self else { return }
       do {
-        let confirmationId = try self.messenger.ud.get()!.sendRegisterFact(
-          .init(type: .email, value: self.stateSubject.value.input)
+        let confirmationId = try self.messenger.ud.tryGet()
+          .sendRegisterFact(.init(
+            type: .email,
+            value: self.stateSubject.value.input
+          )
         )
         self.hudManager.hide()
         self.stateSubject.value.confirmationId = confirmationId
diff --git a/Sources/OnboardingFeature/ViewModels/OnboardingPhoneViewModel.swift b/Sources/OnboardingFeature/ViewModels/OnboardingPhoneViewModel.swift
index 5fa427fb..2787b483 100644
--- a/Sources/OnboardingFeature/ViewModels/OnboardingPhoneViewModel.swift
+++ b/Sources/OnboardingFeature/ViewModels/OnboardingPhoneViewModel.swift
@@ -48,8 +48,11 @@ final class OnboardingPhoneViewModel {
       guard let self else { return }
       let content = "\(self.stateSubject.value.input)\(self.stateSubject.value.country.code)"
       do {
-        let confirmationId = try self.messenger.ud.get()!.sendRegisterFact(
-          .init(type: .phone, value: content)
+        let confirmationId = try self.messenger.ud.tryGet()
+          .sendRegisterFact(.init(
+            type: .phone,
+            value: content
+          )
         )
         self.hudManager.hide()
         self.stateSubject.value.content = content
diff --git a/Sources/ProfileFeature/ViewModels/ProfileCodeViewModel.swift b/Sources/ProfileFeature/ViewModels/ProfileCodeViewModel.swift
index 804dfa78..3e467012 100644
--- a/Sources/ProfileFeature/ViewModels/ProfileCodeViewModel.swift
+++ b/Sources/ProfileFeature/ViewModels/ProfileCodeViewModel.swift
@@ -30,7 +30,7 @@ final class ProfileCodeViewModel {
   private var timer: Timer?
   private let isEmail: Bool
   private let content: String
-  private let confirmationId: String
+  private var confirmationId: String
   private let stateSubject = CurrentValueSubject<ViewState, Never>(.init())
 
   init(
@@ -59,6 +59,18 @@ final class ProfileCodeViewModel {
       }
       self.stateSubject.value.resendDebouncer -= 1
     }
+    bgQueue.schedule { [weak self] in
+      guard let self else { return }
+      do {
+        self.confirmationId = try self.messenger.ud.tryGet()
+          .sendRegisterFact(.init(
+            type: self.isEmail ? .email : .phone,
+            value: self.content
+          ))
+      } catch {
+        self.hudManager.show(.init(error: error))
+      }
+    }
   }
 
   func didTapNext() {
@@ -66,7 +78,7 @@ final class ProfileCodeViewModel {
     bgQueue.schedule { [weak self] in
       guard let self else { return }
       do {
-        try self.messenger.ud.get()!.confirmFact(
+        try self.messenger.ud.tryGet().confirmFact(
           confirmationId: self.confirmationId,
           code: self.stateSubject.value.input
         )
diff --git a/Sources/ProfileFeature/ViewModels/ProfileEmailViewModel.swift b/Sources/ProfileFeature/ViewModels/ProfileEmailViewModel.swift
index 01d93e61..a71210c3 100644
--- a/Sources/ProfileFeature/ViewModels/ProfileEmailViewModel.swift
+++ b/Sources/ProfileFeature/ViewModels/ProfileEmailViewModel.swift
@@ -39,8 +39,11 @@ final class ProfileEmailViewModel {
     bgQueue.schedule { [weak self] in
       guard let self else { return }
       do {
-        let confirmationId = try self.messenger.ud.get()!.sendRegisterFact(
-          .init(type: .email, value: self.stateSubject.value.input)
+        let confirmationId = try self.messenger.ud.tryGet()
+          .sendRegisterFact(.init(
+            type: .email,
+            value: self.stateSubject.value.input
+          )
         )
         self.hudManager.hide()
         self.stateSubject.value.confirmationId = confirmationId
diff --git a/Sources/ProfileFeature/ViewModels/ProfilePhoneViewModel.swift b/Sources/ProfileFeature/ViewModels/ProfilePhoneViewModel.swift
index 02c0d754..b5749cea 100644
--- a/Sources/ProfileFeature/ViewModels/ProfilePhoneViewModel.swift
+++ b/Sources/ProfileFeature/ViewModels/ProfilePhoneViewModel.swift
@@ -48,10 +48,12 @@ final class ProfilePhoneViewModel {
       guard let self else { return }
       let content = "\(self.stateSubject.value.input)\(self.stateSubject.value.country.code)"
       do {
-        let confirmationId = try self.messenger.ud.get()!.sendRegisterFact(
-          .init(type: .phone, value: content)
+        let confirmationId = try self.messenger.ud.tryGet()
+          .sendRegisterFact(.init(
+            type: .phone,
+            value: content
+          )
         )
-
         self.hudManager.hide()
         self.stateSubject.value.content = content
         self.stateSubject.value.confirmationId = confirmationId
diff --git a/Sources/ProfileFeature/ViewModels/ProfileViewModel.swift b/Sources/ProfileFeature/ViewModels/ProfileViewModel.swift
index 4d8e382e..60cddb5d 100644
--- a/Sources/ProfileFeature/ViewModels/ProfileViewModel.swift
+++ b/Sources/ProfileFeature/ViewModels/ProfileViewModel.swift
@@ -91,7 +91,7 @@ final class ProfileViewModel {
     bgQueue.schedule { [weak self] in
       guard let self else { return }
       do {
-        try self.messenger.ud.get()!.removeFact(
+        try self.messenger.ud.tryGet().removeFact(
           .init(
             type: isEmail ? .email : .phone,
             value: isEmail ? self.emailStored! : self.phoneStored!
-- 
GitLab