bitwarden-android/src/App/Pages/Vault/VaultViewSitePage.cs

199 lines
7.3 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
2016-05-07 05:29:03 +03:00
using Acr.UserDialogs;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Bit.App.Models.Page;
using Bit.App.Resources;
2016-05-02 09:52:09 +03:00
using Xamarin.Forms;
2016-05-07 05:29:03 +03:00
using XLabs.Ioc;
2016-05-02 09:52:09 +03:00
2016-05-03 00:50:16 +03:00
namespace Bit.App.Pages
2016-05-02 09:52:09 +03:00
{
public class VaultViewSitePage : ExtendedContentPage
2016-05-02 09:52:09 +03:00
{
2016-05-07 05:29:03 +03:00
private readonly string _siteId;
private readonly ISiteService _siteService;
private readonly IUserDialogs _userDialogs;
private readonly IClipboardService _clipboardService;
2016-05-02 09:52:09 +03:00
2016-05-07 05:29:03 +03:00
public VaultViewSitePage(string siteId)
2016-05-02 09:52:09 +03:00
{
_siteId = siteId;
2016-05-07 05:29:03 +03:00
_siteService = Resolver.Resolve<ISiteService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
_clipboardService = Resolver.Resolve<IClipboardService>();
Init();
}
private VaultViewSitePageModel Model { get; set; } = new VaultViewSitePageModel();
private ExtendedTableView Table { get; set; }
private TableSection SiteInformationSection { get; set; }
private TableSection NotesSection { get; set; }
public LabeledValueCell UsernameCell { get; set; }
public LabeledValueCell PasswordCell { get; set; }
public LabeledValueCell UriCell { get; set; }
private void Init()
2016-05-07 05:29:03 +03:00
{
ToolbarItems.Add(new EditSiteToolBarItem(this, _siteId));
if(Device.OS == TargetPlatform.iOS)
{
ToolbarItems.Add(new DismissModalToolBarItem(this));
}
2016-05-07 05:29:03 +03:00
2016-06-18 01:14:24 +03:00
// Name
var nameCell = new LabeledValueCell(AppResources.Name);
nameCell.Value.SetBinding<VaultViewSitePageModel>(Label.TextProperty, s => s.Name);
// Username
UsernameCell = new LabeledValueCell(AppResources.Username, button1Text: AppResources.Copy);
UsernameCell.Value.SetBinding<VaultViewSitePageModel>(Label.TextProperty, s => s.Username);
UsernameCell.Value.SetBinding<VaultViewSitePageModel>(Label.FontSizeProperty, s => s.UsernameFontSize);
UsernameCell.Button1.Command = new Command(() => Copy(Model.Username, AppResources.Username));
2016-05-07 05:29:03 +03:00
// Password
PasswordCell = new LabeledValueCell(AppResources.Password, button1Text: string.Empty,
button2Text: AppResources.Copy);
PasswordCell.Value.SetBinding<VaultViewSitePageModel>(Label.TextProperty, s => s.MaskedPassword);
PasswordCell.Value.SetBinding<VaultViewSitePageModel>(Label.FontSizeProperty, s => s.PasswordFontSize);
PasswordCell.Button1.SetBinding<VaultViewSitePageModel>(Button.ImageProperty, s => s.ShowHideImage);
2016-06-18 01:14:24 +03:00
if(Device.OS == TargetPlatform.iOS)
{
PasswordCell.Button1.Margin = new Thickness(10, 0);
}
PasswordCell.Button1.Command = new Command(() => Model.RevealPassword = !Model.RevealPassword);
PasswordCell.Button2.Command = new Command(() => Copy(Model.Password, AppResources.Password));
PasswordCell.Value.FontFamily = Device.OnPlatform(iOS: "Courier", Android: "monospace", WinPhone: "Courier");
2016-05-07 05:29:03 +03:00
// URI
UriCell = new LabeledValueCell(AppResources.Website, button1Text: AppResources.Launch);
UriCell.Value.SetBinding<VaultViewSitePageModel>(Label.TextProperty, s => s.UriHost);
UriCell.Button1.SetBinding<VaultViewSitePageModel>(IsVisibleProperty, s => s.ShowLaunch);
UriCell.Button1.Command = new Command(() => Device.OpenUri(new Uri(Model.Uri)));
// Notes
var notesCell = new LabeledValueCell();
notesCell.Value.SetBinding<VaultViewSitePageModel>(Label.TextProperty, s => s.Notes);
2016-05-24 06:48:34 +03:00
notesCell.Value.LineBreakMode = LineBreakMode.WordWrap;
2016-11-26 00:42:52 +03:00
SiteInformationSection = new TableSection(AppResources.SiteInformation)
{
nameCell
};
NotesSection = new TableSection(AppResources.Notes)
{
notesCell
};
Table = new ExtendedTableView
2016-05-07 05:29:03 +03:00
{
Intent = TableIntent.Settings,
2016-05-24 06:48:34 +03:00
EnableScrolling = true,
HasUnevenRows = true,
2016-05-17 06:54:24 +03:00
EnableSelection = false,
Root = new TableRoot
{
SiteInformationSection,
NotesSection
}
};
if(Device.OS == TargetPlatform.iOS)
{
Table.RowHeight = -1;
Table.EstimatedRowHeight = 70;
}
else if(Device.OS == TargetPlatform.Android)
{
// NOTE: This is going to cause problems with i18n strings since various languages have difference string sizes
PasswordCell.Button1.WidthRequest = 40;
PasswordCell.Button2.WidthRequest = 55;
UsernameCell.Button1.WidthRequest = 55;
2016-12-07 06:31:07 +03:00
UriCell.Button1.WidthRequest = 75;
}
2016-05-07 05:29:03 +03:00
2016-11-26 00:42:52 +03:00
Title = AppResources.ViewSite;
2016-05-24 06:48:34 +03:00
Content = Table;
BindingContext = Model;
2016-05-07 05:29:03 +03:00
}
protected async override void OnAppearing()
2016-05-07 05:29:03 +03:00
{
var site = await _siteService.GetByIdAsync(_siteId);
if(site == null)
2016-05-07 05:29:03 +03:00
{
await Navigation.PopForDeviceAsync();
return;
2016-05-07 05:29:03 +03:00
}
Model.Update(site);
2016-05-12 00:30:09 +03:00
if(!Model.ShowUri)
{
SiteInformationSection.Remove(UriCell);
}
else if(!SiteInformationSection.Contains(UriCell))
{
SiteInformationSection.Add(UriCell);
}
if(!Model.ShowUsername)
{
SiteInformationSection.Remove(UsernameCell);
}
else if(!SiteInformationSection.Contains(UsernameCell))
{
SiteInformationSection.Add(UsernameCell);
}
if(!Model.ShowPassword)
{
SiteInformationSection.Remove(PasswordCell);
}
else if(!SiteInformationSection.Contains(PasswordCell))
{
SiteInformationSection.Add(PasswordCell);
}
if(!Model.ShowNotes)
{
Table.Root.Remove(NotesSection);
}
else if(!Table.Root.Contains(NotesSection))
{
Table.Root.Add(NotesSection);
}
base.OnAppearing();
2016-05-07 05:29:03 +03:00
}
private void Copy(string copyText, string alertLabel)
2016-05-07 05:29:03 +03:00
{
_clipboardService.CopyToClipboard(copyText);
_userDialogs.Toast(string.Format(AppResources.ValueHasBeenCopied, alertLabel));
2016-05-07 05:29:03 +03:00
}
private class EditSiteToolBarItem : ToolbarItem
{
private readonly VaultViewSitePage _page;
private readonly string _siteId;
public EditSiteToolBarItem(VaultViewSitePage page, string siteId)
{
_page = page;
_siteId = siteId;
Text = AppResources.Edit;
2016-05-07 05:29:03 +03:00
Clicked += ClickedItem;
}
2016-05-02 09:52:09 +03:00
2016-05-07 05:29:03 +03:00
private async void ClickedItem(object sender, EventArgs e)
{
var page = new VaultEditSitePage(_siteId);
await _page.Navigation.PushForDeviceAsync(page);
2016-05-07 05:29:03 +03:00
}
2016-05-02 09:52:09 +03:00
}
}
}