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

166 lines
6.1 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
2016-05-07 05:29:03 +03:00
using Acr.UserDialogs;
using Bit.App.Abstractions;
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 : ContentPage
{
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();
}
public void Init()
{
ToolbarItems.Add(new EditSiteToolBarItem(this, _siteId));
var site = _siteService.GetByIdAsync(_siteId).GetAwaiter().GetResult();
if(site == null)
{
// TODO: handle error. navigate back? should never happen...
return;
}
var usernameRow = new StackLayout { Orientation = StackOrientation.Horizontal };
var usernameLabel = new Label
{
Text = site.Username?.Decrypt(),
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.Center,
LineBreakMode = LineBreakMode.TailTruncation
};
usernameRow.Children.Add(usernameLabel);
usernameRow.Children.Add(new Button
{
Text = AppResources.Copy,
2016-05-07 05:29:03 +03:00
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Command = new Command(() => Copy(usernameLabel.Text, AppResources.Username))
2016-05-07 05:29:03 +03:00
});
var passwordRow = new StackLayout { Orientation = StackOrientation.Horizontal };
var password = site.Password?.Decrypt();
var passwordLabel = new Label
{
Text = new string('●', password.Length),
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.Center,
LineBreakMode = LineBreakMode.TailTruncation
};
passwordRow.Children.Add(passwordLabel);
var togglePasswordButton = new Button
{
Text = AppResources.Show,
2016-05-07 05:29:03 +03:00
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Command = new Command((self) => TogglePassword(self as Button, passwordLabel, password))
};
togglePasswordButton.CommandParameter = togglePasswordButton;
passwordRow.Children.Add(togglePasswordButton);
passwordRow.Children.Add(new Button
{
Text = AppResources.Copy,
2016-05-07 05:29:03 +03:00
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Command = new Command(() => Copy(password, AppResources.Password))
2016-05-07 05:29:03 +03:00
});
var uriRow = new StackLayout { Orientation = StackOrientation.Horizontal };
var uri = site.Uri?.Decrypt();
uriRow.Children.Add(new Label
{
Text = uri,
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.Center,
LineBreakMode = LineBreakMode.TailTruncation
});
uriRow.Children.Add(new Button
{
Text = AppResources.Launch,
2016-05-07 05:29:03 +03:00
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Command = new Command(() => Device.OpenUri(new Uri(uri)))
});
var stackLayout = new StackLayout();
stackLayout.Children.Add(new Label { Text = AppResources.Username });
2016-05-07 05:29:03 +03:00
stackLayout.Children.Add(usernameRow);
stackLayout.Children.Add(new Label { Text = AppResources.Password });
2016-05-07 05:29:03 +03:00
stackLayout.Children.Add(passwordRow);
stackLayout.Children.Add(new Label { Text = AppResources.Website });
2016-05-07 05:29:03 +03:00
stackLayout.Children.Add(uriRow);
if(site.Notes != null)
{
stackLayout.Children.Add(new Label { Text = AppResources.Notes });
2016-05-07 05:29:03 +03:00
stackLayout.Children.Add(new Label { Text = site.Notes.Decrypt() });
}
var scrollView = new ScrollView
{
Content = stackLayout,
Orientation = ScrollOrientation.Vertical
};
Title = site.Name?.Decrypt() ?? AppResources.SiteNoName;
2016-05-07 05:29:03 +03:00
Content = scrollView;
}
private void TogglePassword(Button toggleButton, Label passwordLabel, string password)
2016-05-07 05:29:03 +03:00
{
if(toggleButton.Text == AppResources.Show)
2016-05-07 05:29:03 +03:00
{
toggleButton.Text = AppResources.Hide;
2016-05-07 05:29:03 +03:00
passwordLabel.Text = password;
}
else
{
toggleButton.Text = AppResources.Show;
2016-05-07 05:29:03 +03:00
passwordLabel.Text = new string('●', password.Length);
}
}
private void Copy(string copyText, string alertLabel)
2016-05-07 05:29:03 +03:00
{
_clipboardService.CopyToClipboard(copyText);
_userDialogs.SuccessToast(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)
{
await _page.Navigation.PushAsync(new VaultEditSitePage(_siteId));
}
2016-05-02 09:52:09 +03:00
}
}
}