mirror of
https://github.com/ryzij/AMessanger-Avalonia-Client.git
synced 2026-09-18 20:13:20 +00:00
First commit
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user