redid lock pin page with pin control.

This commit is contained in:
Kyle Spearrin 2016-06-06 18:48:52 -04:00
parent 2c19413275
commit 89e4189779
6 changed files with 96 additions and 104 deletions

View file

@ -60,6 +60,7 @@
<Compile Include="Controls\LabeledValueCell.cs" /> <Compile Include="Controls\LabeledValueCell.cs" />
<Compile Include="Controls\FormPickerCell.cs" /> <Compile Include="Controls\FormPickerCell.cs" />
<Compile Include="Controls\FormEntryCell.cs" /> <Compile Include="Controls\FormEntryCell.cs" />
<Compile Include="Controls\PinControl.cs" />
<Compile Include="Enums\ReturnType.cs" /> <Compile Include="Enums\ReturnType.cs" />
<Compile Include="Models\Api\ApiError.cs" /> <Compile Include="Models\Api\ApiError.cs" />
<Compile Include="Models\Api\ApiResult.cs" /> <Compile Include="Models\Api\ApiResult.cs" />

View file

@ -47,7 +47,6 @@ namespace Bit.App.Controls
set { SetValue(MaxLengthProperty, value); } set { SetValue(MaxLengthProperty, value); }
} }
public ReturnType? ReturnType { get; set; } public ReturnType? ReturnType { get; set; }
public bool? Autocorrect { get; set; } public bool? Autocorrect { get; set; }
public bool DisableAutocapitalize { get; set; } public bool DisableAutocapitalize { get; set; }

View file

@ -0,0 +1,45 @@
using System;
using Bit.App.Models.Page;
using Xamarin.Forms;
namespace Bit.App.Controls
{
public class PinControl
{
private Action _pinEnteredAction;
public PinControl(Action pinEnteredAction)
{
_pinEnteredAction = pinEnteredAction;
Label = new Label
{
HorizontalTextAlignment = TextAlignment.Center,
FontSize = 30,
TextColor = Color.FromHex("333333"),
FontFamily = "Courier"
};
Label.SetBinding<PinPageModel>(Label.TextProperty, s => s.LabelText);
Entry = new ExtendedEntry
{
Keyboard = Keyboard.Numeric,
IsVisible = false,
MaxLength = 4
};
Entry.SetBinding<PinPageModel>(Xamarin.Forms.Entry.TextProperty, s => s.PIN);
Entry.TextChanged += PinEntry_TextChanged;
}
private void PinEntry_TextChanged(object sender, TextChangedEventArgs e)
{
if(e.NewTextValue.Length >= 4)
{
_pinEnteredAction();
}
}
public Label Label { get; set; }
public ExtendedEntry Entry { get; set; }
}
}

View file

@ -6,33 +6,32 @@ namespace Bit.App.Models.Page
{ {
public class PinPageModel : INotifyPropertyChanged public class PinPageModel : INotifyPropertyChanged
{ {
private string _labelText; private string _pin = string.Empty;
private List<string> _pin;
public PinPageModel()
{
LabelText = "_ _ _ _";
PIN = new List<string>();
}
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
public string LabelText public string LabelText
{ {
get { return _labelText; } get
set
{ {
_labelText = value; var newText = string.Empty;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(LabelText))); for(int i = 0; i < 4; i++)
{
newText += _pin.Length <= i ? "- " : "● ";
}
return newText.TrimEnd();
} }
} }
public List<string> PIN
public string PIN
{ {
get { return _pin; } get { return _pin; }
set set
{ {
_pin = value; _pin = value;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(PIN))); PropertyChanged(this, new PropertyChangedEventArgs(nameof(PIN)));
PropertyChanged(this, new PropertyChangedEventArgs(nameof(LabelText)));
} }
} }
} }

View file

