mirror of
https://github.com/ryzij/AMessanger-Avalonia-Client.git
synced 2026-09-18 20:13:20 +00:00
48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using AMessanger.Models;
|
|
using AMessanger.Services;
|
|
using Avalonia.Threading;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using System.Collections.ObjectModel;
|
|
using System.Windows.Input;
|
|
|
|
namespace AMessanger.ViewModels;
|
|
|
|
public partial class MainViewModel : ViewModelBase
|
|
{
|
|
[ObservableProperty]
|
|
public partial ObservableCollection<Message> 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(Message message)
|
|
{
|
|
Dispatcher.UIThread.Post(() => Messages.Add(message));
|
|
}
|
|
|
|
private void SendMessage()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(Message))
|
|
return;
|
|
|
|
_chat.SendMessageAsync(new Message(UserName, Message.Trim()));
|
|
Message = string.Empty;
|
|
}
|
|
}
|