Username change now dirty reactive in ui

This commit is contained in:
Patrick Hellebrand
2021-09-23 14:02:42 +02:00
parent e313f7bde7
commit 3cce36ee21
3 changed files with 23 additions and 17 deletions

View File

@@ -37,9 +37,14 @@ namespace PolyChat
{ {
UIController = uiController; UIController = uiController;
OwnIP = getIP(); OwnIP = getIP();
loadChats(); //loadChats();
//SaveChats("10", "{das ist ein test}"); //SaveChats("10", "{das ist ein test}");
Serve(); Serve();
// test
UIController.OnIncomingConnection("1.1.1.1");
UIController.OnIncomingConnection("1.2.3.4");
UIController.OnIncomingConnection("1.2.4.8");
} }
public void Connect(string ip) public void Connect(string ip)
@@ -70,7 +75,7 @@ namespace PolyChat
{ {
Debug.WriteLine("--- Client connected! ---"); Debug.WriteLine("--- Client connected! ---");
// setup event listeners // setup event listeners
socket.On("initial", async (JToken[] data) => socket.On("initial", (JToken[] data) =>
{ {
Debug.WriteLine("--- initial packet received ---"); Debug.WriteLine("--- initial packet received ---");
string ForeignIp = data[0].ToString(); string ForeignIp = data[0].ToString();

View File

@@ -26,6 +26,7 @@ namespace PolyChat
private ObservableCollection<ChatPartner> Partners; private ObservableCollection<ChatPartner> Partners;
private ChatPartner selectedPartner = null; private ChatPartner selectedPartner = null;
private string username; private string username;
public MainPage() public MainPage()
{ {
this.InitializeComponent(); this.InitializeComponent();
@@ -80,6 +81,14 @@ namespace PolyChat
public async void OnOpenNewChatDialog(object sender = null, RoutedEventArgs e = null) public async void OnOpenNewChatDialog(object sender = null, RoutedEventArgs e = null)
{ {
OnIncomingMessage(
"1.1.1.1",
new JObject(
new JProperty("type", "username"),
new JProperty("content", "Cloudflare")
).ToString(),
DateTime.Now
);
NewChatDialog dialog = new NewChatDialog(); NewChatDialog dialog = new NewChatDialog();
var result = await dialog.ShowAsync(); var result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary) if (result == ContentDialogResult.Primary)
@@ -138,7 +147,10 @@ namespace PolyChat
{ {
case "username": case "username":
Debug.WriteLine($"! username change for {sendingPartner.Code} -> {content}"); Debug.WriteLine($"! username change for {sendingPartner.Code} -> {content}");
sendingPartner.SetName(Name); sendingPartner.Name = content;
int index = Partners.IndexOf(sendingPartner);
Partners.Remove(sendingPartner);
Partners.Insert(index, sendingPartner);
break; break;
default: default:
sendingPartner.AddMessage(new ChatMessage(origin, type, content, timeStamp)); sendingPartner.AddMessage(new ChatMessage(origin, type, content, timeStamp));

View File

@@ -1,17 +1,17 @@
using SocketIOSharp.Server.Client; using SocketIOSharp.Server.Client;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace PolyChat.Models namespace PolyChat.Models
{ {
public class ChatPartner : INotifyPropertyChanged public class ChatPartner
{ {
public string Name; public string Name;
public string Code; public string Code;
public ObservableCollection<ChatMessage> Messages; public ObservableCollection<ChatMessage> Messages;
private SocketIOSocket socketIOSocket; private SocketIOSocket socketIOSocket;
public event PropertyChangedEventHandler PropertyChanged;
public ChatPartner(string name, string code, ObservableCollection<ChatMessage> messages = null) public ChatPartner(string name, string code, ObservableCollection<ChatMessage> messages = null)
{ {
@@ -23,20 +23,9 @@ namespace PolyChat.Models
public SocketIOSocket SocketIOSocket { get => socketIOSocket; set => socketIOSocket = value; } public SocketIOSocket SocketIOSocket { get => socketIOSocket; set => socketIOSocket = value; }
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void AddMessage(ChatMessage message) public void AddMessage(ChatMessage message)
{ {
Messages.Add(message); Messages.Add(message);
} }
public void SetName(string name)
{
Name = name;
NotifyPropertyChanged("Name");
}
} }
} }