Add/Remove mock

This commit is contained in:
Bartosz Chyży 2020-06-21 01:06:48 +02:00
parent 390fd66d97
commit d6a6b58a9d
11 changed files with 232 additions and 16 deletions

View File

@ -22,14 +22,11 @@ namespace SafeMessageStorage.Encryption.Hash
public virtual string GetHashString(byte[] bytes)
{
using (_hashAlgorithm)
{
return GetHashBytes(bytes)
.Select(b =>
b.ToString("x2"))
.Aggregate((a, b) =>
a + b);
}
return GetHashBytes(bytes)
.Select(b =>
b.ToString("x2"))
.Aggregate((a, b) =>
a + b);
}
public virtual byte[] GetHashBytes(string input)
@ -39,10 +36,7 @@ namespace SafeMessageStorage.Encryption.Hash
public virtual byte[] GetHashBytes(byte[] bytes)
{
using (_hashAlgorithm)
{
return _hashAlgorithm.ComputeHash(bytes);
}
return _hashAlgorithm.ComputeHash(bytes);
}
}
}

View File

@ -6,13 +6,21 @@ namespace SafeMessageStorage.Models
{
public class Message
{
public Message(string title, string content, DateTimeOffset? timeOfCreation = null)
public Message(string title, string content, DateTimeOffset? timeOfCreation = null, string id=null)
{
Id = id ?? Guid.NewGuid().ToString();
Title = title;
Content = content;
TimeOfCreation = timeOfCreation ?? DateTimeOffset.Now;
}
public Message()
{
}
public string Id { get; }
public string Title { get; }
public string Content { get; }

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="SafeMessageStorage.Pages.AddMessagePage"
Title="Add message">
<ContentPage.Content>
<StackLayout>
<StackLayout Padding="40" VerticalOptions="CenterAndExpand">
<Label Text="Title"/>
<Entry x:Name="TitleEntry"></Entry>
<Label Text="Message" Margin="0,20,0,0"/>
<Editor x:Name="ContentEntry" HeightRequest="200" BackgroundColor="#f4f4f4"/>
</StackLayout>
<Button Clicked="Button_OnClicked" Text="Add message to store" VerticalOptions="End"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SafeMessageStorage.Models;
using SafeMessageStorage.Services.MessageStorageService;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace SafeMessageStorage.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AddMessagePage : ContentPage
{
//private IMessageStorageService _messageStorage;
public AddMessagePage(/*IMessageStorageService messageStorageService*/)
{
InitializeComponent();
}
private async void Button_OnClicked(object sender, EventArgs e)
{
var message = new Message(TitleEntry.Text,ContentEntry.Text);
//if(await _messageStorage.SaveMessageAsync(message))
MessageAdded?.Invoke(this,message);
await Navigation.PopAsync();
}
public event EventHandler<Message> MessageAdded;
}
}

View File

@ -36,9 +36,9 @@ namespace SafeMessageStorage.Pages
_viewModel.Initialize();
}
private void _viewModel_AuthorizationSucceeded(object sender, EventArgs e)
private async void _viewModel_AuthorizationSucceeded(object sender, EventArgs e)
{
await Navigation.PushAsync(new MessagesListPage());
}
private async void _viewModel_AuthorizationFailed(object sender, string e)

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="SafeMessageStorage.Pages.MessageDetailPage">
<ContentPage.Content>
<StackLayout Padding="40">
<Label x:Name="MessageContent" LineBreakMode="WordWrap"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SafeMessageStorage.Models;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace SafeMessageStorage.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MessageDetailPage : ContentPage
{
private string _messageId;
public MessageDetailPage(Message message)
{
InitializeComponent();
_messageId = message.Id;
Title = message.Title;
MessageContent.Text = message.Content;
ToolbarItems.Add(new ToolbarItem("Delete", "", () => DeleteMessage()));
}
private async void DeleteMessage()
{
MessageDeleted?.Invoke(this,_messageId);
await Navigation.PopAsync();
}
public event EventHandler<string> MessageDeleted;
}
}

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="SafeMessageStorage.Pages.MessagesListPage">
<StackLayout>
<ListView x:Name="MyListView"
ItemsSource="{Binding Messages}"
ItemTapped="Handle_ItemTapped"
CachingStrategy="RecycleElement">
<!--Built in Cells-->
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Title}" />
</DataTemplate>
</ListView.ItemTemplate>
<!--Custom View Cells-->
<!--
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Text}"
d:Text="{Binding .}"
Style="{DynamicResource ListItemTextStyle}" />
<Label Text="{Binding Detail}"
d:Text="Detail"
Style="{DynamicResource ListItemDetailTextStyle}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
-->
</ListView>
<Button Text="AddMessage" Clicked="Button_OnClicked"></Button>
</StackLayout>
</ContentPage>

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using SafeMessageStorage.Models;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace SafeMessageStorage.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MessagesListPage : ContentPage
{
public ObservableCollection<Message> Messages { get; set; }
public MessagesListPage()
{
InitializeComponent();
Messages = new ObservableCollection<Message>()
{
new Message("Siemano","jakas wiadomosc"),
new Message("Kanapka","elo")
};
BindingContext = this;
}
async void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item == null)
return;
var message = ((ListView) sender).SelectedItem as Message;
var detailPage = new MessageDetailPage(message);
detailPage.MessageDeleted += (sender, messageId) => OnMessageDeleted(messageId);
await Navigation.PushAsync(detailPage);
////Deselect Item
((ListView)sender).SelectedItem = null;
}
private async void Button_OnClicked(object sender, EventArgs e)
{
var addMessagePage = new AddMessagePage();
addMessagePage.MessageAdded+= (o, message) => Dispatcher.BeginInvokeOnMainThread(()=>Messages.Add(message));
await Navigation.PushAsync(addMessagePage);
}
private void OnMessageDeleted(string messageId)
{
var toRemove = Messages.FirstOrDefault(m => m.Id == messageId);
Messages.Remove(toRemove);
}
}
}

View File

@ -16,9 +16,18 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Pages\AddMessagePage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Pages\AuthorizationPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Pages\MessageDetailPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Pages\MessagesListPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Pages\SetPasswordPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using SafeMessageStorage.Encryption.Hash;
using Xamarin.Forms;
namespace SafeMessageStorage.Services.AuthorizationService
{
@ -23,8 +24,11 @@ namespace SafeMessageStorage.Services.AuthorizationService
public bool IsPasswordSet => Xamarin.Essentials.SecureStorage.GetAsync(_passwordKey).Result != null;
public async Task<bool> AuthorizeAsync(string password)
{
if (string.IsNullOrWhiteSpace(password))
return false;
var keyChainPassword = await Xamarin.Essentials.SecureStorage.GetAsync(_passwordKey);
var result = keyChainPassword?.Equals(_hashProvider.GetHashString(password)) ?? false;
var hash = _hashProvider.GetHashString(password);
var result = keyChainPassword?.Equals(hash) ?? false;
IsAuthorized = result;
AuthorizedUserHash = IsAuthorized ? keyChainPassword : null;
return result;