Added OnIncomingMessages (for loading messages from json array)

This commit is contained in:
Patrick Hellebrand
2021-09-23 10:21:55 +02:00
parent 43b78014ca
commit 69d5327c75
3 changed files with 63 additions and 12 deletions

View File

@@ -82,8 +82,7 @@ namespace PolyChat
Debug.WriteLine("--- Controller.OnMessage ---"); Debug.WriteLine("--- Controller.OnMessage ---");
if (data != null && data.Length > 0 && data[0] != null) if (data != null && data.Length > 0 && data[0] != null)
{ {
Debug.WriteLine("Message: " + data[0]); Debug.WriteLine("RAW: " + data[0]);
Debug.WriteLine($"RAW: {data[0].ToString()}");
UIController.OnIncomingMessage(ip, data[0].ToString()); UIController.OnIncomingMessage(ip, data[0].ToString());
} }
else Debug.WriteLine("Undefined: " + data); else Debug.WriteLine("Undefined: " + data);
@@ -94,8 +93,8 @@ namespace PolyChat
Connections[IP].Close(); Connections[IP].Close();
Connections.Remove(IP); Connections.Remove(IP);
UIController.OnChatPartnerDeleted(IP); UIController.OnChatPartnerDeleted(IP);
if(!wasConnected) string heading = wasConnected ? "Connection Closed" : "Connection Failed";
UIController.ShowConnectionError("Connection close", IP, $"Connection to {IP} failed..."); UIController.ShowConnectionError(IP, heading, $"Connecting to {IP} failed...");
} }
public string getIP() public string getIP()

View File

@@ -1,8 +1,10 @@
using PolyChat.Models; using Newtonsoft.Json.Linq;
using PolyChat.Models;
using PolyChat.Util; using PolyChat.Util;
using PolyChat.Views; using PolyChat.Views;
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.UI.Core; using Windows.UI.Core;
@@ -36,7 +38,7 @@ namespace PolyChat
updateSendButtonEnabled(); 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, () => await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{ {
@@ -48,10 +50,10 @@ namespace PolyChat
"Retry", "Retry",
() => () =>
{ {
Controller.Connect(code); Controller.Connect(param);
Partners.Add(new ChatPartner( Partners.Add(new ChatPartner(
"Connecting...", "Connecting...",
code param
)); ));
updateNoChatsPlaceholder(); updateNoChatsPlaceholder();
} }
@@ -122,10 +124,32 @@ namespace PolyChat
/// Adds an message to the UI, based on .sender if known /// Adds an message to the UI, based on .sender if known
/// </summary> /// </summary>
/// <param name="message">ChatMessage</param> /// <param name="message">ChatMessage</param>
public void OnIncomingMessage(string origin, string json) public async void OnIncomingMessage(string origin, string json)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{ {
ChatPartner sendingPartner = Partners.FirstOrDefault(p => p.Code == origin); ChatPartner sendingPartner = Partners.FirstOrDefault(p => p.Code == origin);
sendingPartner.AddMessage(new ChatMessage(origin, json)); 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) private void OnDeleteChat(object sender = null, RoutedEventArgs e = null)

View File

@@ -11,7 +11,13 @@ namespace PolyChat.Models
public string Content; public string Content;
public DateTime TimeStamp; public DateTime TimeStamp;
public readonly bool Foreign; public readonly bool Foreign;
//
/// <summary>
/// Create own Message (directly sent)
/// </summary>
/// <param name="origin">My IP</param>
/// <param name="type">Message Type</param>
/// <param name="content">Message Content (not JSON)</param>
public ChatMessage(string origin, string type, string content) public ChatMessage(string origin, string type, string content)
{ {
Origin = origin; Origin = origin;
@@ -23,6 +29,28 @@ namespace PolyChat.Models
Debug.WriteLine("Created Message: " + ToString()); Debug.WriteLine("Created Message: " + ToString());
} }
/// <summary>
/// Create Message loaded with timestamp
/// </summary>
/// <param name="origin">Origin IP</param>
/// <param name="type">Message Type</param>
/// <param name="content">Message Content (not JSON)</param>
/// <param name="timeStamp">Message Content (not JSON)</param>
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());
}
/// <summary>
/// Create foreign Message (directly incoming)
/// </summary>
/// <param name="origin">Foreign IP</param>
/// <param name="json">Message Content as JSON with type and content</param>
public ChatMessage(string origin, string json) public ChatMessage(string origin, string json)
{ {
Origin = origin; Origin = origin;