private void MainChatsList_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (MainChatsList.SelectedItem != null) { MainMessagesNotify selectedChat = MainChatsList.SelectedItem as MainMessagesNotify; MainChatsList.SelectedItem = null; // select nothing if (selectedChat == null) { return; } // open the chat window if (!OpenWindowsList.ContainsKey(selectedChat.Username)) { OpenWindowsList.Add(selectedChat.Username, new ChatWindow(selectedChat.Username)); OpenWindowsList[selectedChat.Username].Show(); // reset new messages MessagesList.First(x => x == selectedChat).NewMessages = 0; SharedStuff.Database.ExecuteAsync("UPDATE Users SET UnreadMessages = ? WHERE Username = ?", 0, selectedChat.Username); } else { OpenWindowsList[selectedChat.Username].Activate(); } } }
private async void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e) { // create tables await SharedStuff.Database.CreateTablesAsync <DatabaseHelper.Messages, DatabaseHelper.Files, DatabaseHelper.Users>(); // get all of the users that have messaged the user and get their last message { // at first get list of all users var users = await SharedStuff.Database.Table <DatabaseHelper.Users>().ToArrayAsync(); var msgs = new MainMessagesNotify[users.Length]; // then select last message of the users from Messages table for (int i = 0; i < users.Length; i++) { var lastMsgQ = await SharedStuff.Database.QueryAsync <DatabaseHelper.Messages>("SELECT * FROM Messages WHERE Username == ? ORDER BY ID DESC LIMIT 1", users[i].Username); if (lastMsgQ.Count > 0) { var lastMsg = lastMsgQ[0]; string msg = lastMsg.Type == 0 ? lastMsg.Payload : (await SharedStuff.Database.Table <DatabaseHelper.Files>() .Where(file => file.Token == lastMsg.Payload).FirstAsync()).Name; msgs[i] = new MainMessagesNotify { Type = (MessageType)lastMsg.Type, Username = users[i].Username, IsLastMessageForUser = lastMsg.MyMessage, Message = msg, Name = users[i].Name, FullDate = lastMsg.Date, NewMessages = users[i].UnreadMessages }; } } // sort the messages by the date Array.Sort(msgs, (x, y) => y.FullDate.CompareTo(x.FullDate)); // reverse sort; The latest date is at top // add them to main messages list foreach (var msg in msgs) { MessagesList.Add(msg); } } // connect to server and register the websocket await Task.Run(() => { while (true) { try { SharedStuff.Websocket = new WebSocket("wss://" + Properties.Settings.Default.ServerAddress + "/chat/registerUpdater"); SharedStuff.Websocket.OnMessage += WS_OnMessage; if (Properties.Settings.Default.TrustInvalidSSL) { SharedStuff.Websocket.SslConfiguration.ServerCertificateValidationCallback = (a, certificate, chain, sslPolicyErrors) => true; } SharedStuff.Websocket.Connect(); SharedStuff.Websocket.Send(JsonConvert.SerializeObject(new JsonTypes.HelloMessage { Username = _savedData.Username, Password = _savedData.Password, Verify = Convert.ToBase64String(SharedStuff.Curve.calculateSignature(Convert.FromBase64String(_savedData.PrivateKey), Encoding.UTF8.GetBytes(_savedData.Password))) })); } catch (Exception) { Thread.Sleep(5000); // wait 5 seconds and retry continue; } break; } }); }