Example #1
0
        /// <summary>
        /// Gets the login page.
        /// </summary>
        public void GetLoginPage()
        {
            Credentials       = CredentialsManager.GetAllCredentials();
            SystemCredentials = CredentialsManager.GetSystemCredentials();
            var p1   = new SystemLoginPage();
            var page = new NavigationPage(p1);

            if (!string.IsNullOrEmpty(SystemCredentials.Key))
            {
                int    sysLoginCount    = 0;
                string sysLoginAnswer   = "";
                bool   sysLoginFinished = false;
                Task.Run(async() =>
                {
                    while (sysLoginAnswer != PheidiNetworkManager.GoodResult && sysLoginAnswer != AppResources.Erreur_MauvaisEmailOuMdp && sysLoginCount < 10)
                    {
                        sysLoginAnswer = await PheidiNetworkManager.SystemLogin(SystemCredentials.Value["Username"], SystemCredentials.Value["Password"]);
                        sysLoginCount++;
                    }
                    sysLoginFinished = true;
                });

                while (!sysLoginFinished)
                {
                    Task.Delay(500).Wait();
                }
                if (sysLoginAnswer == PheidiNetworkManager.GoodResult)
                {
                    Page p2 = new ServerLoginPage();
                    if (ServerInfoList.Count == 0)
                    {
                        MainPage = page;
                        NotificationManager.DisplayAlert(AppResources.Erreur_AucunServeur, AppResources.Erreur_Title, "OK", () => { });
                    }
                    else if (ServerInfoList.Count == 1)
                    {
                        bool   serverLoginFinished = false;
                        int    serverLoginCount    = 0;
                        string answer = "";
                        Task.Run(async() =>
                        {
                            if (Credentials.Any((arg) => arg.Value["SystemCredentialsNoseq"] == SystemCredentials.Key && arg.Value["ServerNoseq"] == ServerInfoNoseq))
                            {
                                var credentials = Credentials.First((arg) => arg.Value["SystemCredentialsNoseq"] == SystemCredentials.Key && arg.Value["ServerNoseq"] == ServerInfoList[0].Noseq);
                                answer          = await PheidiNetworkManager.UserLogin(credentials.Value["Username"], credentials.Value["Password"], false);
                                if (answer != PheidiNetworkManager.GoodResult)
                                {
                                    CredentialsManager.DeleteUser(credentials.Key);
                                }
                                else
                                {
                                    UserNoseq = credentials.Key;
                                }
                            }
                            else
                            {
                                while ((answer != PheidiNetworkManager.GoodResult && answer != AppResources.Erreur_MauvaisEmailOuMdp) && serverLoginCount < 10)
                                {
                                    answer = await PheidiNetworkManager.UserLogin(SystemCredentials.Value["Username"], SystemCredentials.Value["Password"], false);
                                    serverLoginCount++;
                                }

                                if (answer == PheidiNetworkManager.GoodResult)
                                {
                                    UserNoseq = SystemCredentials.Key;
                                }
                            }
                            serverLoginFinished = true;
                        });

                        while (!serverLoginFinished)
                        {
                            Task.Delay(500).Wait();
                        }

                        if (answer != PheidiNetworkManager.GoodResult)
                        {
                            page.Navigation.PushAsync(p2);
                            MainPage = page;
                        }
                        else if (answer == PheidiNetworkManager.GoodResult)
                        {
                            answer = string.Empty;
                            Task.Run(async() =>
                            {
                                answer = await PheidiNetworkManager.GetPMH();
                            });
                            while (string.IsNullOrEmpty(answer))
                            {
                                Task.Delay(500).Wait();
                            }
                            if (PMH.Count > 1)
                            {
                                Page p3 = new PmhPage();
                                page.Navigation.PushAsync(p3);
                                MainPage = page;
                            }
                            else
                            {
                                Instance.GetToApplication();
                            }
                        }
                    }
                    else
                    {
                        page.Navigation.PushAsync(p2);
                        MainPage = page;
                    }
                }
                else
                {
                    MainPage = page;
                }
            }
            else
            {
                MainPage = page;
                if (!Current.Properties.ContainsKey("DeviceIsPublic"))
                {
                    string        message   = AppResources.Alerte_SeulUsagerAppareil_Message;
                    string        title     = AppResources.Alerte_SeulUsagerAppareil_Title;
                    string        confirm   = AppResources.Oui;
                    string        cancel    = AppResources.Non;
                    System.Action onConfirm = () => { DeviceIsShared = false; };
                    System.Action onCancel  = () => { DeviceIsShared = true; };
                    NotificationManager.DisplayAlert(message, title, confirm, cancel, onConfirm, onCancel);
                }
            }
        }
