using System.Linq; using System.Reactive.Threading.Tasks; using System.Threading.Tasks; using Flawless.Client; using Flawless.Client.Models; using ReactiveUI; using Flawless.Client.Service; using Flawless.Client.ViewModels; using ReactiveUI.SourceGenerators; public partial class IssueDetailViewModel : RoutableViewModelBase { [Reactive] private RepositoryModel.Issue? _currentIssue; [Reactive] private string _newComment; [Reactive] private RepositoryModel.Comment? _replyTo; private readonly RepositoryService _service; private readonly RepositoryModel _repo; public IssueDetailViewModel(IScreen screen, RepositoryModel repo, ulong issueId) : base(screen) { _repo = repo; _service = RepositoryService.Current; LoadDataAsync(repo, issueId); } private async Task LoadDataAsync(RepositoryModel repo, ulong issueId) { using var _ = UIHelper.MakeLoading("Fetching comments from server..."); if (!await _service.UpdateIssueDetailsFromServerAsync(repo, issueId)) { await NavigateBackAsync(); UIHelper.NotifyError("Operation failed", "Can not open issue..."); return; } CurrentIssue = repo.Issues.First(x => x.Id == issueId); } [ReactiveCommand] private Task RefreshIssueAsync() => LoadDataAsync(_repo, CurrentIssue!.Id); [ReactiveCommand] private async Task ToggleIssueStatusAsync() { if (CurrentIssue == null) return; using var _ = UIHelper.MakeLoading("Update issue..."); if (CurrentIssue.Closed) { if (!await _service.ReopenIssueAsync(_repo, CurrentIssue.Id)) return; } else { if (!await _service.CloseIssueAsync(_repo, CurrentIssue.Id)) return; } CurrentIssue.Closed = !CurrentIssue.Closed; } [ReactiveCommand] private async Task AddCommentAsync() { if (CurrentIssue == null) return; using var _ = UIHelper.MakeLoading("Update issue..."); if (string.IsNullOrWhiteSpace(NewComment)) { UIHelper.NotifyError("No input", "No comment has been written!"); } else { if (!await _service.AddCommentAsync(_repo, CurrentIssue.Id, NewComment, _replyTo?.Id ?? null)) return; _replyTo = null; NewComment = string.Empty; } } [ReactiveCommand] private async Task EditIssueAsync() { } [ReactiveCommand] private Task NavigateBackAsync() => HostScreen.Router.NavigateBack.Execute().ToTask(); }