From 7d06bb5285b6071aaa464d0019bc782be5199f52 Mon Sep 17 00:00:00 2001
From: Dani Palou <dani@moodle.com>
Date: Thu, 16 Sep 2021 16:39:48 +0200
Subject: [PATCH] MOBILE-2282 login: Call getPublicConfig in background when
 loading site

---
 src/core/services/navigator.ts |  6 ++--
 src/core/services/sites.ts     | 53 +++++++++++++++++++---------------
 2 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/src/core/services/navigator.ts b/src/core/services/navigator.ts
index 2e3ad5a0c..6e5e471de 100644
--- a/src/core/services/navigator.ts
+++ b/src/core/services/navigator.ts
@@ -532,10 +532,8 @@ export class CoreNavigatorService {
 
         const pathRoot = /^[^/]+/.exec(path)?.[0] ?? '';
         const currentMainMenuTab = this.getCurrentMainMenuTab();
-        const isMainMenuTab = await CoreUtils.ignoreErrors(
-            CoreMainMenu.isMainMenuTab(pathRoot),
-            false,
-        );
+        const isMainMenuTab = pathRoot === currentMainMenuTab || (!currentMainMenuTab && path === DEFAULT_MAIN_MENU_TAB) ||
+            await CoreUtils.ignoreErrors(CoreMainMenu.isMainMenuTab(pathRoot), false);
 
         if (!options.preferCurrentTab && isMainMenuTab) {
             return this.navigate(`/main/${path}`, options);
diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts
index 26e8901b5..fe282d19c 100644
--- a/src/core/services/sites.ts
+++ b/src/core/services/sites.ts
@@ -713,10 +713,11 @@ export class CoreSitesProvider {
 
             const siteId = this.getCurrentSiteId();
             const downloadUrl = CoreApp.getAppStoreUrl(storesConfig);
+            let promise: Promise<unknown>;
 
             if (downloadUrl != null) {
                 // Do not block interface.
-                CoreDomUtils.showConfirm(
+                promise = CoreDomUtils.showConfirm(
                     Translate.instant('core.updaterequireddesc', { $a: config.tool_mobile_minimumversion }),
                     Translate.instant('core.updaterequired'),
                     Translate.instant('core.download'),
@@ -725,19 +726,20 @@ export class CoreSitesProvider {
                     // Do nothing.
                 });
             } else {
-                CoreDomUtils.showAlert(
+                // Do not block interface.
+                promise = CoreDomUtils.showAlert(
                     Translate.instant('core.updaterequired'),
                     Translate.instant('core.updaterequireddesc', { $a: config.tool_mobile_minimumversion }),
-                );
+                ).then((alert) => alert.onWillDismiss());
             }
 
-            if (siteId) {
-                // Logout the currentSite.
-                await this.logout();
-
-                // Always expire the token.
-                await this.setSiteLoggedOut(siteId, true);
-            }
+            promise.finally(() => {
+                if (siteId) {
+                    // Logout the currentSite and expire the token.
+                    this.logout();
+                    this.setSiteLoggedOut(siteId, true);
+                }
+            });
 
             throw new CoreError('Current app version is lower than required version.');
         }
@@ -794,24 +796,27 @@ export class CoreSitesProvider {
             return false;
         }
 
-        let config: CoreSitePublicConfigResponse | undefined;
+        this.login(siteId);
+        // Get some data in background, don't block the UI.
+        this.getPublicConfigAndCheckApplication(site);
+        this.updateSiteInfo(siteId);
 
-        try {
-            config = await site.getPublicConfig();
-        } catch (error) {
-            // Error getting config, maybe the user is offline.
-        }
+        return true;
+    }
 
+    /**
+     * Get site public config and check if app can access the site.
+     *
+     * @param site Site.
+     * @return Promise resolved when done.
+     */
+    protected async getPublicConfigAndCheckApplication(site: CoreSite): Promise<void> {
         try {
+            const config = await site.getPublicConfig();
+
             await this.checkApplication(config);
-
-            this.login(siteId);
-            // Update site info. We don't block the UI.
-            this.updateSiteInfo(siteId);
-
-            return true;
-        } catch (error) {
-            return false;
+        } catch {
+            // Ignore errors, maybe the user is offline.
         }
     }