Ejemplo n.º 1
0
        private void Button_Clicked(object sender, EventArgs e)
        {
            try
            {
                var isSuccessed = false;
                this.IsBusy = true;
                (sender as VisualElement).IsEnabled = false;

                Task.Run(() =>
                {
                    try
                    {
                        var loginResult = Shared.APIs.ILogins.GetLoginToken(emailEntry.Text, passwordEntry.Text);
                        if (loginResult != null)
                        {
                            if (loginResult.Token != null && loginResult.OK)
                            {
                                Shared.LoginToken   = loginResult.Token;
                                Shared.LocalAddress = ServiceAddress;

                                var userModel = Shared.APIs.IUsers.GetCurrentUser();
                                UserModel     = userModel;
                                Shared.UserId = userModel.ID;

                                try
                                {
                                    string notificationId = mAppServices.GetRegistrationNotificationId();
                                    string uniqueDeviceId = mAppServices.UniqueDeviceId;

                                    if (!string.IsNullOrEmpty(notificationId))
                                    {
                                        var platform = "android";
                                        if (Device.RuntimePlatform == Device.iOS)
                                        {
                                            platform = "ios";
                                        }
                                        bool isDeviceTokenSet = Shared.APIs.IUsers.AddDeviceToken(userModel.ID, platform, notificationId, uniqueDeviceId);
                                        if (!isDeviceTokenSet)
                                        {
                                            throw new Exception(AppResources.RegisterNotificationFail);
                                        }
                                        Shared.NotificationToken = notificationId;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Device.BeginInvokeOnMainThread(() =>
                                    {
                                        (sender as VisualElement).IsEnabled = true;
                                        Utils.ShowErrorMessage(new CustomException(ex.Message, ex));
                                    });
                                }

                                isSuccessed = true;
                                System.Diagnostics.Debug.WriteLine("UserID : " + UserModel.ID + " UserName : " + UserModel.Username);
                            }
                            else
                            {
                                Device.BeginInvokeOnMainThread(() =>
                                {
                                    (sender as VisualElement).IsEnabled = true;
                                    Utils.ShowErrorMessage(loginResult.Message);
                                });
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            (sender as VisualElement).IsEnabled = true;
                            Utils.ShowErrorMessage(ex);
                        });
                    }
                }).ContinueWith(t =>
                {
                    if (isSuccessed)
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            this.IsBusy = false;
                            if (this.Done != null)
                            {
                                this.Done(UserModel);
                            }
                            else
                            {
                                Application.Current.MainPage = new HomePage();
                            }
                        });
                    }
                    else
                    {
                        (sender as VisualElement).IsEnabled = true;
                        this.IsBusy = false;
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext());
            }
            catch (Exception exeption)
            {
                ExceptionHandler.Catch(exeption);
            }
        }