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

163 lines
6.4 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.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 : 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();
}
private VaultViewSitePageModel Model { get; set; } = new VaultViewSitePageModel();
private void Init()
2016-05-07 05:29:03 +03:00
{
ToolbarItems.Add(new EditSiteToolBarItem(this, _siteId));
var stackLayout = new StackLayout();
2016-05-07 05:29:03 +03:00
// Username
2016-05-07 05:29:03 +03:00
var usernameRow = new StackLayout { Orientation = StackOrientation.Horizontal };
var usernameLabel = new Label
{
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.Center,
LineBreakMode = LineBreakMode.TailTruncation
};
usernameLabel.SetBinding<VaultViewSitePageModel>(Label.TextProperty, s => s.Username);
2016-05-07 05:29:03 +03:00
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
});
stackLayout.Children.Add(new Label { Text = AppResources.Username });
stackLayout.Children.Add(usernameRow);
2016-05-07 05:29:03 +03:00
// Password
2016-05-07 05:29:03 +03:00
var passwordRow = new StackLayout { Orientation = StackOrientation.Horizontal };
var passwordLabel = new Label
{
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.Center,
LineBreakMode = LineBreakMode.TailTruncation
};
passwordLabel.SetBinding<VaultViewSitePageModel>(Label.TextProperty, s => s.MaskedPassword);
2016-05-07 05:29:03 +03:00
passwordRow.Children.Add(passwordLabel);
var togglePasswordButton = new Button
{
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Command = new Command(() => Model.ShowPassword = !Model.ShowPassword)
2016-05-07 05:29:03 +03:00
};
togglePasswordButton.CommandParameter = togglePasswordButton;
togglePasswordButton.SetBinding<VaultViewSitePageModel>(Button.TextProperty, s => s.ShowHideText);
2016-05-07 05:29:03 +03:00
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(Model.Password, AppResources.Password))
2016-05-07 05:29:03 +03:00
});
stackLayout.Children.Add(new Label { Text = AppResources.Password });
stackLayout.Children.Add(passwordRow);
2016-05-07 05:29:03 +03:00
// URI
2016-05-07 05:29:03 +03:00
var uriRow = new StackLayout { Orientation = StackOrientation.Horizontal };
var uriLabel = new Label
2016-05-07 05:29:03 +03:00
{
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.Center,
LineBreakMode = LineBreakMode.TailTruncation
};
uriLabel.SetBinding<VaultViewSitePageModel>(Label.TextProperty, s => s.Uri);
uriRow.Children.Add(uriLabel);
2016-05-07 05:29:03 +03:00
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(uriLabel.Text)))
2016-05-07 05:29:03 +03:00
});
stackLayout.Children.Add(new Label { Text = AppResources.Website });
2016-05-07 05:29:03 +03:00
stackLayout.Children.Add(uriRow);
// Notes
var notes = new Label { Text = AppResources.Notes };
notes.SetBinding<VaultViewSitePageModel>(Label.IsVisibleProperty, s => s.ShowNotes);
stackLayout.Children.Add(notes);
var notesLabel = new Label();
notesLabel.SetBinding<VaultViewSitePageModel>(Label.TextProperty, s => s.Notes);
notesLabel.SetBinding<VaultViewSitePageModel>(Label.IsVisibleProperty, s => s.ShowNotes);
stackLayout.Children.Add(notesLabel);
2016-05-07 05:29:03 +03:00
var scrollView = new ScrollView
{
Content = stackLayout,
Orientation = ScrollOrientation.Vertical
};
SetBinding(Page.TitleProperty, new Binding("PageTitle"));
2016-05-07 05:29:03 +03:00
Content = scrollView;
BindingContext = Model;
NavigationPage.SetBackButtonTitle(this, AppResources.Back);
2016-05-07 05:29:03 +03:00
}
protected override void OnAppearing()
2016-05-07 05:29:03 +03:00
{
var site = _siteService.GetByIdAsync(_siteId).GetAwaiter().GetResult();
if(site == null)
2016-05-07 05:29:03 +03:00
{
// TODO: handle error. navigate back? should never happen...
return;
2016-05-07 05:29:03 +03:00
}
Model.Update(site);
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.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
}
}
}