Reimplement Direct Messages

This commit is contained in:
Alan Moon 2025-12-13 14:33:34 -08:00
parent 404092b484
commit 3677f145e0
5 changed files with 479 additions and 4 deletions

View File

@ -0,0 +1,127 @@
namespace qtcnet_client.Forms
{
partial class DirectMessageForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
flpMessages = new FlowLayoutPanel();
btnSend = new Button();
rtxtChatbox = new RichTextBox();
pbProfileImage = new PictureBox();
lblUsername = new Label();
((System.ComponentModel.ISupportInitialize)pbProfileImage).BeginInit();
SuspendLayout();
//
// flpMessages
//
flpMessages.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
flpMessages.AutoScroll = true;
flpMessages.BackColor = Color.White;
flpMessages.FlowDirection = FlowDirection.TopDown;
flpMessages.Location = new Point(12, 68);
flpMessages.Name = "flpMessages";
flpMessages.Size = new Size(589, 243);
flpMessages.TabIndex = 10;
flpMessages.WrapContents = false;
//
// btnSend
//
btnSend.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
btnSend.BackgroundImage = Properties.Resources.SendIcon;
btnSend.BackgroundImageLayout = ImageLayout.Zoom;
btnSend.FlatAppearance.BorderSize = 0;
btnSend.FlatStyle = FlatStyle.Flat;
btnSend.Location = new Point(526, 328);
btnSend.Name = "btnSend";
btnSend.Size = new Size(75, 44);
btnSend.TabIndex = 9;
btnSend.UseVisualStyleBackColor = true;
btnSend.Click += btnSend_Click;
//
// rtxtChatbox
//
rtxtChatbox.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
rtxtChatbox.Location = new Point(12, 317);
rtxtChatbox.Name = "rtxtChatbox";
rtxtChatbox.Size = new Size(508, 66);
rtxtChatbox.TabIndex = 8;
rtxtChatbox.Text = "";
rtxtChatbox.KeyDown += rtxtChatbox_KeyDown;
//
// pbProfileImage
//
pbProfileImage.Image = Properties.Resources.DefaultPfp;
pbProfileImage.Location = new Point(12, 12);
pbProfileImage.Name = "pbProfileImage";
pbProfileImage.Size = new Size(52, 50);
pbProfileImage.SizeMode = PictureBoxSizeMode.Zoom;
pbProfileImage.TabIndex = 11;
pbProfileImage.TabStop = false;
//
// lblUsername
//
lblUsername.AutoSize = true;
lblUsername.Font = new Font("Segoe UI", 20F, FontStyle.Bold | FontStyle.Italic);
lblUsername.ForeColor = Color.White;
lblUsername.Location = new Point(64, 17);
lblUsername.Name = "lblUsername";
lblUsername.Size = new Size(145, 37);
lblUsername.TabIndex = 12;
lblUsername.Text = "Username";
//
// DirectMessageForm
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
BackColor = Color.DodgerBlue;
ClientSize = new Size(612, 395);
Controls.Add(lblUsername);
Controls.Add(pbProfileImage);
Controls.Add(flpMessages);
Controls.Add(btnSend);
Controls.Add(rtxtChatbox);
FormBorderStyle = FormBorderStyle.FixedSingle;
MaximizeBox = false;
Name = "DirectMessageForm";
StartPosition = FormStartPosition.CenterScreen;
Text = "Direct Message";
FormClosed += DirectMessageForm_FormClosed;
Load += DirectMessageForm_Load;
((System.ComponentModel.ISupportInitialize)pbProfileImage).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private FlowLayoutPanel flpMessages;
private Button btnSend;
private RichTextBox rtxtChatbox;
private PictureBox pbProfileImage;
private Label lblUsername;
}
}

View File

