Добавьте файлы проекта.
This commit is contained in:
118
src/Massenger.Client/ViewModels/DiscoverViewModel.cs
Normal file
118
src/Massenger.Client/ViewModels/DiscoverViewModel.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Massenger.Client.Models;
|
||||
using Massenger.Client.Pages;
|
||||
using Massenger.Client.Services;
|
||||
|
||||
namespace Massenger.Client.ViewModels;
|
||||
|
||||
public partial class DiscoverViewModel(ApiClient apiClient) : BaseViewModel
|
||||
{
|
||||
public ObservableCollection<DiscoverUserItem> Results { get; } = [];
|
||||
|
||||
[ObservableProperty]
|
||||
private string query = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string groupTitle = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string channelTitle = string.Empty;
|
||||
|
||||
public int SelectedCount => Results.Count(x => x.IsSelected);
|
||||
|
||||
[RelayCommand]
|
||||
public Task SearchAsync() =>
|
||||
RunBusyAsync(async () =>
|
||||
{
|
||||
var users = await apiClient.SearchUsersAsync(Query);
|
||||
MainThread.BeginInvokeOnMainThread(() =>
|
||||
{
|
||||
Results.Clear();
|
||||
foreach (var user in users)
|
||||
{
|
||||
var item = new DiscoverUserItem(user);
|
||||
item.PropertyChanged += (_, args) =>
|
||||
{
|
||||
if (args.PropertyName == nameof(DiscoverUserItem.IsSelected))
|
||||
{
|
||||
OnPropertyChanged(nameof(SelectedCount));
|
||||
}
|
||||
};
|
||||
|
||||
Results.Add(item);
|
||||
}
|
||||
|
||||
OnPropertyChanged(nameof(SelectedCount));
|
||||
});
|
||||
});
|
||||
|
||||
[RelayCommand]
|
||||
public async Task StartDirectChatAsync(DiscoverUserItem? user)
|
||||
{
|
||||
if (user is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await RunBusyAsync(async () =>
|
||||
{
|
||||
var chat = await apiClient.CreateDirectChatAsync(user.Id);
|
||||
await Shell.Current.GoToAsync($"{nameof(ChatPage)}?chatId={chat.Id}");
|
||||
});
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public Task CreateGroupAsync() =>
|
||||
RunBusyAsync(async () =>
|
||||
{
|
||||
var members = GetSelectedMembers();
|
||||
|
||||
if (members.Count < 2)
|
||||
{
|
||||
throw new InvalidOperationException("Select at least two users for a group.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(GroupTitle))
|
||||
{
|
||||
throw new InvalidOperationException("Group title is required.");
|
||||
}
|
||||
|
||||
var chat = await apiClient.CreateGroupChatAsync(GroupTitle, members);
|
||||
GroupTitle = string.Empty;
|
||||
ClearSelection();
|
||||
await Shell.Current.GoToAsync($"{nameof(ChatPage)}?chatId={chat.Id}");
|
||||
});
|
||||
|
||||
[RelayCommand]
|
||||
public Task CreateChannelAsync() =>
|
||||
RunBusyAsync(async () =>
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ChannelTitle))
|
||||
{
|
||||
throw new InvalidOperationException("Channel title is required.");
|
||||
}
|
||||
|
||||
var chat = await apiClient.CreateChannelAsync(ChannelTitle, GetSelectedMembers());
|
||||
ChannelTitle = string.Empty;
|
||||
ClearSelection();
|
||||
await Shell.Current.GoToAsync($"{nameof(ChatPage)}?chatId={chat.Id}");
|
||||
});
|
||||
|
||||
private List<Guid> GetSelectedMembers() =>
|
||||
Results
|
||||
.Where(x => x.IsSelected)
|
||||
.Select(x => x.Id)
|
||||
.ToList();
|
||||
|
||||
private void ClearSelection()
|
||||
{
|
||||
foreach (var item in Results)
|
||||
{
|
||||
item.IsSelected = false;
|
||||
}
|
||||
|
||||
OnPropertyChanged(nameof(SelectedCount));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user