feat(chat): envoi de messages dans un salon joint
- DTO ChatMessage (Name/Room/Message) pour l'evenement ReceiveMessage - ChatViewModel : MessageToSend, HasJoinedRoom, handler ReceiveMessage, SendMessageCommand (CanExecute: connecte + salon joint + texte non vide) - ChatPage : TextBox de saisie (Enter envoie) + bouton Envoyer, StatusBar repositionnee en Grid.Row=3
This commit is contained in:
parent
75bb2600b5
commit
51e445b5ba
3 changed files with 81 additions and 2 deletions
|
|
@ -45,8 +45,17 @@ namespace PostIt.ViewModels.Chat
|
|||
public partial StatusNotice Status { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyCanExecuteChangedFor(nameof(SendMessageCommand))]
|
||||
public partial bool IsConnected { get; set; } = false;
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyCanExecuteChangedFor(nameof(SendMessageCommand))]
|
||||
public partial bool HasJoinedRoom { get; set; } = false;
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyCanExecuteChangedFor(nameof(SendMessageCommand))]
|
||||
public partial string MessageToSend { get; set; } = "";
|
||||
|
||||
// Listes dynamiques pour l'UI
|
||||
public ObservableCollection<string> Messages { get; } = new();
|
||||
public ObservableCollection<string> ConnectedUsers { get; } = new();
|
||||
|
|
@ -58,6 +67,28 @@ namespace PostIt.ViewModels.Chat
|
|||
[RelayCommand]
|
||||
public async Task JoinRoomCommand() => await JoinRoomAsync();
|
||||
|
||||
// Le serveur (ChatHub.Send) refuse tout message d'un utilisateur
|
||||
// qui n'a pas joint le salon ("NoJoinNoSend") : on garde la
|
||||
// commande inactive tant que le Join n'a pas abouti.
|
||||
private bool CanSendMessage() =>
|
||||
IsConnected && HasJoinedRoom && !string.IsNullOrWhiteSpace(MessageToSend);
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanSendMessage))]
|
||||
private async Task SendMessageAsync()
|
||||
{
|
||||
if (_connection == null) return;
|
||||
var text = MessageToSend.Trim();
|
||||
try
|
||||
{
|
||||
await _connection.InvokeAsync("Send", CurrentRoom, text);
|
||||
MessageToSend = string.Empty;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Messages.Add($"Erreur d'envoi : {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanNavigateNext { get; protected set; } = false;
|
||||
public override bool CanNavigatePrevious { get; protected set; } = true;
|
||||
|
||||
|
|
@ -70,6 +101,7 @@ namespace PostIt.ViewModels.Chat
|
|||
_connection = null;
|
||||
}
|
||||
IsConnected = false;
|
||||
HasJoinedRoom = false;
|
||||
Status = new StatusNotice("Déconnecté", StatusSeverity.Info,
|
||||
new RelayCheckCommand("Connecter", async () => await ConnectAsync()));
|
||||
}
|
||||
|
|
@ -140,11 +172,19 @@ namespace PostIt.ViewModels.Chat
|
|||
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
Messages.Add($"Salon rejoint avec succès : {roomInfo.Name}");
|
||||
HasJoinedRoom = true;
|
||||
ConnectedUsers.Clear();
|
||||
foreach (var u in roomInfo.Users) ConnectedUsers.Add(u);
|
||||
});
|
||||
});
|
||||
|
||||
// 4. Réception des messages du salon (diffusés par ChatHub.Send)
|
||||
_connection.On<ChatMessage>("ReceiveMessage", (msg) =>
|
||||
{
|
||||
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
|
||||
Messages.Add($"[{msg.Room}] {msg.Name} : {msg.Message}"));
|
||||
});
|
||||
|
||||
await _connection.StartAsync();
|
||||
IsConnected = true;
|
||||
Status = new StatusNotice("Connecté au Hub SignalR", StatusSeverity.Info, new RelayCheckCommand("Déconnecter", async () => await DisconnectAsync()));
|
||||
|
|
|
|||
Loading…
Reference in a new issue