1
0
2025-05-21 12:56:52 +08:00

121 lines
3.7 KiB
C#

using System;
using System.Linq;
using System.Reactive.Threading.Tasks;
using System.Threading.Tasks;
using Flawless.Client.Models;
using ReactiveUI;
using Flawless.Client.Service;
using Flawless.Client.ViewModels.ModalBox;
using Flawless.Client.Views.ModalBox;
using ReactiveUI.SourceGenerators;
using Ursa.Controls;
namespace Flawless.Client.ViewModels;
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, int issueId) : base(screen)
{
_repo = repo;
_service = RepositoryService.Current;
LoadDataAsync(repo, issueId, true);
}
private async Task LoadDataAsync(RepositoryModel repo, int issueId, bool quitIfFailed)
{
using var _ = UIHelper.MakeLoading("Fetching comments from server...");
if (!await _service.UpdateIssueDetailsFromServerAsync(repo, issueId, false))
{
if (quitIfFailed) await NavigateBackAsync();
UIHelper.NotifyError("Can not load issue...", "Operation failed");
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 comment has been written!", "No input");
}
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();
}