CreatorsViewModel.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Avalonia.Input;
  2. using Avalonia.Notification;
  3. using Avalonia.Threading;
  4. using DynamicData.Binding;
  5. using HanumanInstitute.MvvmDialogs;
  6. using ReactiveUI;
  7. using System;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Reactive.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using VeloeAvaloniaKemonoPartyApp.Models;
  14. namespace VeloeAvaloniaKemonoPartyApp.ViewModels
  15. {
  16. public class CreatorsViewModel : ViewModelBase
  17. {
  18. private CancellationTokenSource? _cancellationTokenSource;
  19. private readonly IDialogService _dialogService;
  20. public CreatorsViewModel(IDialogService dialogService)
  21. {
  22. _dialogService = dialogService;
  23. this.WhenAnyValue(x => x.SearchText)
  24. .Throttle(TimeSpan.FromMilliseconds(800))
  25. .ObserveOn(RxApp.MainThreadScheduler)
  26. .Subscribe(DoSearch!);
  27. this.WhenPropertyChanged(x => x.SelectedCreator).Subscribe(async x =>
  28. {
  29. if (x.Value is null) return;
  30. try
  31. {
  32. var vm = _dialogService.CreateViewModel<CreatorPostsViewModel>();
  33. vm.Creator = x.Value.Creator;
  34. await _dialogService.ShowDialogAsync(this, vm);
  35. }
  36. catch (Exception ex)
  37. {
  38. Manager
  39. .CreateMessage()
  40. .Accent("#1751C3")
  41. .Animates(true)
  42. .Background("#333")
  43. .HasBadge("Error")
  44. .HasMessage(ex.Message)
  45. .Dismiss().WithDelay(TimeSpan.FromSeconds(5))
  46. .Queue();
  47. }
  48. finally
  49. {
  50. Dispatcher.UIThread.Post(() => { SelectedCreator = null; });
  51. }
  52. });
  53. SearchText = "Rukis";
  54. }
  55. private string _searchText = string.Empty;
  56. private bool _isBusy = false;
  57. public string SearchText
  58. {
  59. get => _searchText;
  60. set => this.RaiseAndSetIfChanged(ref _searchText, value);
  61. }
  62. public bool IsBusy
  63. {
  64. get => _isBusy;
  65. set => this.RaiseAndSetIfChanged(ref _isBusy, value);
  66. }
  67. public INotificationMessageManager Manager { get; } = new NotificationMessageManager();
  68. private CreatorViewModel _selectedAlbum;
  69. public ObservableCollection<CreatorViewModel> SearchResults { get; } = new();
  70. public CreatorViewModel? SelectedCreator
  71. {
  72. get => _selectedAlbum;
  73. set => this.RaiseAndSetIfChanged(ref _selectedAlbum, value);
  74. }
  75. private async void DoSearch(string s)
  76. {
  77. _cancellationTokenSource?.Cancel();
  78. _cancellationTokenSource = new CancellationTokenSource();
  79. var cancellationToken = _cancellationTokenSource.Token;
  80. IsBusy = true;
  81. SearchResults.Clear();
  82. if (!string.IsNullOrWhiteSpace(s))
  83. {
  84. try
  85. {
  86. var creators = await Creator.SearchAsync(s, false);
  87. foreach (var creator in creators)
  88. {
  89. var vm = new CreatorViewModel(creator);
  90. SearchResults.Add(vm);
  91. }
  92. if (!cancellationToken.IsCancellationRequested)
  93. {
  94. LoadAvatars(cancellationToken);
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. Manager
  100. .CreateMessage()
  101. .Accent("#1751C3")
  102. .Animates(true)
  103. .Background("#333")
  104. .HasBadge("Error")
  105. .HasMessage(ex.Message)
  106. .Dismiss().WithDelay(TimeSpan.FromSeconds(5))
  107. .Queue();
  108. }
  109. }
  110. IsBusy = false;
  111. }
  112. public async Task ReloadCreatorsSource(object? sender, PullGestureEndedEventArgs e)
  113. {
  114. if (IsBusy || e.PullDirection != PullDirection.TopToBottom) return;
  115. _cancellationTokenSource?.Cancel();
  116. _cancellationTokenSource = new CancellationTokenSource();
  117. var cancellationToken = _cancellationTokenSource.Token;
  118. IsBusy = true;
  119. SearchResults.Clear();
  120. if (!string.IsNullOrWhiteSpace(SearchText))
  121. {
  122. try
  123. {
  124. var creators = await Creator.SearchAsync(SearchText, true);
  125. foreach (var creator in creators)
  126. {
  127. var vm = new CreatorViewModel(creator);
  128. SearchResults.Add(vm);
  129. }
  130. if (!cancellationToken.IsCancellationRequested)
  131. {
  132. LoadAvatars(cancellationToken);
  133. }
  134. }
  135. catch (Exception ex)
  136. {
  137. Manager
  138. .CreateMessage()
  139. .Accent("#1751C3")
  140. .Animates(true)
  141. .Background("#333")
  142. .HasBadge("Error")
  143. .HasMessage(ex.Message)
  144. .Dismiss().WithDelay(TimeSpan.FromSeconds(5))
  145. .Queue();
  146. }
  147. }
  148. IsBusy = false;
  149. }
  150. private async void LoadAvatars(CancellationToken cancellationToken)
  151. {
  152. foreach (var creator in SearchResults.ToList())
  153. {
  154. await creator.LoadAvatar();
  155. if (cancellationToken.IsCancellationRequested)
  156. {
  157. return;
  158. }
  159. }
  160. }
  161. }
  162. }