2016-05-18 06:09:20 +03:00
|
|
|
|
using System;
|
|
|
|
|
using Acr.UserDialogs;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Bit.App.Controls;
|
|
|
|
|
using Bit.App.Resources;
|
|
|
|
|
using Plugin.Connectivity.Abstractions;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using XLabs.Ioc;
|
2016-07-20 01:46:39 +03:00
|
|
|
|
using System.Linq;
|
2016-05-18 06:09:20 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
2016-06-28 02:53:31 +03:00
|
|
|
|
public class SettingsEditFolderPage : ExtendedContentPage
|
2016-05-18 06:09:20 +03:00
|
|
|
|
{
|
|
|
|
|
private readonly string _folderId;
|
|
|
|
|
private readonly IFolderService _folderService;
|
|
|
|
|
private readonly IUserDialogs _userDialogs;
|
|
|
|
|
private readonly IConnectivity _connectivity;
|
2016-08-05 02:35:56 +03:00
|
|
|
|
private readonly IGoogleAnalyticsService _googleAnalyticsService;
|
2016-05-18 06:09:20 +03:00
|
|
|
|
|
|
|
|
|
public SettingsEditFolderPage(string folderId)
|
|
|
|
|
{
|
|
|
|
|
_folderId = folderId;
|
|
|
|
|
_folderService = Resolver.Resolve<IFolderService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
|
|
|
|
_connectivity = Resolver.Resolve<IConnectivity>();
|
2016-08-05 02:35:56 +03:00
|
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
2016-05-18 06:09:20 +03:00
|
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Init()
|
|
|
|
|
{
|
|
|
|
|
var folder = _folderService.GetByIdAsync(_folderId).GetAwaiter().GetResult();
|
|
|
|
|
if(folder == null)
|
|
|
|
|
{
|
|
|
|
|
// TODO: handle error. navigate back? should never happen...
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var nameCell = new FormEntryCell(AppResources.Name);
|
|
|
|
|
nameCell.Entry.Text = folder.Name.Decrypt();
|
|
|
|
|
|
2016-05-18 06:25:40 +03:00
|
|
|
|
var deleteCell = new ExtendedTextCell { Text = AppResources.Delete, TextColor = Color.Red };
|
|
|
|
|
deleteCell.Tapped += DeleteCell_Tapped;
|
|
|
|
|
|
2016-05-18 06:09:20 +03:00
|
|
|
|
var mainTable = new ExtendedTableView
|
|
|
|
|
{
|
|
|
|
|
Intent = TableIntent.Settings,
|
|
|
|
|
EnableScrolling = false,
|
2016-05-18 06:25:40 +03:00
|
|
|
|
HasUnevenRows = true,
|
2016-07-03 09:57:09 +03:00
|
|
|
|
VerticalOptions = LayoutOptions.Start,
|
2016-05-18 06:09:20 +03:00
|
|
|
|
Root = new TableRoot
|
|
|
|
|
{
|
2016-07-05 05:31:15 +03:00
|
|
|
|
new TableSection
|
2016-05-18 06:09:20 +03:00
|
|
|
|
{
|
|
|
|
|
nameCell
|
2016-07-05 05:31:15 +03:00
|
|
|
|
},
|
|
|
|
|
new TableSection
|
|
|
|
|
{
|
|
|
|
|
deleteCell
|
2016-05-18 06:09:20 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
|
|
|
|
mainTable.RowHeight = -1;
|
|
|
|
|
mainTable.EstimatedRowHeight = 70;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var saveToolBarItem = new ToolbarItem(AppResources.Save, null, async () =>
|
|
|
|
|
{
|
|
|
|
|
if(!_connectivity.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(nameCell.Entry.Text))
|
|
|
|
|
{
|
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Name), AppResources.Ok);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
folder.Name = nameCell.Entry.Text.Encrypt();
|
|
|
|
|
|
|
|
|
|
var saveTask = _folderService.SaveAsync(folder);
|
|
|
|
|
_userDialogs.ShowLoading("Saving...", MaskType.Black);
|
|
|
|
|
await saveTask;
|
|
|
|
|
|
|
|
|
|
_userDialogs.HideLoading();
|
2016-07-20 01:46:39 +03:00
|
|
|
|
|
|
|
|
|
if(saveTask.Result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
await Navigation.PopModalAsync();
|
2016-07-26 07:38:41 +03:00
|
|
|
|
_userDialogs.Toast("Folder updated.");
|
2016-08-05 02:35:56 +03:00
|
|
|
|
_googleAnalyticsService.TrackAppEvent("EditedFolder");
|
2016-07-20 01:46:39 +03:00
|
|
|
|
}
|
|
|
|
|
else if(saveTask.Result.Errors.Count() > 0)
|
|
|
|
|
{
|
|
|
|
|
await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred);
|
|
|
|
|
}
|
2016-07-31 01:16:09 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _userDialogs.AlertAsync(AppResources.AnErrorHasOccurred);
|
|
|
|
|
}
|
2016-05-18 06:09:20 +03:00
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
|
|
|
|
|
|
|
|
|
Title = "Edit Folder";
|
2016-07-05 05:31:15 +03:00
|
|
|
|
Content = mainTable;
|
2016-05-18 06:09:20 +03:00
|
|
|
|
ToolbarItems.Add(saveToolBarItem);
|
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
|
|
|
|
ToolbarItems.Add(new DismissModalToolBarItem(this, "Cancel"));
|
|
|
|
|
}
|
2016-07-30 23:39:52 +03:00
|
|
|
|
}
|
2016-05-18 06:09:20 +03:00
|
|
|
|
|
2016-07-30 23:39:52 +03:00
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2016-05-18 06:09:20 +03:00
|
|
|
|
if(!_connectivity.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 06:25:40 +03:00
|
|
|
|
private async void DeleteCell_Tapped(object sender, EventArgs e)
|
|
|
|
|
{
|
2016-07-07 07:00:12 +03:00
|
|
|
|
if(!_connectivity.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 06:25:40 +03:00
|
|
|
|
// TODO: Validate the delete operation. ex. Cannot delete a folder that has sites in it?
|
|
|
|
|
|
|
|
|
|
if(!await _userDialogs.ConfirmAsync(AppResources.DoYouReallyWantToDelete, null, AppResources.Yes, AppResources.No))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var deleteTask = _folderService.DeleteAsync(_folderId);
|
|
|
|
|
_userDialogs.ShowLoading("Deleting...", MaskType.Black);
|
|
|
|
|
await deleteTask;
|
|
|
|
|
_userDialogs.HideLoading();
|
|
|
|
|
|
|
|
|
|
if((await deleteTask).Succeeded)
|
|
|
|
|
{
|
|
|
|
|
await Navigation.PopModalAsync();
|
2016-07-26 07:38:41 +03:00
|
|
|
|
_userDialogs.Toast("Folder deleted.");
|
2016-05-18 06:25:40 +03:00
|
|
|
|
}
|
2016-07-31 01:16:09 +03:00
|
|
|
|
else if((await deleteTask).Errors.Count() > 0)
|
|
|
|
|
{
|
|
|
|
|
await _userDialogs.AlertAsync((await deleteTask).Errors.First().Message, AppResources.AnErrorHasOccurred);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _userDialogs.AlertAsync(AppResources.AnErrorHasOccurred);
|
|
|
|
|
}
|
2016-05-18 06:25:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 06:09:20 +03:00
|
|
|
|
private void AlertNoConnection()
|
|
|
|
|
{
|
|
|
|
|
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage, AppResources.Ok);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|