@ -0,0 +1,130 @@
using qtcnet_client.Controls;
using qtcnet_client.Factories;
using qtcnet_client.Properties;
using QtCNETAPI.Dtos.User;
using QtCNETAPI.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace qtcnet_client.Forms
{
public partial class DirectMessageForm : Form
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string UserId { get; set; } = string.Empty;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string Username { get; set; } = "Username";
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public Image ProfileImage { get; set; } = Resources.DefaultPfp;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Dictionary<string, Bitmap> UserProfileImages { get; set; } = [];
public string SentMessage { get; private set; } = string.Empty;
public event EventHandler? OnSend;
public event EventHandler? OnClose;
private readonly ImageFactory _imgFactory;
public DirectMessageForm(ImageFactory imageFactory)
{
_imgFactory = imageFactory;
InitializeComponent();
}
private void DirectMessageForm_Load(object sender, EventArgs e)
{
lblUsername.Text = Username;
pbProfileImage.Image = ProfileImage;
}
private void btnSend_Click(object sender, EventArgs e)
{
if (ValidateChatBox())
{
SentMessage = rtxtChatbox.Text;
OnSend?.Invoke(this, EventArgs.Empty);
rtxtChatbox.Clear();
}
}
private void rtxtChatbox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (ValidateChatBox())
{
SentMessage = rtxtChatbox.Text;
OnSend?.Invoke(this, EventArgs.Empty);
rtxtChatbox.Clear();
}
}
}
private void DirectMessageForm_FormClosed(object sender, FormClosedEventArgs e) => OnClose?.Invoke(this, EventArgs.Empty);
public async Task LoadProfileImage(MessageControl ctrl, UserInformationDto user)
{
_ = Task.Run(async () =>
{
var img = await AddUserProfileImageToDictionary(user);
if (img == null || ctrl.IsDisposed)
return;
if (ctrl.InvokeRequired)
ctrl.Invoke(() =>
{
ctrl.ProfileImage = img;
ctrl.ReloadProfileImage();
});
else
{
ctrl.ProfileImage = img;
ctrl.ReloadProfileImage();
}
});
}
private async Task<Bitmap?> AddUserProfileImageToDictionary(UserInformationDto user)
{
if (UserProfileImages.TryGetValue(user.Id, out var image))
return image;
else
{
var img = _imgFactory.GetAndCreateProfileImage(user.Id, -1, user.ProfileCosmeticId);
UserProfileImages.Add(user.Id, img);
return img;
}
}
public void AddMessage(MessageControl messageCtrl)
{
messageCtrl.Width = flpMessages.ClientSize.Width - 10;
messageCtrl.Margin = new Padding(0, 0, 0, 6);
if (flpMessages.InvokeRequired)
{
Invoke(() =>
{
flpMessages.Controls.Add(messageCtrl);
flpMessages.ScrollControlIntoView(messageCtrl);
});
}
else
{
flpMessages.Controls.Add(messageCtrl);
flpMessages.ScrollControlIntoView(messageCtrl);
}
}
private bool ValidateChatBox()
{
return !string.IsNullOrWhiteSpace(rtxtChatbox.Text) || !string.IsNullOrEmpty(rtxtChatbox.Text);
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -25,6 +25,7 @@ namespace qtcnet_client
private MainTabControl? MainTabControl; private MainTabControl? MainTabControl;
private readonly List<ChatRoomForm> OpenChatRoomForms = []; private readonly List<ChatRoomForm> OpenChatRoomForms = [];
private readonly List<DirectMessageForm> OpenDirectMessagesForms = [];
private readonly List<ProfileForm> OpenProfileForms = []; private readonly List<ProfileForm> OpenProfileForms = [];
private readonly List<Contact> UserContactsList = []; private readonly List<Contact> UserContactsList = [];
@ -340,6 +341,7 @@ namespace qtcnet_client
_profile.ProfileImage = _imgFactory.GetAndCreateProfileImage(_user.Data.Id, _user.Data.Status, _user.Data.ProfileCosmeticId); _profile.ProfileImage = _imgFactory.GetAndCreateProfileImage(_user.Data.Id, _user.Data.Status, _user.Data.ProfileCosmeticId);
_profile.OnClose += ProfileForm_OnClose; _profile.OnClose += ProfileForm_OnClose;
_profile.OnMessageClicked += ProfileForm_OnMessageClicked;
OpenProfileForms.Add(_profile); OpenProfileForms.Add(_profile);
_profile.Show(); _profile.Show();
@ -347,6 +349,25 @@ namespace qtcnet_client
} }
} }
private void ProfileForm_OnMessageClicked(object? sender, EventArgs e)
{
if(sender is ProfileForm _profileForm)
{
// create dm form
DirectMessageForm _dmForm = new(_imgFactory)
{
UserId = _profileForm.UserId,
Username = _profileForm.Username,
ProfileImage = _imgFactory.GetAndCreateProfileImage(_profileForm.UserId, -1), // TODO - probably rework forms to take in models instead of individual params
};
_dmForm.OnClose += DirectMessage_OnClose;
_dmForm.OnSend += DirectMessage_OnSend;
_dmForm.Show();
OpenDirectMessagesForms.Add(_dmForm);
}
}
private async void MainTabControl_OnUserItemDoubleClicked(object? sender, EventArgs e) private async void MainTabControl_OnUserItemDoubleClicked(object? sender, EventArgs e)
{ {
if (sender is MainTabControl _mtCtrl) if (sender is MainTabControl _mtCtrl)
@ -526,9 +547,84 @@ namespace qtcnet_client
} }
} }
private void _gatewayService_OnDirectMessageReceived(object? sender, EventArgs e) private async void _gatewayService_OnDirectMessageReceived(object? sender, EventArgs e)
{ {
throw new NotImplementedException(); if(e is DirectMessageEventArgs _args)
{
// check for existing open form
var _existingForm = OpenDirectMessagesForms.FirstOrDefault(e => e.UserId == _args.User.Id);
if (_existingForm != null)
{
// just create a new message ctrl and add it
MessageControl _newMsg = new()
{
Message = _args.Message.Content,
Username = _args.User.Username,
};
_existingForm.AddMessage(_newMsg);
await _existingForm.LoadProfileImage(_newMsg, _args.User);
_audioService.PlaySoundEffect("sndMessage");
}
else
{
// create a new form
DirectMessageForm _dmForm = new(_imgFactory)
{
UserId = _args.User.Id,
Username = _args.User.Username,
ProfileImage = _imgFactory.GetAndCreateProfileImage(_args.User.Id, -1, _args.User.ProfileCosmeticId),
};
_dmForm.OnSend += DirectMessage_OnSend;
_dmForm.OnClose += DirectMessage_OnClose;
// add the new message to it
MessageControl _msg = new()
{
Username = _args.User.Username,
Message = _args.Message.Content,
};
_dmForm.AddMessage(_msg);
await _dmForm.LoadProfileImage(_msg, _args.User);
Task.Run(_dmForm.ShowDialog); // workaround async issues (i suck at coding and dont wanna rework the entire ui again)
OpenDirectMessagesForms.Add(_dmForm);
}
}
}
private void DirectMessage_OnClose(object? sender, EventArgs e)
{
if (sender is DirectMessageForm _dmForm)
{
OpenDirectMessagesForms.Remove(_dmForm);
_dmForm.Dispose();
}
}
private async void DirectMessage_OnSend(object? sender, EventArgs e)
{
if(sender is DirectMessageForm _dmForm)
{
// send message to user
var _userInfo = await _apiService.GetUserInformationAsync(_dmForm.UserId);
if(_userInfo.Success && _userInfo.Data != null)
{
await _gatewayService.SendDirectMessageAsync(_userInfo.Data, new QtCNETAPI.Models.Message { Content = _dmForm.SentMessage });
// add own message to form message list
MessageControl _selfMsg = new()
{
Username = _apiService.CurrentUser!.Username,
Message = _dmForm.SentMessage,
};
_dmForm.AddMessage(_selfMsg);
await _dmForm.LoadProfileImage(_selfMsg, new UserInformationDto { Id = _apiService.CurrentUser!.Id, ProfileCosmeticId = _apiService.CurrentUser.ActiveProfileCosmetic });
_audioService.PlaySoundEffect("sndSendClick");
}
}
} }
private async Task<bool> PingAPI() private async Task<bool> PingAPI()
@ -592,7 +688,7 @@ namespace qtcnet_client
_gatewayService.OnServerReconnecting += _gatewayService_OnServerReconnecting; _gatewayService.OnServerReconnecting += _gatewayService_OnServerReconnecting;
_gatewayService.OnServerReconnected += _gatewayService_OnServerReconnected; _gatewayService.OnServerReconnected += _gatewayService_OnServerReconnected;
_gatewayService.OnServerDisconnect += _gatewayService_OnServerDisconnect; _gatewayService.OnServerDisconnect += _gatewayService_OnServerDisconnect;
//_gatewayService.OnDirectMessageReceived += _gatewayService_OnDirectMessageReceived; _gatewayService.OnDirectMessageReceived += _gatewayService_OnDirectMessageReceived;
_gatewayService.OnRoomMessageReceived += _gatewayService_OnRoomMessageReceived; _gatewayService.OnRoomMessageReceived += _gatewayService_OnRoomMessageReceived;
_gatewayService.OnRoomUserListReceived += _gatewayService_OnRoomUserListReceived; _gatewayService.OnRoomUserListReceived += _gatewayService_OnRoomUserListReceived;
_gatewayService.OnRefreshUserListsReceived += _gatewayService_OnRefreshUserListReceived; _gatewayService.OnRefreshUserListsReceived += _gatewayService_OnRefreshUserListReceived;

View File

@ -33,6 +33,7 @@ namespace qtcnet_client.Forms
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public Image ProfileImage { get; set; } = Resources.DefaultPfp; public Image ProfileImage { get; set; } = Resources.DefaultPfp;
public event EventHandler? OnMessageClicked;
public event EventHandler? OnClose; public event EventHandler? OnClose;
private IApiService _apiService; private IApiService _apiService;
@ -177,7 +178,8 @@ namespace qtcnet_client.Forms
private void BtnAction1_MessageClick(object? sender, EventArgs e) private void BtnAction1_MessageClick(object? sender, EventArgs e)
{ {
throw new NotImplementedException(); Close();
OnMessageClicked?.Invoke(this, EventArgs.Empty);
} }
private async void BtnAction2_RemoveContactClick(object? sender, EventArgs e) private async void BtnAction2_RemoveContactClick(object? sender, EventArgs e)