From d42acbc90ebb529c2d1cab3d83383cddb40f135a Mon Sep 17 00:00:00 2001 From: "Felix Hartmann (PEA3-Fe-FI)" Date: Tue, 21 Sep 2021 09:28:29 +0200 Subject: [PATCH] remove MSG.cs, merge content from MSG.cs and ChatMessage.cs, refactor code --- PolyChat/MainPage.xaml | 4 ++-- PolyChat/MainPage.xaml.cs | 13 +++++++------ PolyChat/Models/ChatMessage.cs | 26 +++++++++++++++++--------- PolyChat/Models/Client.cs | 6 +++--- PolyChat/Models/MSG.cs | 29 ----------------------------- PolyChat/PolyChat.csproj | 1 - 6 files changed, 29 insertions(+), 50 deletions(-) delete mode 100644 PolyChat/Models/MSG.cs diff --git a/PolyChat/MainPage.xaml b/PolyChat/MainPage.xaml index 8439a3e..0887502 100644 --- a/PolyChat/MainPage.xaml +++ b/PolyChat/MainPage.xaml @@ -90,8 +90,8 @@ - - + + diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index b0b8e00..2e26594 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -38,8 +38,8 @@ namespace PolyChat public void OnSendMessage(object sender = null, RoutedEventArgs e = null) { selectedPartner.AddMessage(new ChatMessage( + DateTime.Now, inputSend.Text, - DateTime.Now.ToString(), false )); Controller.sendMessage(selectedPartner.Code, inputUsername.Text, inputSend.Text); @@ -62,13 +62,14 @@ namespace PolyChat } } - public void OnIncomingMessage(MSG message) + public void OnIncomingMessage(ChatMessage message) { - ChatPartner sendingPartner = Partners.First(p => p.Code == message.ip.ToString()); + ChatPartner sendingPartner = Partners.First(p => p.Code == message.Ip); sendingPartner.AddMessage(new ChatMessage( - message.msg, - message.timestamp.ToString(), - true + message.Timestamp, + message.Msg, + true, + message.Sender )); } diff --git a/PolyChat/Models/ChatMessage.cs b/PolyChat/Models/ChatMessage.cs index b429318..e2ebb6d 100644 --- a/PolyChat/Models/ChatMessage.cs +++ b/PolyChat/Models/ChatMessage.cs @@ -1,23 +1,31 @@ -namespace PolyChat.Models +using System; + +namespace PolyChat.Models { public class ChatMessage { - public string Text; - public string Date; - public bool Foreign; + public readonly string Sender; + public readonly DateTime Timestamp = new DateTime(1970, 01, 01); + public readonly string Msg = "empty"; + public readonly string Ip; + public readonly bool Foreign; + public readonly string StringTimeStamp; - public ChatMessage(string text, string date, bool foreign) + public ChatMessage(DateTime Timestamp, string Msg, bool Foreign = true, string Sender= "Unknown", string Ip = "127.0.0.1") { - Text = text; - Date = date; - Foreign = foreign; + this.Sender = Sender; + this.Timestamp = Timestamp; + StringTimeStamp = Timestamp.ToString(); + this.Msg = Msg; + this.Foreign = Foreign; + this.Ip = Ip; } override public string ToString() { string prefix = Foreign ? "Other" : "Me"; - return $"{prefix}: Text"; + return $"{prefix}: {Msg}({Sender})"; } } } \ No newline at end of file diff --git a/PolyChat/Models/Client.cs b/PolyChat/Models/Client.cs index 0b2967e..194d2db 100644 --- a/PolyChat/Models/Client.cs +++ b/PolyChat/Models/Client.cs @@ -24,7 +24,7 @@ namespace PolyChat.Models { private SocketIOClient connection; public Boolean isConnected = false; - private List msgStack = new List(); + private List msgStack = new List(); private Boolean active = true; private String ip; @@ -49,7 +49,7 @@ namespace PolyChat.Models new Thread(() => { //create msg - MSG msg = new MSG(sender, Controller.ip, chatMessage, timestamp); + ChatMessage msg = new ChatMessage(timestamp, chatMessage, false, sender, Controller.ip); //convert msg String petJson = JsonNet.Serialize(msg); @@ -78,7 +78,7 @@ namespace PolyChat.Models { connection.On(SendCode.Message.ToString(), (Data) => { - MSG pet = JsonNet.Deserialize(BitConverter.ToString(Data[0].ToObject())); + ChatMessage pet = JsonNet.Deserialize(BitConverter.ToString(Data[0].ToObject())); //TODO: send message to GUI }); connection.On(SendCode.Command.ToString(), (Data) => diff --git a/PolyChat/Models/MSG.cs b/PolyChat/Models/MSG.cs deleted file mode 100644 index 95dae94..0000000 --- a/PolyChat/Models/MSG.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; - -namespace PolyChat.Models -{ - /// - /// dumy class for json converter - /// - public class MSG - { - public string sender = "unknown"; - public DateTime timestamp = new DateTime(2000, 01, 01); - public string msg = "empty"; - public string ip; - - - public MSG(string sender, string ip, String msg, DateTime timestamp) - { - this.sender = sender; - this.ip = ip; - this.timestamp = timestamp; - this.msg = msg; - } - } -} diff --git a/PolyChat/PolyChat.csproj b/PolyChat/PolyChat.csproj index f81e50a..1393077 100644 --- a/PolyChat/PolyChat.csproj +++ b/PolyChat/PolyChat.csproj @@ -127,7 +127,6 @@ -