Pārlūkot izejas kodu

PRS MOBILE - rework of login and pin pages for stable login/deep linking

Nick 2 gadi atpakaļ
vecāks
revīzija
8a55049874

+ 111 - 35
prs.mobile/comal.timesheets/Main/App.xaml.cs

@@ -16,6 +16,7 @@ using SkiaSharp;
 using System.Threading;
 using System.Linq;
 using System.Collections.Generic;
+using static Android.Icu.Text.AlphabeticIndex;
 
 //[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
 namespace comal.timesheets
@@ -46,6 +47,7 @@ namespace comal.timesheets
 
         public App()
         {
+            InitializeComponent();
             LoadAll();
         }
 
@@ -53,67 +55,140 @@ namespace comal.timesheets
         {
             try
             {
-                Material.Init(this);
-                InitializeComponent();
-                Material.Use("Material.Configuration");
+                InitAndRegister();
 
-                MobileUtils.Init();
-                CoreUtils.RegisterClasses();
-                ComalUtils.RegisterClasses();
+                LoadSettings();
 
-                FindDeviceInfo();
+                ApplyComalSettings();
 
-                Settings = new LocalConfiguration<ConnectionSettings>().Load();
+                SaveSettings();
 
-                DBSettings = new LocalConfiguration<DatabaseSettings>().Load();
+                SetupClient();
 
-                if (!string.IsNullOrWhiteSpace(Settings.UserID) && string.IsNullOrWhiteSpace(DBSettings.UserID))
-                    DBSettings.UserID = Settings.UserID;
-                if (!string.IsNullOrWhiteSpace(Settings.Password) && string.IsNullOrWhiteSpace(DBSettings.Password))
-                    DBSettings.Password = Settings.Password;
+                FinishSetup();
 
-                List<string> list = new List<string>();
-                list.Add("remote.com-al.com.au:8000");
-                list.Add("remote2.com-al.com.au:8000");
-                list.Add("remote3.com-al.com.au:8000");
+                LaunchLoginPage();
+            }
+            catch
+            {
+                LaunchLoginPage();
+            }
+        }
 
-                DBSettings.URLs = list.ToArray();
+        private void LaunchLoginPage()
+        {
+            MainPage = new MaterialNavigationPage(new PINLoginPage());
+        }
 
-                new LocalConfiguration<DatabaseSettings>().Save(DBSettings);
-                MobileUtils.SaveToSecureStorage();
+        private void FinishSetup()
+        {
+            GlobalVariables.InternalOnAppearing = true;
+            GlobalVariables.ChangeUser = false;
 
-                if (!string.IsNullOrWhiteSpace(GlobalVariables.LoadFromLinkString))
-                {
-                    MobileUtils.LoadFromLink();
-                }
+            RunTimers();
+        }
 
-                var result = JsonClient<User>.Ping(DBSettings.URLs, out DatabaseInfo info);
+        private void SetupClient()
+        {
+            if (CheckLoadFromLink())
+                return;
 
+            try
+            {
+                var result = JsonClient<User>.Ping(DBSettings.URLs, out DatabaseInfo info);
                 ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + DeviceString, result, true);
+            }
+            catch { }
+        }
 
-                GlobalVariables.InternalOnAppearing = true;
-                GlobalVariables.ChangeUser = false;
+        private bool CheckLoadFromLink()
+        {
+            if (!string.IsNullOrWhiteSpace(GlobalVariables.LoadFromLinkString))
+            {
+                MobileUtils.LoadFromLink();
+                return true;
+            }
+            return false;
+        }
 
-                if (!reload)
-                    RunTimers();
+        private void SaveSettings()
+        {
+            try
+            {
+                new LocalConfiguration<DatabaseSettings>().Save(DBSettings);
+                MobileUtils.SaveToSecureStorage();
+            }
+            catch { }
+        }
 
