remove MSG.cs, merge content from MSG.cs and ChatMessage.cs, refactor code

This commit is contained in:
Felix Hartmann (PEA3-Fe-FI)
2021-09-21 09:28:29 +02:00
parent 5d69842ec0
commit d42acbc90e
6 changed files with 29 additions and 50 deletions

View File

@@ -90,8 +90,8 @@
<ListView.ItemTemplate>
<DataTemplate x:DataType="models:ChatMessage">
<StackPanel x:Name="Message" Margin="0 4" Padding="16 8" CornerRadius="4" Background="{ThemeResource SystemAccentColor}">
<TextBlock Text="{x:Bind Text}"/>
<TextBlock Text="{x:Bind Date}"/>
<TextBlock Text="{x:Bind Msg}"/>
<TextBlock Text="{x:Bind StringTimeStamp}"/>
<TextBlock Text="{x:Bind Foreign}"/>
</StackPanel>
</DataTemplate>

View File

@@ -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
));
}

View File

@@ -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})";
}
}
}

View File

@@ -24,7 +24,7 @@ namespace PolyChat.Models
{
private SocketIOClient connection;
public Boolean isConnected = false;
private List<MSG> msgStack = new List<MSG>();
private List<ChatMessage> msgStack = new List<ChatMessage>();
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<MSG>(BitConverter.ToString(Data[0].ToObject<byte[]>()));
ChatMessage pet = JsonNet.Deserialize<ChatMessage>(BitConverter.ToString(Data[0].ToObject<byte[]>()));
//TODO: send message to GUI
});
connection.On(SendCode.Command.ToString(), (Data) =>

View File

@@ -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
{
/// <summary>
/// dumy class for json converter
/// </summary>
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;
}
}
}

View File

@@ -127,7 +127,6 @@
<Compile Include="Models\Client.cs" />
<Compile Include="Models\ClientHandler.cs" />
<Compile Include="Models\Exceptions\ConnectionFailedException.cs" />
<Compile Include="Models\MSG.cs" />
<Compile Include="Models\SendCode.cs" />
<Compile Include="Models\Socket.cs" />
<Compile Include="Controller.cs" />