123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- using Avalonia.Notification;
- using HanumanInstitute.MvvmDialogs;
- using ReactiveUI;
- using System;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Net.Http;
- using System.Reactive.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using VeloeAvaloniaKemonoPartyApp.Models;
- namespace VeloeAvaloniaKemonoPartyApp.ViewModels
- {
- public class CreatorPostsViewModel : ViewModelBase, IModalDialogViewModel, IViewClosing, IViewLoaded, ICloseable
- {
- private readonly IDialogService _dialogService;
- public event EventHandler? RequestClose;
- public bool? DialogResult => true;
- public ObservableCollection<PostViewModel> Posts { get; } = new();
- public CreatorPostsViewModel(IDialogService dialogService)
- {
- _dialogService = dialogService;
- Close = ReactiveCommand.Create(CloseImpl);
- this.WhenAnyValue(x => x.SearchText)
- .Throttle(TimeSpan.FromMilliseconds(400))
- .ObserveOn(RxApp.MainThreadScheduler)
- .Subscribe(InitPosts!);
- }
- private CancellationTokenSource? _cancellationTokenSource;
- private bool _isBusy = false;
- public bool IsBusy
- {
- get => _isBusy;
- set => this.RaiseAndSetIfChanged(ref _isBusy, value);
- }
- private string _searchText = string.Empty;
- public string SearchText
- {
- get => _searchText;
- set => this.RaiseAndSetIfChanged(ref _searchText, value);
- }
- public INotificationMessageManager Manager { get; } = new NotificationMessageManager();
- private Creator _creator;
- public Creator Creator
- {
- get => _creator;
- set
- {
- _creator = value;
- InitPosts();
- }
- }
- private async void InitPosts(string search = "")
- {
- _cancellationTokenSource?.Cancel();
- _cancellationTokenSource = new CancellationTokenSource();
- var cancellationToken = _cancellationTokenSource.Token;
- IsBusy = true;
- Posts.Clear();
- if (_creator is not null)
- {
- try
- {
- var posts = await Post.LoadPostsAsync(_creator, 0, search);
- foreach (var post in posts)
- {
- var vm = new PostViewModel(_dialogService, this, post);
- Posts.Add(vm);
- }
- if (!cancellationToken.IsCancellationRequested)
- {
- LoadAvatars(cancellationToken);
- }
- }
- catch (HttpRequestException ex)
- {
- Manager
- .CreateMessage()
- .Accent("#1751C3")
- .Animates(true)
- .Background("#333")
- .HasBadge("Error")
- .HasMessage(ex.Message)
- .Dismiss().WithDelay(TimeSpan.FromSeconds(5))
- .Queue();
- }
- }
- IsBusy = false;
- }
- public async void LoadMorePosts()
- {
- if (IsBusy) return;
- if (_cancellationTokenSource is null)
- _cancellationTokenSource = new();
- var cancellationToken = _cancellationTokenSource.Token;
- IsBusy = true;
- if (_creator is not null)
- {
- try
- {
- var startIndex = Posts.Count;
- var posts = await Post.LoadPostsAsync(_creator, startIndex, SearchText);
- foreach (var post in posts)
- {
- var vm = new PostViewModel(_dialogService, this, post);
- Posts.Add(vm);
- }
- if (!cancellationToken.IsCancellationRequested)
- {
- LoadAvatars(cancellationToken, startIndex);
- }
- }
- catch (HttpRequestException 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,int startIndex = 0)
- {
- foreach (var images in Posts.Skip(startIndex).SelectMany(x=>x.Images.Concat(x.ContentViewModels.OfType<PostImageViewModel>())).ToList())
- {
- await images.LoadAvatar();
- if (cancellationToken.IsCancellationRequested)
- {
- return;
- }
- }
- }
- public ReactiveCommand<System.Reactive.Unit, System.Reactive.Unit> Close { get; }
- public void OnLoaded()
- {
-
- }
- public void OnClosing(CancelEventArgs e)
- {
- e.Cancel = true;
- }
- private void CloseImpl()
- {
- RequestClose?.Invoke(this, EventArgs.Empty);
- }
- public async Task OnClosingAsync(CancelEventArgs e)
- {
- foreach (var post in Posts)
- {
- post.Attachments.Clear();
- post.Images.Clear();
- post.ContentViewModels.Clear();
- }
- Posts.Clear();
- GC.Collect();
- //var result = await _dialogService.ShowMessageBoxAsync(this, "Do you want to close it?", "Confirmation", MessageBoxButton.YesNo);
- _cancellationTokenSource?.Cancel();
- e.Cancel = false;
- }
- }
- }
|