-                MainPage = new MaterialNavigationPage(new PINLoginPage());
+        private void ApplyComalSettings() //temporary fix
+        {
+            try
+            {
+                List<string> list = new List<string>
+            {
+                "remote.com-al.com.au:8000",
+                "remote2.com-al.com.au:8000",
+                "remote3.com-al.com.au:8000"
+            };
+
+                DBSettings.URLs = list.ToArray();
             }
-            catch
+            catch { }
+        }
+
+        private void LoadSettings()
+        {
+            try
+            {
+                Settings = new LocalConfiguration<ConnectionSettings>().Load();
+
+                DBSettings = new LocalConfiguration<DatabaseSettings>().Load();
+
+                if (!string.IsNullOrWhiteSpace(Settings.UserID) && string.IsNullOrWhiteSpace(DBSettings.UserID))
+                    DBSettings.UserID = Settings.UserID;
+                if (!string.IsNullOrWhiteSpace(Settings.Password) && string.IsNullOrWhiteSpace(DBSettings.Password))
+                    DBSettings.Password = Settings.Password;
+            }
+            catch { }
+        }
+
+        private void InitAndRegister()
+        {
+            try
             {
-                Thread.Sleep(1000);
-                LoadAll(true);
+                Material.Init(this);
+
+                Material.Use("Material.Configuration");
+
+                MobileUtils.Init();
+                CoreUtils.RegisterClasses();
+                ComalUtils.RegisterClasses();
+
+                FindDeviceInfo();
             }
+            catch { }
         }
 
         private void RunTimers()
         {
-            Task.Run(() =>
+            try
             {
                 GPS.GetLocation();
+            }
+            catch
+            {
+
+            }
 
+            try
+            {
                 Bluetooth.ScanForDevices();
+            }
+            catch { }
 
+            try
+            {
                 Device.StartTimer(new TimeSpan(0, 0, 30), () =>
                 {
                     if (App.IsInForeground)
@@ -131,7 +206,8 @@ namespace comal.timesheets
                     }
                     return true;
                 });
-            });
+            }
+            catch { }
         }
 
         private async void TryLoadFromSecureCache()

+ 65 - 35
prs.mobile/comal.timesheets/Main/PINLoginPage.xaml.cs

@@ -32,7 +32,7 @@ namespace comal.timesheets
                 ShowPINPad();
                 CheckForLatestVersion();
                 if (!string.IsNullOrWhiteSpace(logindetails))
-                    loadFromIOSLink = logindetails;               
+                    loadFromIOSLink = logindetails;
             }
             catch
             {
@@ -41,6 +41,53 @@ namespace comal.timesheets
         }
 
         protected override void OnAppearing()
+        {
+            try
+            {
+                CheckLoadFromLink();
+
+                if (!CheckAndPaste2FAText())
+                    return;
+
+                SetPinAndShowSplash();
+
+                if (App.IsUserLoggedIn && GlobalVariables.ChangeUser == false)
+                {
+                    LaunchMainPage();
+                    return;
+                }
+                else
+                {
+                    if (String.IsNullOrWhiteSpace(App.DBSettings.UserID) || String.IsNullOrWhiteSpace(App.DBSettings.Password))
+                    {
+                        DisplayAlert("Unable to log in", "User ID or password is blank", "OK");
+                        ShowPINPad();
+                        return;
+                    }
+                    RunValidate();
+                }
+
+            }
+            catch
+            {
+                ShowPINPad();
+            }
+            base.OnAppearing();
+        }
+
+        private void SetPinAndShowSplash()
+        {
+            try
+            {
+                GlobalVariables.InternalOnAppearing = false;
+                pin = "";
+                PIN.Text = "";
+                ShowSplashPage();
+            }
+            catch { }
+        }
+
+        private void CheckLoadFromLink()
         {
             try
             {
@@ -55,11 +102,18 @@ namespace comal.timesheets
                     if (GlobalVariables.LoadFromLinkString == "Link has expired")
                         DisplayAlert("Alert", "Link has expired", "OK");
                 }
+            }
+            catch { }
+        }
 
+        private bool CheckAndPaste2FAText()
+        {
+            try
+            {
                 if (!GlobalVariables.InternalOnAppearing)
                 {
                     if (!twoFAvisible)
-                        return;
+                        return false;
                     Device.BeginInvokeOnMainThread(async () =>
                     {
                         try
@@ -70,43 +124,19 @@ namespace comal.timesheets
                             else
                                 ent0.Text = text;
                         }
-                        catch { }
-                    });
-                    return;
-                }
-
-                GlobalVariables.InternalOnAppearing = false;
-                pin = "";
-                PIN.Text = "";
-                ShowSplashPage();
-                Task.Run(() =>
-                {
-                    Thread.Sleep(250);
-                    Device.BeginInvokeOnMainThread(() =>
-                    {
-                        if (App.IsUserLoggedIn && GlobalVariables.ChangeUser == false)
+                        catch
                         {
-                            LaunchMainPage();
-                            return;
-                        }
-                        else
-                        {
-                            if (String.IsNullOrWhiteSpace(App.DBSettings.UserID) || String.IsNullOrWhiteSpace(App.DBSettings.Password))
-                            {
-                                DisplayAlert("Unable to log in", "User ID or password is blank", "OK");
-                                ShowPINPad();
-                                return;
-                            }
-                            RunValidate();
                         }
                     });
-                });
+                    return false;
+                }
+                return true;
+
             }
-            catch 
-            { 
-                ShowPINPad(); 
+            catch
+            {
+                return true;
             }
-            base.OnAppearing();
         }
 
         private async void RunValidate(bool usePIN = false, string pin = "")
@@ -745,7 +775,7 @@ namespace comal.timesheets
         private async void Settingsform_Disappearing(object sender, EventArgs e)
         {
             try
-            {       
+            {
                 if (App.IsUserLoggedIn)
                     await Navigation.PushAsync(new MainPage());
             }