@ -8,6 +8,8 @@ using XLabs.Ioc;
using Plugin.Settings.Abstractions; using Plugin.Settings.Abstractions;
using System.Collections.Generic; using System.Collections.Generic;
using Bit.App.Models.Page; using Bit.App.Models.Page;
using Bit.App.Controls;
using System.Diagnostics;
namespace Bit.App.Pages namespace Bit.App.Pages
{ {
@ -27,43 +29,11 @@ namespace Bit.App.Pages
} }
public PinPageModel Model { get; set; } = new PinPageModel(); public PinPageModel Model { get; set; } = new PinPageModel();
public PinControl PinControl { get; set; }
public void Init() public void Init()
{ {
var label = new Label PinControl = new PinControl(PinEntered);
{
HorizontalTextAlignment = TextAlignment.Center,
FontSize = 40,
TextColor = Color.FromHex("333333")
};
label.SetBinding<PinPageModel>(Label.TextProperty, s => s.LabelText);
var grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
Action updateLabelAction = new Action(UpdateLabel);
grid.Children.Add(new PinButton("1", this, updateLabelAction), 0, 0);
grid.Children.Add(new PinButton("2", this, updateLabelAction), 1, 0);
grid.Children.Add(new PinButton("3", this, updateLabelAction), 2, 0);
grid.Children.Add(new PinButton("4", this, updateLabelAction), 0, 1);
grid.Children.Add(new PinButton("5", this, updateLabelAction), 1, 1);
grid.Children.Add(new PinButton("6", this, updateLabelAction), 2, 1);
grid.Children.Add(new PinButton("7", this, updateLabelAction), 0, 2);
grid.Children.Add(new PinButton("8", this, updateLabelAction), 1, 2);
grid.Children.Add(new PinButton("9", this, updateLabelAction), 2, 2);
grid.Children.Add(new Label(), 0, 3);
grid.Children.Add(new PinButton("0", this, updateLabelAction), 1, 3);
grid.Children.Add(new DeleteButton(this, updateLabelAction), 2, 3);
var logoutButton = new Button var logoutButton = new Button
{ {
@ -76,7 +46,7 @@ namespace Bit.App.Pages
{ {
Padding = new Thickness(30, 40), Padding = new Thickness(30, 40),
Spacing = 10, Spacing = 10,
Children = { label, grid, logoutButton } Children = { PinControl.Label, PinControl.Entry, logoutButton }
}; };
Title = "Verify PIN"; Title = "Verify PIN";
@ -87,6 +57,22 @@ namespace Bit.App.Pages
protected override void OnAppearing() protected override void OnAppearing()
{ {
base.OnAppearing(); base.OnAppearing();
PinControl.Entry.Focus();
}
protected void PinEntered()
{
if(Model.PIN == "1234")
{
PinControl.Entry.Unfocus();
Navigation.PopModalAsync();
}
else
{
_userDialogs.Alert("Invalid PIN. Try again.");
Model.PIN = string.Empty;
PinControl.Entry.Focus();
}
} }
private async Task LogoutAsync() private async Task LogoutAsync()
@ -100,60 +86,5 @@ namespace Bit.App.Pages
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
Application.Current.MainPage = new LoginNavigationPage(); Application.Current.MainPage = new LoginNavigationPage();
} }
private void UpdateLabel()
{
var newText = string.Empty;
for(int i = 0; i < 4; i++)
{
if(Model.PIN.Count <= i)
{
newText += "_ ";
}
else
{
newText += "* ";
}
}
Model.LabelText = newText.TrimEnd();
}
public class PinButton : Button
{
public PinButton(string text, LockPinPage page, Action updateLabelAction)
{
Text = text;
Command = new Command(() =>
{
if(page.Model.PIN.Count >= 4)
{
return;
}
page.Model.PIN.Add(text);
updateLabelAction();
});
CommandParameter = text;
}
}
public class DeleteButton : Button
{
public DeleteButton(LockPinPage page, Action updateLabelAction)
{
Text = "Delete";
Command = new Command(() =>
{
if(page.Model.PIN.Count == 0)
{
return;
}
page.Model.PIN.RemoveAt(page.Model.PIN.Count - 1);
updateLabelAction();
});
}
}
} }
} }

View file

@ -23,6 +23,7 @@ namespace Bit.iOS.Controls
{ {
SetBorder(view); SetBorder(view);
SetMaxLength(view); SetMaxLength(view);
UpdateKeyboard();
if(view.DisableAutocapitalize) if(view.DisableAutocapitalize)
{ {
@ -79,6 +80,10 @@ namespace Bit.iOS.Controls
{ {
SetBorder(view); SetBorder(view);
} }
else if(e.PropertyName == Xamarin.Forms.InputView.KeyboardProperty.PropertyName)
{
UpdateKeyboard();
}
} }
private void SetBorder(ExtendedEntry view) private void SetBorder(ExtendedEntry view)
@ -112,5 +117,17 @@ namespace Bit.iOS.Controls
return newLength <= view.MaxLength; return newLength <= view.MaxLength;
}; };
} }
private void UpdateKeyboard()
{
if(Element.Keyboard == Keyboard.Numeric)
{
Control.KeyboardType = UIKeyboardType.NumberPad;
}
else
{
Control.ApplyKeyboard(Element.Keyboard);
}
}
} }
} }