using Newtonsoft.Json.Linq; using XFEWarframe.Desktop.Editing; namespace XFEWarframe.Player.ViewModels; public sealed class EquipmentPageViewModel : PlayerViewModelBase { public EquipmentEditorModel Editor { get; } private string? accountId; public EquipmentPageViewModel() => Editor = new( async target => { RequireOwner(target); var data = await App.Accounts.ReadEditorAsync(target.Category, "", target.Item, 0); data["permissionsRevision"] = App.Accounts.State.EditingRevision; return data; }, async (target, body) => { RequireOwner(target); return await App.Accounts.SaveEditorAsync(body); }, async target => { RequireOwner(target, requireEditing: false); var result = await App.Accounts.EditorRevisionAsync(); if (result.Permissions != App.Accounts.State.EditingRevision || result.Enabled != App.Accounts.State.EditingEnabled) await App.Accounts.RefreshAsync(); return new EquipmentRevision(result.Version, result.Permissions, result.Enabled); }, (title, message) => Interaction.ConfirmAsync(title, message), Notify, target => Navigation.NavigateTo(target)); private static void RequireOwner(EquipmentTarget target, bool requireEditing = true) { var state = App.Accounts.State; if (!state.IsLoggedIn || (requireEditing && !state.EditingEnabled) || state.AccountId != target.AccountId) throw new InvalidOperationException(UiText.Translate("管理员未允许编辑此物品。")); } protected override async Task LoadAsync(object? parameter) { if (parameter is EquipmentTarget target) { accountId = target.AccountId; await App.Accounts.InitializeAsync(); RequireOwner(target); } await Editor.LoadAsync(parameter as EquipmentTarget); } public void Attach() { Editor.Attach(); App.Accounts.State.PropertyChanged += AccountChanged; } public void Detach() { Editor.Detach(); App.Accounts.State.PropertyChanged -= AccountChanged; } private void AccountChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs args) { if (!App.Accounts.State.IsLoggedIn || App.Accounts.State.AccountId != accountId) Editor.Revoke(forgetTarget: true); else if (!App.Accounts.State.EditingEnabled) Editor.Revoke(); } public void Back() => Navigation.NavigationService.GoBack(); }