123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- using Avalonia.Input;
- using Avalonia.Notification;
- using Avalonia.Threading;
- using DynamicData.Binding;
- using HanumanInstitute.MvvmDialogs;
- using ReactiveUI;
- using System;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Reactive.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using VeloeAvaloniaKemonoPartyApp.Models;
- namespace VeloeAvaloniaKemonoPartyApp.ViewModels
- {
- public class CreatorsViewModel : ViewModelBase
- {
- private CancellationTokenSource? _cancellationTokenSource;
- private readonly IDialogService _dialogService;
- public CreatorsViewModel(IDialogService dialogService)
- {
- _dialogService = dialogService;
- this.WhenAnyValue(x => x.SearchText)
- .Throttle(TimeSpan.FromMilliseconds(800))
- .ObserveOn(RxApp.MainThreadScheduler)
- .Subscribe(DoSearch!);
- this.WhenPropertyChanged(x => x.SelectedCreator).Subscribe(async x =>
- {
- if (x.Value is null) return;
- try
- {
- var vm = _dialogService.CreateViewModel<CreatorPostsViewModel>();
- vm.Creator = x.Value.Creator;
- await _dialogService.ShowDialogAsync(this, vm);
- }
- catch (Exception ex)
- {
- Manager
- .CreateMessage()
- .Accent("#1751C3")
- .Animates(true)
- .Background("#333")
- .HasBadge("Error")
- .HasMessage(ex.Message)
- .Dismiss().WithDelay(TimeSpan.FromSeconds(5))
- .Queue();
- }
- finally
- {
- Dispatcher.UIThread.Post(() => { SelectedCreator = null; });
- }
- });
- SearchText = "Rukis";
- }
- private string _searchText = string.Empty;
- private bool _isBusy = false;
- public string SearchText
- {
- get => _searchText;
- set => this.RaiseAndSetIfChanged(ref _searchText, value);
- }
- public bool IsBusy
- {
- get => _isBusy;
- set => this.RaiseAndSetIfChanged(ref _isBusy, value);
- }
- public INotificationMessageManager Manager { get; } = new NotificationMessageManager();
- private CreatorViewModel _selectedAlbum;
- public ObservableCollection<CreatorViewModel> SearchResults { get; } = new();
- public CreatorViewModel? SelectedCreator
- {
- get => _selectedAlbum;
- set => this.RaiseAndSetIfChanged(ref _selectedAlbum, value);
- }
- private async void DoSearch(string s)
- {
- _cancellationTokenSource?.Cancel();
- _cancellationTokenSource = new CancellationTokenSource();
- var cancellationToken = _cancellationTokenSource.Token;
- IsBusy = true;
- SearchResults.Clear();
- if (!string.IsNullOrWhiteSpace(s))
- {
- try
- {
- var creators = await Creator.SearchAsync(s, false);
- foreach (var creator in creators)
- {
- var vm = new CreatorViewModel(creator);
- SearchResults.Add(vm);
- }
- if (!cancellationToken.IsCancellationRequested)
- {
- LoadAvatars(cancellationToken);
- }
- }
- catch (Exception ex)
- {
- Manager
- .CreateMessage()
- .Accent("#1751C3")
- .Animates(true)
- .Background("#333")
- .HasBadge("Error")
- .HasMessage(ex.Message)
- .Dismiss().WithDelay(TimeSpan.FromSeconds(5))
- .Queue();
- }
- }
- IsBusy = false;
- }
- public async Task ReloadCreatorsSource(object? sender, PullGestureEndedEventArgs e)
- {
- if (IsBusy || e.PullDirection != PullDirection.TopToBottom) return;
- _cancellationTokenSource?.Cancel();
- _cancellationTokenSource = new CancellationTokenSource();
- var cancellationToken = _cancellationTokenSource.Token;
- IsBusy = true;
- SearchResults.Clear();
- if (!string.IsNullOrWhiteSpace(SearchText))
- {
- try
- {
- var creators = await Creator.SearchAsync(SearchText, true);
- foreach (var creator in creators)
- {
- var vm = new CreatorViewModel(creator);
- SearchResults.Add(vm);
- }
- if (!cancellationToken.IsCancellationRequested)
- {
- LoadAvatars(cancellationToken);
- }
- }
- catch (Exception ex)
- {
- Manager
- .CreateMessage()
- .Accent("#1751C3")
- .Animates(true)
- .Background("#333")
- .HasBadge("Error")
- .HasMessage(ex.Message)
- .Dismiss().WithDelay(TimeSpan.FromSeconds(5))
- .Queue();
- }
- }
- IsBusy = false;
- }
- private async void LoadAvatars(CancellationToken cancellationToken)
- {
- foreach (var creator in SearchResults.ToList())
- {
- await creator.LoadAvatar();
- if (cancellationToken.IsCancellationRequested)
- {
- return;
- }
- }
- }
- }
- }
|