XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using Microsoft.UI.Xaml.Automation;
using Windows.Storage.Pickers;
using XFEWorkflow.Abstractions;
using XFEWorkflow.Controls;
using XFEWorkflow.Services;

namespace XFEWorkflow.Views;

public sealed partial class SpacePage
{
    private async Task<bool> RequireLoginAsync() => Services.Space.User is not null || await AccountDialog.ShowAsync(XamlRoot);
    private async Task UploadPluginAsync()
    {
        if (!await RequireLoginAsync() || await PickAsync(".xfeplugin") is not { } file) return;
        await Services.Space.UploadAsync(file, UploadProgress());
        await ShowMineAsync(false); Status.Text = "插件已提交审核,可在我的发布中查看状态";
    }
    private IProgress<double> UploadProgress() => new Progress<double>(p => { if (loaded) Status.Text = $"上传 {p:P0}"; });
    private async Task ShowMineAsync(bool workflow)
    {
        resourceKind.SelectedIndex = workflow ? 1 : 0; sections.SelectedItem = mineTab; page = 0; query.Text = ""; stateFilter.SelectedIndex = 0;
        await ReloadAsync();
    }
    private async Task UploadWorkflowAsync()
    {
        if (!await RequireLoginAsync()) return;
        var available = await Services.Workflows.ListAsync();
        WorkflowDefinition? chosen = null;
        var selection = new ComboBox { Header = "本地工作流", ItemsSource = available, DisplayMemberPath = "Name", HorizontalAlignment = HorizontalAlignment.Stretch };
        var name = new TextBox { Header = "发布名称", MaxLength = 150 };
        var version = new TextBox { Header = "发布版本", Text = "1.0.0", PlaceholderText = "1.0.0" };
        var description = new TextBox { Header = "介绍", AcceptsReturn = true, TextWrapping = TextWrapping.Wrap, MaxLength = 10000, MaxHeight = 130 };
        var source = Caption("选择本地工作流,或导入 .xfeworkflow / .json 文件。每个发布版本不可覆盖。" );
        var error = new InfoBar { Severity = InfoBarSeverity.Error, IsClosable = true };
        var content = new StackPanel { Spacing = 12, MinWidth = Math.Min(420, Math.Max(240, XamlRoot.Size.Width - 100)) };
        var dialog = new ContentDialog { Title = "上传工作流", Content = new ScrollViewer { Content = content, MaxHeight = 520, HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled }, PrimaryButtonText = "提交审核", CloseButtonText = "取消", XamlRoot = XamlRoot, IsPrimaryButtonEnabled = false };
        AutomationProperties.SetAutomationId(dialog, "SpaceWorkflowUploadDialog");
        AutomationProperties.SetAutomationId(version, "SpaceWorkflowVersion");
        void Validate() => dialog.IsPrimaryButtonEnabled = chosen is not null && !string.IsNullOrWhiteSpace(name.Text) && SemanticVersion.TryParse(version.Text.Trim(), out _);
        void Choose(WorkflowDefinition workflow) { chosen = workflow; name.Text = workflow.Name; description.Text = workflow.Description; source.Text = "工作流 ID:" + workflow.Id; Validate(); }
        selection.SelectionChanged += (_, _) => { if (selection.SelectedItem is WorkflowDefinition workflow) Choose(workflow); };
        name.TextChanged += (_, _) => Validate(); version.TextChanged += (_, _) => Validate();
        var browse = new Button { Content = "从文件选择…" }; AutomationProperties.SetAutomationId(browse, "SpaceWorkflowBrowse");
        browse.Click += async (_, _) =>
        {
            browse.IsEnabled = false;
            try
            {
                var picker = new FileOpenPicker(); picker.FileTypeFilter.Add(".xfeworkflow"); picker.FileTypeFilter.Add(".json");
                WinRT.Interop.InitializeWithWindow.Initialize(picker, WinRT.Interop.WindowNative.GetWindowHandle(App.MainWindow));
                if (await picker.PickSingleFileAsync() is { } file)
                { var workflow = await Task.Run(() => WorkflowPackage.Inspect(file.Path)); selection.SelectedItem = null; Choose(workflow); error.IsOpen = false; }
            }
            catch (Exception ex) { error.Message = Services.Redactor.Redact(ex.Message); error.IsOpen = true; }
            finally { browse.IsEnabled = true; }
        };
        foreach (var item in new UIElement[] { selection, browse, source, name, version, description, error }) content.Children.Add(item);
        if (available.Count > 0) selection.SelectedIndex = 0;
        if (await dialog.ShowAsync() != ContentDialogResult.Primary || chosen is null) return;
        var publication = WorkflowJson.Clone(chosen); publication.Name = name.Text.Trim(); publication.Description = description.Text;
        await Services.Space.UploadWorkflowAsync(publication, version.Text.Trim(), UploadProgress());
        await ShowMineAsync(true); Status.Text = "工作流已提交审核,可在我的发布中查看状态";
    }
    private async Task ReviewAsync(SpaceCardModel card, string decision)
    {
        var reason = new TextBox { Header = "处理说明", AcceptsReturn = true, TextWrapping = TextWrapping.Wrap, MinWidth = Math.Min(360, Math.Max(240, XamlRoot.Size.Width - 100)), MaxLength = 2000 };
        var dialog = new ContentDialog { Title = decision == "publish" ? "审核上架" : decision == "reject" ? "拒绝发布" : "下架此版本", Content = reason, PrimaryButtonText = "确认提交", CloseButtonText = "取消", XamlRoot = XamlRoot };
        AutomationProperties.SetAutomationId(dialog, "SpaceReviewDialog");
        if (await dialog.ShowAsync() != ContentDialogResult.Primary) return;
        await Services.Space.RequestAsync(mode == "mine" ? card.IsWorkflow ? "workflows/withdraw" : "plugins/withdraw" : "reviews/decide", new() { ["kind"] = card.Kind, ["id"] = card.Id, ["decision"] = decision, ["reason"] = reason.Text });
        await ReloadAsync();
    }
}