From 924676bee691dbd283b4e613d820b74eb73861d6 Mon Sep 17 00:00:00 2001 From: Patrick Hellebrand Date: Thu, 23 Sep 2021 12:53:57 +0200 Subject: [PATCH] Chats now only try to connect, if they are opened --- PolyChat/Controller.cs | 9 +++++---- PolyChat/MainPage.xaml.cs | 7 +++++-- PolyChat/Models/ChatPartner.cs | 11 ++++++++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index ec9ee05..989b9e2 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -146,9 +146,11 @@ namespace PolyChat private bool isInConnections(string IP) { - if (Connections.ContainsKey(IP)) - return true; - return false; + return Connections.ContainsKey(IP); + } + public bool IsConnected(string ip) + { + return Connections.ContainsKey(ip) && Connections[ip].IsConnected(); } public static string getIP() @@ -195,7 +197,6 @@ namespace PolyChat ip = ip.Substring(0, ip.Length - 4); Debug.WriteLine($"-{ip}"); Debug.WriteLine(jsonArr); - Connect(ip); UIController.OnIncomingConnection(ip); UIController.OnIncomingMessages(ip, jsonArr); } diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index c3c3b84..d39e27d 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -11,6 +11,7 @@ using System.Threading.Tasks; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Data; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 @@ -136,6 +137,7 @@ namespace PolyChat switch (type) { case "username": + Debug.WriteLine($"! username change for {sendingPartner.Code} -> {content}"); sendingPartner.SetName(Name); break; default: @@ -156,8 +158,8 @@ namespace PolyChat new ChatMessage( origin, item["type"].ToString(), - item["content"].ToString(), - DateTime.Parse(item["timestamp"].ToString()) + item["content"].ToString()//, + //DateTime.Parse(item["timestamp"].ToString()) ) ); } @@ -190,6 +192,7 @@ namespace PolyChat listViewMessages.ItemsSource = selectedPartner.Messages; selectedPartnerName.Text = selectedPartner.Name; updateNoChatSelected(); + if (!Controller.IsConnected(code)) Controller.Connect(code); } private void OnKeyUp(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e) diff --git a/PolyChat/Models/ChatPartner.cs b/PolyChat/Models/ChatPartner.cs index 118b7af..32df3be 100644 --- a/PolyChat/Models/ChatPartner.cs +++ b/PolyChat/Models/ChatPartner.cs @@ -1,14 +1,17 @@ using SocketIOSharp.Server.Client; using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Runtime.CompilerServices; namespace PolyChat.Models { - public class ChatPartner + public class ChatPartner : INotifyPropertyChanged { public string Name; public string Code; public ObservableCollection Messages; private SocketIOSocket socketIOSocket; + public event PropertyChangedEventHandler PropertyChanged; public ChatPartner(string name, string code, ObservableCollection messages = null) { @@ -20,6 +23,11 @@ namespace PolyChat.Models 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) { Messages.Add(message); @@ -28,6 +36,7 @@ namespace PolyChat.Models public void SetName(string name) { Name = name; + NotifyPropertyChanged("Name"); } } } \ No newline at end of file