diff --git a/PolyChat/MainPage.xaml b/PolyChat/MainPage.xaml
index 8439a3e..c650b15 100644
--- a/PolyChat/MainPage.xaml
+++ b/PolyChat/MainPage.xaml
@@ -20,22 +20,30 @@
-
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs
index b0b8e00..e90250f 100644
--- a/PolyChat/MainPage.xaml.cs
+++ b/PolyChat/MainPage.xaml.cs
@@ -1,4 +1,5 @@
using PolyChat.Models;
+using PolyChat.Util;
using PolyChat.Views;
using System;
using System.Collections.ObjectModel;
@@ -19,12 +20,13 @@ namespace PolyChat
private Controller Controller;
private ObservableCollection Partners;
private ChatPartner selectedPartner;
+ private string username;
public MainPage()
{
this.InitializeComponent();
Controller = new Controller(this);
-
Partners = new ObservableCollection();
+ //ipAddress.Text = IP.GetCodeFromIP(Controller.GetIP());
}
public void OnChatPartnerSelected(object sender, RoutedEventArgs e)
@@ -42,7 +44,7 @@ namespace PolyChat
DateTime.Now.ToString(),
false
));
- Controller.sendMessage(selectedPartner.Code, inputUsername.Text, inputSend.Text);
+ Controller.sendMessage(selectedPartner.Code, username, inputSend.Text);
// clear input
inputSend.Text = "";
}
@@ -53,12 +55,27 @@ namespace PolyChat
var result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
- string ip = dialog.getText();
- Controller.Connect(ip);
- Partners.Add(new ChatPartner(
- "NO NAME",
- ip
- ));
+ string ip = dialog.getValue();
+ if (IP.ValidateIP(ip))
+ {
+ Controller.Connect(ip);
+ Partners.Add(new ChatPartner(
+ "Connecting...",
+ ip
+ ));
+ }
+ }
+ }
+
+ public async void OnOpenEditUsernameDialog(object sender = null, RoutedEventArgs e = null)
+ {
+ EditUsernameDialog dialog = new EditUsernameDialog(username);
+ var result = await dialog.ShowAsync();
+ if (result == ContentDialogResult.Primary)
+ {
+ username = dialog.getValue();
+ if (username.Length == 0) textUsername.Text = "Unknown";
+ else textUsername.Text = username;
}
}
diff --git a/PolyChat/PolyChat.csproj b/PolyChat/PolyChat.csproj
index 6119e13..a820cb1 100644
--- a/PolyChat/PolyChat.csproj
+++ b/PolyChat/PolyChat.csproj
@@ -132,6 +132,10 @@
+
+
+ EditUsernameDialog.xaml
+
NewChatDialog.xaml
@@ -160,6 +164,10 @@
MSBuild:Compile
Designer
+
+ MSBuild:Compile
+ Designer
+
MSBuild:Compile
Designer
diff --git a/PolyChat/Util/IP.cs b/PolyChat/Util/IP.cs
new file mode 100644
index 0000000..fcb6fd6
--- /dev/null
+++ b/PolyChat/Util/IP.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Text.RegularExpressions;
+
+namespace PolyChat.Util
+{
+ static class IP
+ {
+ private const string REGEX_IP = @"^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\.(?!$)|$)){4}$";
+
+ public static string GetIPfromCode(string code)
+ {
+ return code;
+ }
+
+ public static string GetCodeFromIP(string ip)
+ {
+ string[] arr = ip.Split('.');
+ for (int i = 0; i < arr.Length; i++)
+ {
+ arr[i] = int.Parse(arr[i]).ToString("X");
+ Console.WriteLine(arr[i]);
+ }
+ return ip;
+ }
+
+ public static bool ValidateIP(string ip)
+ {
+ return Regex.IsMatch(ip, REGEX_IP);
+ }
+ }
+}
diff --git a/PolyChat/Views/EditUsernameDialog.xaml b/PolyChat/Views/EditUsernameDialog.xaml
new file mode 100644
index 0000000..7bfba27
--- /dev/null
+++ b/PolyChat/Views/EditUsernameDialog.xaml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/PolyChat/Views/EditUsernameDialog.xaml.cs b/PolyChat/Views/EditUsernameDialog.xaml.cs
new file mode 100644
index 0000000..faec438
--- /dev/null
+++ b/PolyChat/Views/EditUsernameDialog.xaml.cs
@@ -0,0 +1,51 @@
+using PolyChat.Models;
+using PolyChat.Util;
+using System;
+using System.Collections.ObjectModel;
+using System.Linq;
+using Windows.UI.Xaml;
+using Windows.UI.Xaml.Controls;
+using Windows.UI.Xaml.Media;
+
+// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
+
+namespace PolyChat.Views
+{
+ ///
+ /// An empty page that can be used on its own or navigated to within a Frame.
+ ///
+ public sealed partial class EditUsernameDialog : ContentDialog
+ {
+ public EditUsernameDialog(string initialValue)
+ {
+ this.InitializeComponent();
+ if (initialValue == null || initialValue.Length == 0) IsSecondaryButtonEnabled = false;
+ else input.Text = initialValue;
+ validate();
+ }
+
+ public string getValue()
+ {
+ return input.Text;
+ }
+
+ private void OnKeyUp(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
+ {
+ validate();
+ }
+
+ private void validate()
+ {
+ if (input.Text.Length == 0)
+ {
+ textError.Visibility = Visibility.Visible;
+ IsPrimaryButtonEnabled = false;
+ }
+ else
+ {
+ textError.Visibility = Visibility.Collapsed;
+ IsPrimaryButtonEnabled = true;
+ }
+ }
+ }
+}
diff --git a/PolyChat/Views/NewChatDialog.xaml b/PolyChat/Views/NewChatDialog.xaml
index 6470c43..bfa8119 100644
--- a/PolyChat/Views/NewChatDialog.xaml
+++ b/PolyChat/Views/NewChatDialog.xaml
@@ -6,13 +6,17 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="ContentDialog"
Title="Connect to"
- PrimaryButtonClick="OnConnect"
PrimaryButtonText="Connect"
- SecondaryButtonClick="OnClose"
SecondaryButtonText="Cancel"
mc:Ignorable="d">
-
+
+
+
+
+
+
+
diff --git a/PolyChat/Views/NewChatDialog.xaml.cs b/PolyChat/Views/NewChatDialog.xaml.cs
index 56a0489..600c62f 100644
--- a/PolyChat/Views/NewChatDialog.xaml.cs
+++ b/PolyChat/Views/NewChatDialog.xaml.cs
@@ -1,9 +1,11 @@
using PolyChat.Models;
+using PolyChat.Util;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
+using Windows.UI.Xaml.Media;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
@@ -17,19 +19,27 @@ namespace PolyChat.Views
public NewChatDialog()
{
this.InitializeComponent();
+ IsPrimaryButtonEnabled = false;
}
- public string getText()
+ public string getValue()
{
- return inputIP.Text;
+ return input.Text;
}
- public void OnConnect(ContentDialog sender, ContentDialogButtonClickEventArgs args)
- {
- }
-
- public void OnClose(ContentDialog sender, ContentDialogButtonClickEventArgs args)
+ private void OnKeyUp(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
+ if (!IP.ValidateIP(IP.GetIPfromCode(input.Text)))
+ {
+ textSuccess.Visibility = Visibility.Collapsed;
+ textError.Visibility = Visibility.Visible;
+ IsPrimaryButtonEnabled = false;
+ } else
+ {
+ textSuccess.Visibility = Visibility.Visible;
+ textError.Visibility = Visibility.Collapsed;
+ IsPrimaryButtonEnabled = true;
+ }
}
}
}