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

165 lines
5.9 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;
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 = "Copy",
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Command = new Command(() => Copy(usernameLabel.Text, "Username"))
});
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 = "Show",
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 = "Copy",
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Command = new Command(() => Copy(password, "Password"))
});
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 = "Launch",
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Command = new Command(() => Device.OpenUri(new Uri(uri)))
});
var stackLayout = new StackLayout();
stackLayout.Children.Add(new Label { Text = "Username" });
stackLayout.Children.Add(usernameRow);
stackLayout.Children.Add(new Label { Text = "Password" });
stackLayout.Children.Add(passwordRow);
stackLayout.Children.Add(new Label { Text = "Website" });
stackLayout.Children.Add(uriRow);
if(site.Notes != null)
{
stackLayout.Children.Add(new Label { Text = "Notes" });
stackLayout.Children.Add(new Label { Text = site.Notes.Decrypt() });
}
var scrollView = new ScrollView
{
Content = stackLayout,
Orientation = ScrollOrientation.Vertical
};
Title = site.Name?.Decrypt() ?? "No Name";
Content = scrollView;
}
public void TogglePassword(Button toggleButton, Label passwordLabel, string password)
{
if(toggleButton.Text == "Show")
{
toggleButton.Text = "Hide";
passwordLabel.Text = password;
}
else
{
toggleButton.Text = "Show";
passwordLabel.Text = new string('●', password.Length);
}
}
public void Copy(string copyText, string alertLabel)
{
_clipboardService.CopyToClipboard(copyText);
_userDialogs.SuccessToast($"{alertLabel} has been copied.");
}
private class EditSiteToolBarItem : ToolbarItem
{
private readonly VaultViewSitePage _page;
private readonly string _siteId;
public EditSiteToolBarItem(VaultViewSitePage page, string siteId)
{
_page = page;
_siteId = siteId;
Text = "Edit";
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
}
}
}