mirror of
https://github.com/ryzij/AMessanger-Avalonia-Client.git
synced 2026-09-18 11:43:21 +00:00
49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using System.Collections.ObjectModel;
|
|
using AMessanger.Services;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using Avalonia.Threading;
|
|
using System.Windows.Input;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using System.Diagnostics;
|
|
|
|
namespace AMessanger.ViewModels;
|
|
|
|
public partial class MainViewModel : ViewModelBase
|
|
{
|
|
[ObservableProperty]
|
|
public partial ObservableCollection<string> Messages { get; private set; } = new();
|
|
|
|
[ObservableProperty]
|
|
public partial string Message { get; set; } = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
public partial string UserName { get; set; } = "Test user";
|
|
|
|
public ICommand SendButtonCommand { get; private set; }
|
|
|
|
private readonly ChatService _chat;
|
|
|
|
public MainViewModel(ChatService chatService)
|
|
{
|
|
_chat = chatService;
|
|
_chat.OnMessageRecieved += OnMessageRecived;
|
|
|
|
SendButtonCommand = new RelayCommand(SendMessage);
|
|
}
|
|
|
|
private void OnMessageRecived(string user, string message)
|
|
{
|
|
Dispatcher.UIThread.Post(() => Messages.Add($"{user}: {message}"));
|
|
Debug.WriteLine($"{user}: {message}");
|
|
}
|
|
|
|
private void SendMessage()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(Message))
|
|
return;
|
|
|
|
_chat.SendMessageAsync(UserName, Message.Trim());
|
|
Message = string.Empty;
|
|
}
|
|
}
|