From 15cda95c640171c48d4308e10aedd0a4e22f4e18 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 1 May 2019 15:53:56 -0400 Subject: [PATCH] hint page --- src/App/App.csproj | 3 ++ src/App/Pages/Accounts/HintPage.xaml | 37 ++++++++++++++ src/App/Pages/Accounts/HintPage.xaml.cs | 27 ++++++++++ src/App/Pages/Accounts/HintPageViewModel.cs | 56 +++++++++++++++++++++ src/App/Pages/Accounts/LoginPage.xaml | 3 +- src/App/Pages/Accounts/LoginPage.xaml.cs | 5 ++ src/Core/Services/ApiService.cs | 2 +- 7 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 src/App/Pages/Accounts/HintPage.xaml create mode 100644 src/App/Pages/Accounts/HintPage.xaml.cs create mode 100644 src/App/Pages/Accounts/HintPageViewModel.cs diff --git a/src/App/App.csproj b/src/App/App.csproj index 02479f122..93cec5d17 100644 --- a/src/App/App.csproj +++ b/src/App/App.csproj @@ -22,6 +22,9 @@ + + HintPage.xaml + LoginPage.xaml diff --git a/src/App/Pages/Accounts/HintPage.xaml b/src/App/Pages/Accounts/HintPage.xaml new file mode 100644 index 000000000..2557b4856 --- /dev/null +++ b/src/App/Pages/Accounts/HintPage.xaml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + diff --git a/src/App/Pages/Accounts/HintPage.xaml.cs b/src/App/Pages/Accounts/HintPage.xaml.cs new file mode 100644 index 000000000..3161ebe7c --- /dev/null +++ b/src/App/Pages/Accounts/HintPage.xaml.cs @@ -0,0 +1,27 @@ +using System; + +namespace Bit.App.Pages +{ + public partial class HintPage : BaseContentPage + { + private HintPageViewModel _vm; + + public HintPage() + { + InitializeComponent(); + _vm = BindingContext as HintPageViewModel; + _vm.Page = this; + } + + protected override void OnAppearing() + { + base.OnAppearing(); + RequestFocus(_email); + } + + private async void Submit_Clicked(object sender, EventArgs e) + { + await _vm.SubmitAsync(); + } + } +} diff --git a/src/App/Pages/Accounts/HintPageViewModel.cs b/src/App/Pages/Accounts/HintPageViewModel.cs new file mode 100644 index 000000000..6b833d9b6 --- /dev/null +++ b/src/App/Pages/Accounts/HintPageViewModel.cs @@ -0,0 +1,56 @@ +using Bit.App.Abstractions; +using Bit.App.Resources; +using Bit.Core.Abstractions; +using Bit.Core.Exceptions; +using Bit.Core.Utilities; +using System.Threading.Tasks; + +namespace Bit.App.Pages +{ + public class HintPageViewModel : BaseViewModel + { + private readonly IDeviceActionService _deviceActionService; + private readonly IApiService _apiService; + + public HintPageViewModel() + { + _deviceActionService = ServiceContainer.Resolve("deviceActionService"); + _apiService = ServiceContainer.Resolve("apiService"); + + PageTitle = AppResources.PasswordHint; + } + + public string Email { get; set; } + + public async Task SubmitAsync() + { + if(string.IsNullOrWhiteSpace(Email)) + { + await Page.DisplayAlert(AppResources.AnErrorHasOccurred, + string.Format(AppResources.ValidationFieldRequired, AppResources.EmailAddress), + AppResources.Ok); + return; + } + if(!Email.Contains("@")) + { + await Page.DisplayAlert(AppResources.AnErrorHasOccurred, AppResources.InvalidEmail, AppResources.Ok); + return; + } + + try + { + await _deviceActionService.ShowLoadingAsync(AppResources.Submitting); + await _apiService.PostPasswordHintAsync( + new Core.Models.Request.PasswordHintRequest { Email = Email }); + await _deviceActionService.HideLoadingAsync(); + await Page.DisplayAlert(null, AppResources.PasswordHintAlert, AppResources.Ok); + await Page.Navigation.PopModalAsync(); + } + catch(ApiException e) + { + await _deviceActionService.HideLoadingAsync(); + await Page.DisplayAlert(AppResources.AnErrorHasOccurred, e.Error.GetSingleMessage(), AppResources.Ok); + } + } + } +} diff --git a/src/App/Pages/Accounts/LoginPage.xaml b/src/App/Pages/Accounts/LoginPage.xaml index a0a8d9bea..9b8d058c9 100644 --- a/src/App/Pages/Accounts/LoginPage.xaml +++ b/src/App/Pages/Accounts/LoginPage.xaml @@ -33,6 +33,7 @@ @@ -66,7 +67,7 @@ - + diff --git a/src/App/Pages/Accounts/LoginPage.xaml.cs b/src/App/Pages/Accounts/LoginPage.xaml.cs index 67ed7af93..8f3f4730d 100644 --- a/src/App/Pages/Accounts/LoginPage.xaml.cs +++ b/src/App/Pages/Accounts/LoginPage.xaml.cs @@ -35,5 +35,10 @@ namespace Bit.App.Pages { await _vm.LogInAsync(); } + + private void Hint_Clicked(object sender, EventArgs e) + { + Navigation.PushModalAsync(new NavigationPage(new HintPage())); + } } } diff --git a/src/Core/Services/ApiService.cs b/src/Core/Services/ApiService.cs index ae73fe121..f1f29c7e9 100644 --- a/src/Core/Services/ApiService.cs +++ b/src/Core/Services/ApiService.cs @@ -345,7 +345,7 @@ namespace Bit.Core.Services var responseJsonString = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(responseJsonString); } - else if(response.IsSuccessStatusCode) + else if(!response.IsSuccessStatusCode) { var error = await HandleErrorAsync(response, false); throw new ApiException(error);