121 lines
3.7 KiB
C#
121 lines
3.7 KiB
C#
using System;
|
|
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 Flawless.Client.ViewModels.ModalBox;
|
|
using Flawless.Client.Views.ModalBox;
|
|
using ReactiveUI.SourceGenerators;
|
|
using Ursa.Controls;
|
|
|
|
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, true);
|
|
}
|
|
|
|
private async Task LoadDataAsync(RepositoryModel repo, ulong issueId, bool quitIfFailed)
|
|
{
|
|
using var _ = UIHelper.MakeLoading("Fetching comments from server...");
|
|
if (!await _service.UpdateIssueDetailsFromServerAsync(repo, issueId, false))
|
|
{
|
|
if (quitIfFailed) await NavigateBackAsync();
|
|
UIHelper.NotifyError("Operation failed", "Can not load issue...");
|
|
return;
|
|
}
|
|
|
|
CurrentIssue = repo.Issues.First(x => x.Id == issueId);
|
|
}
|
|
|
|
[ReactiveCommand]
|
|
private Task RefreshIssueAsync() => LoadDataAsync(_repo, CurrentIssue!.Id, false);
|
|
|
|
[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 void MarkAsReplayTo(RepositoryModel.Comment? comment)
|
|
{
|
|
if (CurrentIssue == null) return;
|
|
ReplyTo = comment;
|
|
}
|
|
|
|
[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()
|
|
{
|
|
if (CurrentIssue == null) return;
|
|
|
|
var opt = UIHelper.DefaultOverlayDialogOptionsYesNo();
|
|
var vm = new IssueEditDialogViewModel(CurrentIssue);
|
|
|
|
var result = await OverlayDialog.ShowModal<IssueDetailEditView, IssueEditDialogViewModel>(
|
|
vm, AppDefaultValues.HostId, opt);
|
|
|
|
if (result == DialogResult.Yes)
|
|
{
|
|
using var l = UIHelper.MakeLoading("Updating issue...");
|
|
var tags = vm.Tag?.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
|
|
|
// 调用服务端更新接口
|
|
await RepositoryService.C.UpdateIssueAsync(
|
|
_repo, CurrentIssue.Id,
|
|
vm.Title, vm.Description, tags
|
|
);
|
|
|
|
}
|
|
}
|
|
|
|
[ReactiveCommand]
|
|
private Task NavigateBackAsync() => HostScreen.Router.NavigateBack.Execute().ToTask();
|
|
} |