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

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using XFEWorkflow.Abstractions;
using XFEWorkflow.Space.Contracts;

namespace XFEWorkflow.ViewModels;

public sealed class PluginCardModel
{
    public InstalledPlugin? Installed { get; init; }
    public IReadOnlyList<InstalledPlugin> InstalledVersions { get; init; } = [];
    public PluginPublication? Publication { get; init; }
    public IReadOnlyList<PluginPublication> PublicationVersions { get; init; } = [];
    public PluginManifest Manifest => Installed?.Manifest ?? Publication!.Manifest;
    public string Name => Manifest.Name;
    public string Description => Manifest.Description;
    public string VersionLabel => "v" + Manifest.Version + " · " + (Publication?.AuthorName ?? Manifest.Author) + (InstalledVersions.Count > 1 ? $" · {InstalledVersions.Count} 个版本" : "");
    public string Capabilities => string.Join(" · ", Manifest.Capabilities.Select(x => x.Category).Distinct()
        .Concat(Manifest.Pages.Select(x => "页面:" + x.Name)));
    public string StateLabel => Installed is { } local ? local.PendingRemoval ? "等待卸载" : local.Enabled ? "已启用" : "已停用" : Publication!.Status switch { PublicationStatus.Pending => "待审核", PublicationStatus.Published => "已上架", PublicationStatus.Rejected => "已拒绝", _ => "已下架" };
    public string Glyph => Manifest.Id switch { "xfe.vision" => "\uE722", "xfe.desktop" => "\uE7F4", "xfe.ai" => "\uE734", "xfe.wecom" => "\uE8F2", "xfe.file" => "\uE8B7", "xfe.timer" => "\uE787", _ => "\uE774" };
    public string SearchText => $"{Name} {Manifest.Id} {Manifest.Author} {Description} {Capabilities} " + string.Join(" ", Manifest.Capabilities.Select(x => x.Name));
}

public sealed record RunRowModel(RunRecord Record)
{
    public string Name => Record.WorkflowName;
    public string StatusLabel => StatusText(Record.Status);
    public string DetailLabel => Record.Trigger + " · " + Record.StartedAt.ToLocalTime().ToString("MM-dd HH:mm:ss") + " · " + Duration;
    public string Duration => Record.FinishedAt is { } end ? (end - Record.StartedAt).TotalSeconds.ToString("0.0") + " 秒" : "运行中";
    public static string StatusText(RunStatus status) => status switch { RunStatus.Succeeded => "成功", RunStatus.Failed => "失败", RunStatus.Running => "运行中", RunStatus.Pending => "等待", RunStatus.Cancelled => "已取消", RunStatus.Interrupted => "已中断", _ => "已跳过" };
}

public sealed class SpaceCardModel
{
    public PluginPublication? Plugin { get; init; }
    public WorkflowPublication? Workflow { get; init; }
    public bool IsWorkflow => Workflow is not null;
    public string Kind => IsWorkflow ? "workflow" : "plugin";
    public string Id => Workflow?.Id ?? Plugin!.Id;
    public string ResourceId => Workflow?.Workflow.Id ?? Plugin!.Manifest.Id;
    public string Name => Workflow?.Workflow.Name ?? Plugin!.Manifest.Name;
    public string Description => Workflow?.Workflow.Description ?? Plugin!.Manifest.Description;
    public string Version => Workflow?.Workflow.Version ?? Plugin!.Manifest.Version;
    public string Author => Workflow?.AuthorName ?? Plugin!.AuthorName;
    public string VersionLabel => "v" + Version + " · " + Author;
    public PublicationStatus Status => Workflow?.Status ?? Plugin!.Status;
    public string StateLabel => Status switch { PublicationStatus.Pending => "待审核", PublicationStatus.Published => "已上架", PublicationStatus.Rejected => "已拒绝", _ => "已下架" };
    public string Glyph => IsWorkflow ? "\uEF90" : new PluginCardModel { Publication = Plugin }.Glyph;
    public string Capabilities => Workflow is { } flow
        ? $"工作流 · {(flow.Workflow.Kind == WorkflowKind.Graph ? "图形化" : "C# 代码")} · {flow.Workflow.PluginVersions.Count} 个插件依赖"
        : "插件 · " + new PluginCardModel { Publication = Plugin }.Capabilities;
    public DateTimeOffset SubmittedAt => Workflow?.SubmittedAt ?? Plugin!.SubmittedAt;
    public long PackageBytes => Workflow?.PackageBytes ?? Plugin!.PackageBytes;
    public string ReviewReason => Workflow?.ReviewReason ?? Plugin!.ReviewReason;
    public string SearchText => $"{Name} {ResourceId} {Description} {Author} {Capabilities}";
}