This commit is contained in:
Bartosz Chyży 2020-06-24 21:15:13 +02:00
parent ea202d0a8e
commit 82e9a7d1ee
3 changed files with 16 additions and 6 deletions

View File

@ -15,12 +15,10 @@ namespace SafeMessageStorage.Pages
public partial class ChangePasswordPage : ContentPage
{
private ChangePasswordViewModel _viewModel => this.BindingContext as ChangePasswordViewModel;
private IMessageStorageService _messageStorage;
public ChangePasswordPage(IAuthorizationService authorizationService, IMessageStorageService messageStorage)
{
InitializeComponent();
BindingContext = new ChangePasswordViewModel(authorizationService);
_messageStorage = messageStorage;
BindingContext = new ChangePasswordViewModel(authorizationService,messageStorage);
_viewModel.PasswordSetFailed += _viewModel_PasswordSetFailed;
_viewModel.PasswordSetSucceeded += _viewModel_PasswordSetSucceeded;
@ -29,7 +27,6 @@ namespace SafeMessageStorage.Pages
private async void _viewModel_PasswordSetSucceeded(object sender, EventArgs e)
{
await _messageStorage.DeleteAllMessagesAsync();
await Navigation.PopAsync();
}

View File

@ -62,7 +62,7 @@ namespace SafeMessageStorage.Services.MessageStorageService
public async Task<bool> DeleteAllMessagesAsync()
{
CheckIfAuthorized();
//CheckIfAuthorized();
return Xamarin.Essentials.SecureStorage.Remove(_messagesKey);
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using SafeMessageStorage.Services.AuthorizationService;
using SafeMessageStorage.Services.MessageStorageService;
using Xamarin.Forms;
namespace SafeMessageStorage.ViewModels
@ -10,10 +11,12 @@ namespace SafeMessageStorage.ViewModels
public class ChangePasswordViewModel : ViewModelBase
{
private IAuthorizationService _authorizationService;
private IMessageStorageService _messageStorage;
public ChangePasswordViewModel(IAuthorizationService authorizationService)
public ChangePasswordViewModel(IAuthorizationService authorizationService, IMessageStorageService messageStorage)
{
_authorizationService = authorizationService;
_messageStorage = messageStorage;
}
private string _currentPassword;
@ -45,6 +48,7 @@ namespace SafeMessageStorage.ViewModels
private async void SetPassword(string currentPassword, string password, string repeatPassword)
{
var oldData = await _messageStorage.ReadMessagesAsync();
if(!await _authorizationService.AuthorizeAsync(currentPassword))
PasswordSetFailed?.Invoke(this, "Current password doesn't match");
@ -55,7 +59,16 @@ namespace SafeMessageStorage.ViewModels
else
{
if (await _authorizationService.ChangePasswordAsync(currentPassword, password))
{
await _messageStorage.DeleteAllMessagesAsync();
foreach (var message in oldData)
{
await _messageStorage.SaveMessageAsync(message);
}
PasswordSetSucceeded?.Invoke(this, EventArgs.Empty);
}
}
}
}