using CommunityToolkit.Mvvm.ComponentModel; using Newtonsoft.Json.Linq; namespace XFEWarframe.Desktop.Editing; public sealed record OverviewTarget(string Server, string AccountId, string Name); public sealed record OverviewOperation(OverviewTarget Target, int Generation, string Version, EditorAction Action, IReadOnlyList Fields); /// Account-wide commands shared by the administrator and player overview. public sealed class OverviewActionsModel(Func> read, Func save, Action notify) : ObservableObject { private OverviewTarget? target; private int generation; private bool busy; private string message = ""; private IReadOnlyList actions = []; public IReadOnlyList Actions { get => actions; private set => SetProperty(ref actions, value); } public bool IsBusy { get => busy; private set => SetProperty(ref busy, value); } public string Message { get => message; private set => SetProperty(ref message, value); } public Func? AppliedAsync { get; set; } public void Reset(OverviewTarget? next) { generation++; target = next; Actions = []; Message = ""; IsBusy = false; } public void CancelPending() => Reset(null); public async Task RefreshAsync() { if (target is not { } current) return; var request = ++generation; IsBusy = true; Message = ""; try { var data = await read(current); if (request == generation && current == target) Actions = Parse(data); } catch (Exception ex) { if (request == generation) { Actions = []; Message = ex.Message; } } finally { if (request == generation) IsBusy = false; } } public async Task PrepareAsync(string key) { if (IsBusy || target is not { } current) return null; var request = generation; IsBusy = true; Message = ""; try { var data = await read(current); if (request != generation || current != target) return null; var action = Parse(data).FirstOrDefault(x => x.Key == key); if (action is null) { Actions = Parse(data); throw new InvalidOperationException(UiText.Translate("此操作的权限已撤销,请刷新总览。")); } return new(current, request, data.Value("version")!, action, action.Fields.OfType().Select(x => new EditorField(x)).ToArray()); } catch (Exception ex) { if (request == generation) Message = ex.Message; return null; } finally { if (request == generation) IsBusy = false; } } public async Task ExecuteAsync(OverviewOperation operation) { if (IsBusy) return; if (operation.Target != target || operation.Generation != generation) { Message = UiText.Translate("当前账号或权限已变化,请重新选择操作。"); return; } IsBusy = true; Message = ""; try { var values = new JObject(operation.Fields.Select(x => new JProperty(x.Key, x.Serialize()))); await save(operation.Target, new JObject { ["version"] = operation.Version, ["category"] = "progress", ["action"] = operation.Action.Key, ["values"] = values }); if (operation.Target != target || operation.Generation != generation) return; notify(operation.Action.Label + " · " + UiText.Translate("已完成")); if (AppliedAsync is not null) await AppliedAsync(); } catch (Exception ex) { if (operation.Generation == generation) Message = ex.Message; } finally { if (operation.Generation == generation) IsBusy = false; } } private static EditorAction[] Parse(JObject data) => (data["actions"] as JArray ?? []).OfType().Select(x => new EditorAction(x.Value("key")!, x.Value("label")!, x.Value("description") ?? "", (JArray)x["fields"]!, "")).ToArray(); }