From 4e60fd2bdb6537a8b7d6be888fcb15f649ab4bff Mon Sep 17 00:00:00 2001 From: "Felix Hartmann (PEA3-Fe-FI)" Date: Thu, 23 Sep 2021 14:50:35 +0200 Subject: [PATCH] delete chat only on explicit button press, not on disconnect, dialog opening fixed --- PolyChat/Controller.cs | 13 ++++++---- PolyChat/MainPage.xaml.cs | 43 +++++++++++++++++----------------- PolyChat/Models/Connection.cs | 8 +++---- PolyChat/Models/FileManager.cs | 2 +- PolyChat/Views/Dialog.xaml.cs | 2 -- 5 files changed, 35 insertions(+), 33 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 1ec8a3e..ce10213 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -96,7 +96,6 @@ namespace PolyChat Debug.WriteLine("---- Added new Connection ----"); Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data), CloseChat)); UIController.OnIncomingConnection(ForeignIp); - fileManager.loadChat(ForeignIp); } }); }); @@ -139,7 +138,7 @@ namespace PolyChat else Debug.WriteLine("Undefined: " + data); } - public void CloseChat(string IP, bool wasConnected = true) + public void CloseChat(string IP, bool wasConnected = true, bool delete = false) { Debug.WriteLine($"Deleting connection with IP:{IP}"); if (IP != null && Connections.ContainsKey(IP)) @@ -147,14 +146,18 @@ namespace PolyChat Connections[IP].Close(); Connections.Remove(IP); } - CloseChatUI(IP, wasConnected); + if (delete || !wasConnected) + CloseChatUI(IP, wasConnected, delete); + if (delete) + fileManager.deleteChat(IP); } - private void CloseChatUI(string IP, bool wasConnected = true) + private void CloseChatUI(string IP, bool wasConnected = true, bool delete = false) { UIController.OnChatPartnerDeleted(IP); string heading = wasConnected ? "Connection Closed" : "Connection Failed"; - UIController.ShowConnectionError(IP, heading, $"Connecting to {IP} failed..."); + if(!delete) + UIController.ShowConnectionError(IP, heading, $"Connecting to {IP} failed..."); } private bool isInConnections(string IP) diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index 752a608..59ef3c3 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -46,26 +46,27 @@ namespace PolyChat await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Dialog dialog = new Dialog( - Dialog.TYPE_ERROR, - heading, - message, - new DialogButton( - "Retry", - () => - { - Controller.Connect(param); - Partners.Add(new ChatPartner( - "Connecting...", - param - )); - updateNoChatsPlaceholder(); - } - ), - new DialogButton( - "Ignore", - () => { /* do nothing */ } - ) - ); + Dialog.TYPE_ERROR, + heading, + message, + new DialogButton( + "Retry", + () => + { + Controller.Connect(param); + Partners.Add(new ChatPartner( + "Connecting...", + param + )); + updateNoChatsPlaceholder(); + } + ), + new DialogButton( + "Ignore", + () => { /* do nothing */ } + ) + ); + SafelyOpenDialog(dialog); }); } @@ -184,7 +185,7 @@ namespace PolyChat private void OnDeleteChat(object sender = null, RoutedEventArgs e = null) { - Controller.CloseChat(selectedPartner.Code); + Controller.CloseChat(selectedPartner.Code,delete: true); Partners.Remove(selectedPartner); updateNoChatsPlaceholder(); updateNoChatSelected(); diff --git a/PolyChat/Models/Connection.cs b/PolyChat/Models/Connection.cs index 9c985b6..5d45440 100644 --- a/PolyChat/Models/Connection.cs +++ b/PolyChat/Models/Connection.cs @@ -15,9 +15,9 @@ namespace PolyChat.Models private SocketIOSocket Socket; private bool Connected = false; private readonly string IP; - private Action DeleteConnection; + private Action DeleteConnection; - public Connection(string ip, ushort port, Action onMessage, Action onClose) + public Connection(string ip, ushort port, Action onMessage, Action onClose) { Debug.WriteLine("! CONNECTING TO SERVER !"); IP = ip; @@ -32,7 +32,7 @@ namespace PolyChat.Models Client.On("message", (Action) onMessage); } - public Connection(SocketIOSocket socket, Action onMessage, Action onClose) + public Connection(SocketIOSocket socket, Action onMessage, Action onClose) { Socket = socket; DeleteConnection = onClose; @@ -66,7 +66,7 @@ namespace PolyChat.Models { Debug.WriteLine("--- Disconnected! ---"); Debug.WriteLine($"--- Deleting Connection with IP: {IP}---"); - DeleteConnection(IP, IsConnected()); + DeleteConnection(IP, IsConnected(),false); Connected = false; } private void OnError(JToken[] data) diff --git a/PolyChat/Models/FileManager.cs b/PolyChat/Models/FileManager.cs index 474aee2..9a8045e 100644 --- a/PolyChat/Models/FileManager.cs +++ b/PolyChat/Models/FileManager.cs @@ -36,7 +36,7 @@ namespace PolyChat.Models { foreach (String path in filepaths) { - if (Path.GetFileName(path).Equals(ip)) + if (Path.GetFileName(path).Equals(ip+".txt")) { File.Delete(path); return; diff --git a/PolyChat/Views/Dialog.xaml.cs b/PolyChat/Views/Dialog.xaml.cs index 331fb6f..6e1ce68 100644 --- a/PolyChat/Views/Dialog.xaml.cs +++ b/PolyChat/Views/Dialog.xaml.cs @@ -34,8 +34,6 @@ namespace PolyChat.Views // TODO: use event handlers and asign actions here Primary = primary.Action; Secondary = secondary.Action; - // show - MainPage.SafelyOpenDialog(this); } private void setType(string type, string message)