bitwarden-android/src/App/Pages/Settings/SettingsPinPage.cs

98 lines
3 KiB
C#
Raw Normal View History

2016-06-12 07:49:35 +03:00
using System;
using System.Threading.Tasks;
using Acr.UserDialogs;
using Bit.App.Abstractions;
using Bit.App.Resources;
using Xamarin.Forms;
using XLabs.Ioc;
using Plugin.Settings.Abstractions;
using Bit.App.Models.Page;
using Bit.App.Controls;
namespace Bit.App.Pages
{
public class SettingsPinPage : ExtendedContentPage
2016-06-12 07:49:35 +03:00
{
private readonly IUserDialogs _userDialogs;
private readonly ISettings _settings;
2017-02-18 05:18:59 +03:00
private Action<SettingsPinPage> _pinEnteredAction;
2016-06-12 07:49:35 +03:00
2017-02-18 05:18:59 +03:00
public SettingsPinPage(Action<SettingsPinPage> pinEnteredAction)
2016-06-12 07:49:35 +03:00
{
2017-02-18 05:18:59 +03:00
_pinEnteredAction = pinEnteredAction;
2016-06-12 07:49:35 +03:00
_userDialogs = Resolver.Resolve<IUserDialogs>();
_settings = Resolver.Resolve<ISettings>();
Init();
}
public PinPageModel Model { get; set; } = new PinPageModel();
public PinControl PinControl { get; set; }
2017-02-18 05:18:59 +03:00
public TapGestureRecognizer Tgr { get; set; }
2016-06-12 07:49:35 +03:00
public void Init()
{
var instructionLabel = new Label
{
Text = AppResources.SetPINDirection,
LineBreakMode = LineBreakMode.WordWrap,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
HorizontalTextAlignment = TextAlignment.Center,
Style = (Style)Application.Current.Resources["text-muted"]
};
2016-06-12 07:49:35 +03:00
PinControl = new PinControl();
PinControl.Label.SetBinding<PinPageModel>(Label.TextProperty, s => s.LabelText);
PinControl.Entry.SetBinding<PinPageModel>(Entry.TextProperty, s => s.PIN);
var stackLayout = new StackLayout
{
Padding = new Thickness(30, 40),
Spacing = 20,
Children = { PinControl.Label, instructionLabel, PinControl.Entry }
2016-06-12 07:49:35 +03:00
};
2017-02-18 05:18:59 +03:00
Tgr = new TapGestureRecognizer();
PinControl.Label.GestureRecognizers.Add(Tgr);
instructionLabel.GestureRecognizers.Add(Tgr);
2016-06-12 07:49:35 +03:00
if(Device.OS == TargetPlatform.iOS)
{
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Cancel));
}
Title = AppResources.SetPIN;
2016-06-12 07:49:35 +03:00
Content = stackLayout;
2017-02-18 05:18:59 +03:00
Content.GestureRecognizers.Add(Tgr);
2016-06-12 07:49:35 +03:00
BindingContext = Model;
}
protected override void OnAppearing()
{
base.OnAppearing();
2017-02-18 05:18:59 +03:00
Tgr.Tapped += Tgr_Tapped;
PinControl.OnPinEntered += PinEntered;
PinControl.InitEvents();
PinControl.Entry.FocusWithDelay();
2016-06-12 07:49:35 +03:00
}
protected override void OnDisappearing()
{
base.OnDisappearing();
PinControl.Dispose();
2017-02-18 05:18:59 +03:00
Tgr.Tapped -= Tgr_Tapped;
PinControl.OnPinEntered -= PinEntered;
}
2016-06-12 07:49:35 +03:00
protected void PinEntered(object sender, EventArgs args)
{
2017-02-18 05:18:59 +03:00
_pinEnteredAction?.Invoke(this);
}
private void Tgr_Tapped(object sender, EventArgs e)
{
PinControl.Entry.Focus();
2016-06-12 07:49:35 +03:00
}
}
}