From 5f71dc605666a0c8dd7bf11c768a6eeca6b0ff5f Mon Sep 17 00:00:00 2001
From: ryanzhao <ryanzhaors@qq.com>
Date: Tue, 22 Aug 2023 17:10:11 +1000
Subject: [PATCH] WIP: load account logic

---
 Session/Onboarding/LoadingView.swift | 38 +++++++++++++++++++++++-----
 1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/Session/Onboarding/LoadingView.swift b/Session/Onboarding/LoadingView.swift
index 0d71e3505..6414c4a65 100644
--- a/Session/Onboarding/LoadingView.swift
+++ b/Session/Onboarding/LoadingView.swift
@@ -9,6 +9,7 @@ struct LoadingView: View {
     @EnvironmentObject var host: HostWrapper
     
     @State var percentage: Double = 0.0
+    @State var animationTimer: Timer?
     
     private let flow: Onboarding.Flow
     
@@ -36,6 +37,7 @@ struct LoadingView: View {
                         .padding(.bottom, Values.mediumSpacing)
                         .onAppear {
                             progress()
+                            observeProfileRetrieving()
                         }
                     
                     Text("onboarding_load_account_waiting".localized())
@@ -56,7 +58,7 @@ struct LoadingView: View {
     }
     
     private func progress() {
-        Timer.scheduledTimerOnMainThread(
+        animationTimer = Timer.scheduledTimerOnMainThread(
             withTimeInterval: 0.15,
             repeats: true
         ) { timer in
@@ -64,15 +66,39 @@ struct LoadingView: View {
             if percentage >= 1 {
                 self.percentage = 1
                 timer.invalidate()
-                finishLoading()
+                finishLoading(success: false)
             }
         }
     }
     
-    private func finishLoading() {
-        let viewController: SessionHostingViewController = SessionHostingViewController(rootView: DisplayNameView(flow: flow))
-        viewController.setUpNavBarSessionIcon()
-        self.host.controller?.navigationController?.pushViewController(viewController, animated: true)
+    private func observeProfileRetrieving() {
+        Onboarding.profileNamePublisher
+            .subscribe(on: DispatchQueue.global(qos: .userInitiated))
+            .receive(on: DispatchQueue.main)
+            .sinkUntilComplete(
+                receiveValue: { displayName in
+                    finishLoading(success: true)
+                }
+            )
+    }
+    
+    private func finishLoading(success: Bool) {
+        guard success else {
+            let viewController: SessionHostingViewController = SessionHostingViewController(rootView: DisplayNameView(flow: flow))
+            viewController.setUpNavBarSessionIcon()
+            self.host.controller?.navigationController?.pushViewController(viewController, animated: true)
+            return
+        }
+        self.animationTimer?.invalidate()
+        self.animationTimer = nil
+        Timer.scheduledTimerOnMainThread(withTimeInterval: 0.3) { _ in
+            self.percentage = 1
+        }
+        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
+            let homeVC: HomeVC = HomeVC(flow: self.flow)
+            self.host.controller?.navigationController?.setViewControllers([ homeVC ], animated: true)
+        }
+        
     }
 }