Example #1
0
        private void Init()
        {
            _canUseAttachments = _cryptoService.EncKey != null;

            SubscribeFileResult(true);
            var selectButton = new ExtendedButton
            {
                Text     = AppResources.ChooseFile,
                Command  = new Command(async() => await _deviceActionService.SelectFileAsync()),
                Style    = (Style)Application.Current.Resources["btn-primaryAccent"],
                FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Button))
            };

            FileLabel = new Label
            {
                Text     = AppResources.NoFileChosen,
                Style    = (Style)Application.Current.Resources["text-muted"],
                FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
                HorizontalTextAlignment = TextAlignment.Center
            };

            AddNewStackLayout = new StackLayout
            {
                Children        = { selectButton, FileLabel },
                Orientation     = StackOrientation.Vertical,
                Padding         = new Thickness(20, Helpers.OnPlatform(iOS: 10, Android: 20), 20, 20),
                VerticalOptions = LayoutOptions.Start
            };

            NewTable = new ExtendedTableView
            {
                Intent              = TableIntent.Settings,
                HasUnevenRows       = true,
                NoFooter            = true,
                EnableScrolling     = false,
                EnableSelection     = false,
                VerticalOptions     = LayoutOptions.Start,
                Margin              = new Thickness(0, Helpers.OnPlatform(iOS: 10, Android: 30), 0, 0),
                WrappingStackLayout = () => NoDataStackLayout,
                Root = new TableRoot
                {
                    new TableSection(AppResources.AddNewAttachment)
                    {
                        new ExtendedViewCell
                        {
                            View            = AddNewStackLayout,
                            BackgroundColor = Color.White
                        }
                    }
                }
            };

            ListView = new ExtendedListView(ListViewCachingStrategy.RecycleElement)
            {
                ItemsSource     = PresentationAttchments,
                HasUnevenRows   = true,
                ItemTemplate    = new DataTemplate(() => new VaultAttachmentsViewCell()),
                VerticalOptions = LayoutOptions.FillAndExpand
            };

            NoDataLabel = new Label
            {
                Text = AppResources.NoAttachments,
                HorizontalTextAlignment = TextAlignment.Center,
                FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
                Style    = (Style)Application.Current.Resources["text-muted"]
            };

            NoDataStackLayout = new RedrawableStackLayout
            {
                VerticalOptions = LayoutOptions.Start,
                Spacing         = 0,
                Margin          = new Thickness(0, 40, 0, 0)
            };

            SaveToolbarItem = new ToolbarItem(AppResources.Save, Helpers.ToolbarImage("envelope.png"), async() =>
            {
                if (_lastAction.LastActionWasRecent() || _cipher == null)
                {
                    return;
                }
                _lastAction = DateTime.UtcNow;


                if (!_canUseAttachments)
                {
                    await ShowUpdateKeyAsync();
                    return;
                }

                if (!_connectivity.IsConnected)
                {
                    AlertNoConnection();
                    return;
                }

                if (_fileBytes == null)
                {
                    await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
                                                                                      AppResources.File), AppResources.Ok);
                    return;
                }

                await _deviceActionService.ShowLoadingAsync(AppResources.Saving);
                var saveTask = await _cipherService.EncryptAndSaveAttachmentAsync(_cipher, _fileBytes, FileLabel.Text);
                await _deviceActionService.HideLoadingAsync();

                if (saveTask.Succeeded)
                {
                    _fileBytes     = null;
                    FileLabel.Text = AppResources.NoFileChosen;
                    _deviceActionService.Toast(AppResources.AttachementAdded);
                    _googleAnalyticsService.TrackAppEvent("AddedAttachment");
                    await LoadAttachmentsAsync();
                }
                else if (saveTask.Errors.Count() > 0)
                {
                    await DisplayAlert(AppResources.AnErrorHasOccurred, saveTask.Errors.First().Message, AppResources.Ok);
                }
                else
                {
                    await DisplayAlert(null, AppResources.AnErrorHasOccurred, AppResources.Ok);
                }
            }, ToolbarItemOrder.Default, 0);

            Title   = AppResources.Attachments;
            Content = ListView;

            if (Device.RuntimePlatform == Device.iOS)
            {
                ListView.RowHeight          = -1;
                NewTable.RowHeight          = -1;
                NewTable.EstimatedRowHeight = 44;
                NewTable.HeightRequest      = 180;
                ListView.BackgroundColor    = Color.Transparent;
                ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Close));
            }
            else if (Device.RuntimePlatform == Device.Android)
            {
                ListView.BottomPadding = 50;
            }
        }