Example #2
0
        public SystemLoginPage()
        {
            //Cache la nav bar
            NavigationPage.SetHasNavigationBar(this, false);
            InitializeComponent();

            //Bouton de login
            btnLogin.Clicked += (sender, e) =>
            {
                Task.Run(async() =>
                {
                    Device.BeginInvokeOnMainThread(() => AppLoadingView.SetVisibility(true));
                    string answer = await PheidiNetworkManager.SystemLogin(usernameEntry.Text, passwordEntry.Text);
                    if (answer == PheidiNetworkManager.GoodResult)
                    {
                        App.CredentialsManager.DeleteSystemCredentials();
                        App.CredentialsManager.SaveSystemCredentials(usernameEntry.Text, passwordEntry.Text);
                        DatabaseHelper.Database.DropTable <Geofence>();
                        DatabaseHelper.Database.CreateTable <Geofence>();
                        Device.BeginInvokeOnMainThread(App.Instance.GetLoginPage);
                    }
                    else
                    {
                        App.NotificationManager.DisplayAlert(answer, AppResources.Erreur_Title, "OK", () => { });
                    }
                    Device.BeginInvokeOnMainThread(() => AppLoadingView.SetVisibility(false));
                });
            };



            usernameEntry.Placeholder = AppResources.CourrielPlaceHolder;
            passwordEntry.Placeholder = AppResources.MotDePassePlaceHolder;
            btnLogin.Text             = AppResources.ConnexionBouton;

            var systemAccount = App.CredentialsManager.GetSystemCredentials();

            SetEntryLayoutVisibility(string.IsNullOrEmpty(systemAccount.Key));
            if (systemAccount.Value != null)
            {
                btnCurrentAccount.Text = systemAccount.Value["Username"];
            }
            btnCurrentAccount.BackgroundColor = App.ColorPrimary;
            btnCurrentAccount.TextColor       = Color.White;
            //btnCurrentAccount.BorderColor = App.ColorPrimary;
            btnCurrentAccount.Clicked += (sender, e) =>
            {
                Task.Run(async() =>
                {
                    Device.BeginInvokeOnMainThread(() => AppLoadingView.SetVisibility(true));
                    int count       = 0;
                    string answer   = "";
                    var credentials = App.CredentialsManager.GetSystemCredentials();
                    while (answer != PheidiNetworkManager.GoodResult && answer != AppResources.Erreur_MauvaisEmailOuMdp && count < 5)
                    {
                        answer = await PheidiNetworkManager.SystemLogin(credentials.Value["Username"], credentials.Value["Password"]);
                        count++;
                    }
                    if (answer == PheidiNetworkManager.GoodResult)
                    {
                        Device.BeginInvokeOnMainThread(App.Instance.GetLoginPage);
                    }
                    else
                    {
                        if (answer == AppResources.Erreur_MauvaisEmailOuMdp)
                        {
                            Device.BeginInvokeOnMainThread(() => updateEntryLayout.IsVisible = true);
                        }
                        App.NotificationManager.DisplayAlert(answer, AppResources.Erreur_Title, "OK", () => { });
                    }
                    Device.BeginInvokeOnMainThread(() => AppLoadingView.SetVisibility(false));
                });
            };

            btnOtherAccount.TextColor       = App.ColorPrimary;
            btnOtherAccount.BorderColor     = App.ColorPrimary;
            btnOtherAccount.BackgroundColor = Color.White;
            btnOtherAccount.Text            = AppResources.AutreCompteBouton;
            btnOtherAccount.Clicked        += (sender, e) =>
            {
                SetEntryLayoutVisibility(true);
            };

            btnBackToMainAccount.IsVisible       = !string.IsNullOrEmpty(systemAccount.Key);
            btnBackToMainAccount.TextColor       = App.ColorPrimary;
            btnBackToMainAccount.BorderColor     = App.ColorPrimary;
            btnBackToMainAccount.BackgroundColor = Color.White;
            btnBackToMainAccount.Text            = AppResources.RetourBouton;
            btnBackToMainAccount.Clicked        += (sender, e) =>
            {
                SetEntryLayoutVisibility(false);
            };

            demiCercle.Foreground = App.ColorPrimary;

            updateUsernameEntry.Placeholder = AppResources.CourrielPlaceHolder;
            updatePasswordEntry.Placeholder = AppResources.MotDePassePlaceHolder;
            btnUpdateLogin.Text             = AppResources.ConnexionBouton;
            //Bouton de login
            btnUpdateLogin.FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Button));
            btnUpdateLogin.Clicked += (sender, e) =>
            {
                Task.Run(async() =>
                {
                    Device.BeginInvokeOnMainThread(() => AppLoadingView.SetVisibility(true));
                    string answer = await PheidiNetworkManager.SystemLogin(updateUsernameEntry.Text, updatePasswordEntry.Text);
                    if (answer == PheidiNetworkManager.GoodResult)
                    {
                        var credentials = App.CredentialsManager.GetSystemCredentials();
                        credentials.Value["Password"] = updatePasswordEntry.Text;
                        credentials.Value["Username"] = updateUsernameEntry.Text;
                        App.CredentialsManager.UpdateSystemCredentials(credentials);
                        Device.BeginInvokeOnMainThread(App.Instance.GetLoginPage);
                    }
                    else
                    {
                        App.NotificationManager.DisplayAlert(answer, AppResources.Erreur_Title, "OK", () => { });
                    }
                    Device.BeginInvokeOnMainThread(() => AppLoadingView.SetVisibility(false));
                });
            };

            SetFooter();
            mainLayout.RaiseChild(AppLoadingView);
            AppLoadingView.SetVisibility(false);
        }