mirror of
https://github.com/ryzij/AMessanger-Avalonia-Client.git
synced 2026-09-18 19:55:35 +00:00
Compare commits
2 Commits
a40fcca7be
...
305d253c03
| Author | SHA1 | Date | |
|---|---|---|---|
| 305d253c03 | |||
| 4d36c5242f |
@@ -7,7 +7,6 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Models\" />
|
|
||||||
<AvaloniaResource Include="Assets\**" />
|
<AvaloniaResource Include="Assets\**" />
|
||||||
<None Update="ConnectionUrl">
|
<None Update="ConnectionUrl">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace AMessanger.Models
|
||||||
|
{
|
||||||
|
public class Message(string userName, string content)
|
||||||
|
{
|
||||||
|
public string UserName { get; set; } = userName;
|
||||||
|
public string Content { get; set; } = content;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using AMessanger.Models;
|
||||||
using Microsoft.AspNetCore.SignalR.Client;
|
using Microsoft.AspNetCore.SignalR.Client;
|
||||||
|
|
||||||
namespace AMessanger.Services;
|
namespace AMessanger.Services;
|
||||||
@@ -9,7 +10,7 @@ public class ChatService
|
|||||||
{
|
{
|
||||||
private readonly HubConnection _connection;
|
private readonly HubConnection _connection;
|
||||||
|
|
||||||
public event Action<string, string>? OnMessageRecieved;
|
public event Action<Message>? OnMessageRecieved;
|
||||||
|
|
||||||
public bool IsConnected { get; private set; } = false;
|
public bool IsConnected { get; private set; } = false;
|
||||||
|
|
||||||
@@ -20,8 +21,8 @@ public class ChatService
|
|||||||
.WithAutomaticReconnect()
|
.WithAutomaticReconnect()
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
_connection.On<string, string>("ReceiveMessage", (user, message) =>
|
_connection.On<Message>("ReceiveMessage", m =>
|
||||||
OnMessageRecieved?.Invoke(user, message));
|
OnMessageRecieved?.Invoke(m));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task ConnectAsync(CancellationToken cancellationToken = default)
|
public Task ConnectAsync(CancellationToken cancellationToken = default)
|
||||||
@@ -42,8 +43,8 @@ public class ChatService
|
|||||||
return _connection.StopAsync(cancellationToken);
|
return _connection.StopAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task SendMessageAsync(string user, string message, CancellationToken cancellationToken = default)
|
public Task SendMessageAsync(Message message, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return _connection.InvokeAsync("SendMessage", user, message, cancellationToken);
|
return _connection.InvokeAsync("SendMessage", message, cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,17 +1,17 @@
|
|||||||
using System.Collections.ObjectModel;
|
using AMessanger.Models;
|
||||||
using AMessanger.Services;
|
using AMessanger.Services;
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
|
||||||
using Avalonia.Threading;
|
using Avalonia.Threading;
|
||||||
using System.Windows.Input;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
using System.Diagnostics;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Windows.Input;
|
||||||
|
|
||||||
namespace AMessanger.ViewModels;
|
namespace AMessanger.ViewModels;
|
||||||
|
|
||||||
public partial class MainViewModel : ViewModelBase
|
public partial class MainViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial ObservableCollection<string> Messages { get; private set; } = new();
|
public partial ObservableCollection<Message> Messages { get; private set; } = new();
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial string Message { get; set; } = string.Empty;
|
public partial string Message { get; set; } = string.Empty;
|
||||||
@@ -31,10 +31,9 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
SendButtonCommand = new RelayCommand(SendMessage);
|
SendButtonCommand = new RelayCommand(SendMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnMessageRecived(string user, string message)
|
private void OnMessageRecived(Message message)
|
||||||
{
|
{
|
||||||
Dispatcher.UIThread.Post(() => Messages.Add($"{user}: {message}"));
|
Dispatcher.UIThread.Post(() => Messages.Add(message));
|
||||||
Debug.WriteLine($"{user}: {message}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SendMessage()
|
private void SendMessage()
|
||||||
@@ -42,7 +41,7 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
if (string.IsNullOrWhiteSpace(Message))
|
if (string.IsNullOrWhiteSpace(Message))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_chat.SendMessageAsync(UserName, Message.Trim());
|
_chat.SendMessageAsync(new Message(UserName, Message.Trim()));
|
||||||
Message = string.Empty;
|
Message = string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,8 +21,26 @@
|
|||||||
<RowDefinition Height="Auto" />
|
<RowDefinition Height="Auto" />
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
|
|
||||||
<ListBox Grid.Row="0"
|
<ListBox Grid.Row="0" ItemsSource="{Binding Messages}">
|
||||||
ItemsSource="{Binding Messages}" />
|
<ListBox.ItemTemplate>
|
||||||
|
<DataTemplate>
|
||||||
|
<Grid>
|
||||||
|
<Grid.ColumnDefinitions>
|
||||||
|
<ColumnDefinition Width="Auto" />
|
||||||
|
<ColumnDefinition Width="*" />
|
||||||
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
|
<TextBlock Grid.Column="0"
|
||||||
|
Text="{Binding UserName}"
|
||||||
|
Margin="5" />
|
||||||
|
<TextBlock Grid.Column="1"
|
||||||
|
Text="{Binding Content}"
|
||||||
|
TextWrapping="WrapWithOverflow"
|
||||||
|
Margin="5" />
|
||||||
|
</Grid>
|
||||||
|
</DataTemplate>
|
||||||
|
</ListBox.ItemTemplate>
|
||||||
|
</ListBox>
|
||||||
|
|
||||||
<Grid Grid.Row="1" Margin="5">
|
<Grid Grid.Row="1" Margin="5">
|
||||||
<Grid.ColumnDefinitions>
|
<Grid.ColumnDefinitions>
|
||||||
@@ -31,8 +49,8 @@
|
|||||||
<ColumnDefinition Width="Auto" />
|
<ColumnDefinition Width="Auto" />
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
<TextBox Text="{Binding UserName}" />
|
<TextBox Text="{Binding UserName}" VerticalContentAlignment="Center" />
|
||||||
<TextBox Grid.Column="1" Text="{Binding Message}" />
|
<TextBox Grid.Column="1" Text="{Binding Message}" TextWrapping="WrapWithOverflow" />
|
||||||
<Button Grid.Column="2" Command="{Binding SendButtonCommand}">Send</Button>
|
<Button Grid.Column="2" Command="{Binding SendButtonCommand}">Send</Button>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
Reference in New Issue
Block a user