Example #1
0
        async partial void SaveBarButton_Activated(UIBarButtonItem sender)
        {
            if (!_connectivity.IsConnected)
            {
                AlertNoConnection();
                return;
            }

            if (string.IsNullOrWhiteSpace(PasswordCell.TextField.Text))
            {
                DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Password), AppResources.Ok);
                return;
            }

            if (string.IsNullOrWhiteSpace(NameCell.TextField.Text))
            {
                DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Name), AppResources.Ok);
                return;
            }

            var login = new Login
            {
                Uri      = string.IsNullOrWhiteSpace(UriCell.TextField.Text) ? null : UriCell.TextField.Text.Encrypt(),
                Name     = string.IsNullOrWhiteSpace(NameCell.TextField.Text) ? null : NameCell.TextField.Text.Encrypt(),
                Username = string.IsNullOrWhiteSpace(UsernameCell.TextField.Text) ? null : UsernameCell.TextField.Text.Encrypt(),
                Password = string.IsNullOrWhiteSpace(PasswordCell.TextField.Text) ? null : PasswordCell.TextField.Text.Encrypt(),
                Notes    = string.IsNullOrWhiteSpace(NotesCell.TextView.Text) ? null : NotesCell.TextView.Text.Encrypt(),
                Favorite = FavoriteCell.Switch.On,
                FolderId = FolderCell.SelectedIndex == 0 ? null : _folders.ElementAtOrDefault(FolderCell.SelectedIndex - 1)?.Id
            };

            var saveTask     = _loginService.SaveAsync(login);
            var loadingAlert = Dialogs.CreateLoadingAlert(AppResources.Saving);

            PresentViewController(loadingAlert, true, null);
            await saveTask;

            if (saveTask.Result.Succeeded)
            {
                _googleAnalyticsService.TrackExtensionEvent("CreatedLogin");
                if (LoginListController != null)
                {
                    LoginListController.DismissModal();
                }
                else if (LoadingController != null)
                {
                    LoadingController.CompleteUsernamePasswordRequest(UsernameCell.TextField.Text, PasswordCell.TextField.Text,
                                                                      null);
                }
            }
            else if (saveTask.Result.Errors.Count() > 0)
            {
                DisplayAlert(AppResources.AnErrorHasOccurred, saveTask.Result.Errors.First().Message, AppResources.Ok);
            }
            else
            {
                DisplayAlert(null, AppResources.AnErrorHasOccurred, AppResources.Ok);
            }
        }