diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 5088c48..5645822 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -80,8 +80,7 @@ namespace PolyChat Debug.WriteLine("--- Controller.OnMessage ---"); if (data != null && data.Length > 0 && data[0] != null) { - Debug.WriteLine("Message: " + data[0]); - Debug.WriteLine($"RAW: {data[0].ToString()}"); + Debug.WriteLine("RAW: " + data[0]); UIController.OnIncomingMessage(ip, data[0].ToString()); } else Debug.WriteLine("Undefined: " + data); @@ -92,8 +91,8 @@ namespace PolyChat Connections[IP].Close(); Connections.Remove(IP); UIController.OnChatPartnerDeleted(IP); - if(!wasConnected) - UIController.ShowConnectionError("Connection close", IP, $"Connection to {IP} failed..."); + string heading = wasConnected ? "Connection Closed" : "Connection Failed"; + UIController.ShowConnectionError(IP, heading, $"Connecting to {IP} failed..."); } public string getIP() diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index 08b3b2e..cfa30b5 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -1,8 +1,10 @@ -using PolyChat.Models; +using Newtonsoft.Json.Linq; +using PolyChat.Models; using PolyChat.Util; using PolyChat.Views; using System; using System.Collections.ObjectModel; +using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Windows.UI.Core; @@ -36,7 +38,7 @@ namespace PolyChat updateSendButtonEnabled(); } - public async void ShowConnectionError(string code, string heading, string message) + public async void ShowConnectionError(string param, string heading, string message) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { @@ -48,10 +50,10 @@ namespace PolyChat "Retry", () => { - Controller.Connect(code); + Controller.Connect(param); Partners.Add(new ChatPartner( "Connecting...", - code + param )); updateNoChatsPlaceholder(); } @@ -122,10 +124,32 @@ namespace PolyChat /// Adds an message to the UI, based on .sender if known /// /// ChatMessage - public void OnIncomingMessage(string origin, string json) + public async void OnIncomingMessage(string origin, string json) { - ChatPartner sendingPartner = Partners.FirstOrDefault(p => p.Code == origin); - sendingPartner.AddMessage(new ChatMessage(origin, json)); + await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => + { + ChatPartner sendingPartner = Partners.FirstOrDefault(p => p.Code == origin); + sendingPartner.AddMessage(new ChatMessage(origin, json)); + }); + } + public async void OnIncomingMessages(string origin, string json) + { + await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => + { + ChatPartner sendingPartner = Partners.FirstOrDefault(p => p.Code == origin); + JArray arr = JArray.Parse(json); + foreach (JObject item in arr) + { + sendingPartner.AddMessage( + new ChatMessage( + origin, + item["type"].ToString(), + item["content"].ToString()//, + //DateTime.Parse(item["timestamp"].ToString()) + ) + ); + } + }); } private void OnDeleteChat(object sender = null, RoutedEventArgs e = null) diff --git a/PolyChat/Models/ChatMessage.cs b/PolyChat/Models/ChatMessage.cs index 30b3ea2..9fd22c2 100644 --- a/PolyChat/Models/ChatMessage.cs +++ b/PolyChat/Models/ChatMessage.cs @@ -11,7 +11,13 @@ namespace PolyChat.Models public string Content; public DateTime TimeStamp; public readonly bool Foreign; - // + + /// + /// Create own Message (directly sent) + /// + /// My IP + /// Message Type + /// Message Content (not JSON) public ChatMessage(string origin, string type, string content) { Origin = origin; @@ -23,6 +29,28 @@ namespace PolyChat.Models Debug.WriteLine("Created Message: " + ToString()); } + /// + /// Create Message loaded with timestamp + /// + /// Origin IP + /// Message Type + /// Message Content (not JSON) + /// Message Content (not JSON) + public ChatMessage(string origin, string type, string content, DateTime timeStamp, bool foreign = false) + { + Origin = origin; + TimeStamp = timeStamp; + Type = type; + Content = content; + Foreign = foreign; + Debug.WriteLine("Created Loaded Message: " + ToString()); + } + + /// + /// Create foreign Message (directly incoming) + /// + /// Foreign IP + /// Message Content as JSON with type and content public ChatMessage(string origin, string json) { Origin = origin;