using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using XFEWarframe.Player.Models;
using XFEWarframe.Player.Views;
using XFEWarframe.Desktop.Editing;
using Newtonsoft.Json.Linq;
namespace XFEWarframe.Player.ViewModels;
public partial class InventoryPageViewModel : PlayerViewModelBase
{
public PlayerAccountState Account => App.Accounts.State;
public ObservableCollection<InventoryRow> Items { get; } = [];
public ObservableCollection<InventoryCategory> Categories { get; } = [new("", "全部物品")];
public IReadOnlyList<WalletCurrency> Currencies { get; } = [new("PremiumCredits", "白金"), new("RegularCredits", "星币"), new("FusionPoints", "内融核心"), new("PrimeTokens", "御品阿耶"), new("CrewShipFusionPoints", "航道星舰融合点 / 狄拉克")];
[ObservableProperty] public partial WalletCurrency? Currency { get; set; }
[ObservableProperty] public partial InventoryCategory? Category { get; set; }
[ObservableProperty] public partial string Query { get; set; } = "";
[ObservableProperty] public partial double Amount { get; set; } = 1000;
[ObservableProperty] public partial string Summary { get; set; } = "登录后查看你的仓库";
[ObservableProperty] public partial bool HasMore { get; set; }
[ObservableProperty] public partial bool HasPrevious { get; set; }
private int offset;
public Microsoft.UI.Xaml.Visibility LoadingVisibility => IsBusy ? Microsoft.UI.Xaml.Visibility.Visible : Microsoft.UI.Xaml.Visibility.Collapsed;
public OverviewActionsModel OverviewActions { get; }
private bool attached;
public InventoryPageViewModel()
{
PropertyChanged += (_, args) => { if (args.PropertyName == nameof(IsBusy)) OnPropertyChanged(nameof(LoadingVisibility)); };
OverviewActions = new(_ => App.Accounts.ReadEditorAsync("progress", "", null, 0), async (target, body) =>
{
if (target.Server != State.ServerUrl || target.AccountId != Account.AccountId || !Account.IsLoggedIn) throw new InvalidOperationException("当前账号或服务器已变化。");
await App.Accounts.SaveEditorAsync(body);
}, Notify);
OverviewActions.AppliedAsync = () => RunAsync(async () => { await App.Accounts.RefreshAsync(); await LoadInventoryAsync(); });
}
public void Attach()
{
if (attached) return; attached = true;
Account.PropertyChanged += AccountChanged;
}
public void Detach() { attached = false; Account.PropertyChanged -= AccountChanged; OverviewActions.CancelPending(); }
private void ResetOverview() => OverviewActions.Reset(Account.IsLoggedIn && Account.EditingEnabled ? new(State.ServerUrl, Account.AccountId, Account.DisplayName) : null);
private async void AccountChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName is not (nameof(PlayerAccountState.EditingRevision) or nameof(PlayerAccountState.IsLoggedIn) or nameof(PlayerAccountState.AccountId))) return;
ResetOverview();
if (Account.IsLoggedIn && Account.EditingEnabled) await OverviewActions.RefreshAsync();
}
protected override async Task LoadAsync(object? parameter)
{
await base.LoadAsync(parameter);
Currency ??= Currencies[0]; Category ??= Categories[0];
ResetOverview();
if (Account.IsLoggedIn) await LoadInventoryAsync();
await OverviewActions.RefreshAsync();
}
private async Task LoadInventoryAsync()
{
var selected = Category?.Key ?? "";
var result = await App.Accounts.InventoryAsync(Query.Trim(), selected, offset);
Items.Clear();
foreach (var item in result.Items) Items.Add(new(item.Name, InventoryCategory.Label(item.Category), item.Type,
string.Join(" · ", new[] { item.Xp > 0 ? $"经验 {item.Xp:N0}" : item.Id.Length > 0 ? "已拥有装备" : "仓库物品", item.Tags }.Where(x => !string.IsNullOrEmpty(x))), item.Count.ToString("N0")));
Categories.Clear(); Categories.Add(new("", "全部物品"));
foreach (var value in result.Categories) Categories.Add(new(value, InventoryCategory.Label(value)));
Category = Categories.FirstOrDefault(x => x.Key == selected) ?? Categories[0];
HasPrevious = offset > 0; HasMore = offset + result.Items.Length < result.Total;
Summary = result.Total == 0 ? "没有匹配的物品。新账号可先进入游戏领取初始装备。" : $"共 {result.Total:N0} 项 · 第 {offset / 100 + 1} 页";
}
[RelayCommand] private Task SearchAsync() => RunAsync(async () => { offset = 0; await LoadInventoryAsync(); });
[RelayCommand] private Task NextAsync() => RunAsync(async () => { if (HasMore) { offset += 100; await LoadInventoryAsync(); } });
[RelayCommand] private Task PreviousAsync() => RunAsync(async () => { offset = Math.Max(0, offset - 100); await LoadInventoryAsync(); });
[RelayCommand] private Task GrantAsync() => RunAsync(async () =>
{
if (!double.IsFinite(Amount) || Amount != Math.Truncate(Amount) || Amount is < -10_000_000 or > 10_000_000 or 0) throw new InvalidOperationException("请输入 -10,000,000 至 10,000,000 的非零整数。");
var currency = Currency ?? throw new InvalidOperationException("请选择货币类型。");
if (Amount < 0 && !await Interaction.ConfirmAsync("减少货币", $"将从当前账号扣除 {-Amount:N0} {currency.Name}。")) return;
await App.Accounts.GrantAsync(currency.Key, (long)Amount);
Notify($"已调整 {Amount:+#,##0;-#,##0} {currency.Name}");
});
[RelayCommand] private void OpenAccount() => Navigation.NavigateTo<AccountPage>();
}
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using XFEWarframe.Player.Models;
using XFEWarframe.Player.Views;
using XFEWarframe.Desktop.Editing;
using Newtonsoft.Json.Linq;
namespace XFEWarframe.Player.ViewModels;
public partial class InventoryPageViewModel : PlayerViewModelBase
{
public PlayerAccountState Account => App.Accounts.State;
public ObservableCollection<InventoryRow> Items { get; } = [];
public ObservableCollection<InventoryCategory> Categories { get; } = [new("", "全部物品")];
public IReadOnlyList<WalletCurrency> Currencies { get; } = [new("PremiumCredits", "白金"), new("RegularCredits", "星币"), new("FusionPoints", "内融核心"), new("PrimeTokens", "御品阿耶"), new("CrewShipFusionPoints", "航道星舰融合点 / 狄拉克")];
[ObservableProperty] public partial WalletCurrency? Currency { get; set; }
[ObservableProperty] public partial InventoryCategory? Category { get; set; }
[ObservableProperty] public partial string Query { get; set; } = "";
[ObservableProperty] public partial double Amount { get; set; } = 1000;
[ObservableProperty] public partial string Summary { get; set; } = "登录后查看你的仓库";
[ObservableProperty] public partial bool HasMore { get; set; }
[ObservableProperty] public partial bool HasPrevious { get; set; }
private int offset;
public Microsoft.UI.Xaml.Visibility LoadingVisibility => IsBusy ? Microsoft.UI.Xaml.Visibility.Visible : Microsoft.UI.Xaml.Visibility.Collapsed;
public OverviewActionsModel OverviewActions { get; }
private bool attached;
public InventoryPageViewModel()
{
PropertyChanged += (_, args) => { if (args.PropertyName == nameof(IsBusy)) OnPropertyChanged(nameof(LoadingVisibility)); };
OverviewActions = new(_ => App.Accounts.ReadEditorAsync("progress", "", null, 0), async (target, body) =>
{
if (target.Server != State.ServerUrl || target.AccountId != Account.AccountId || !Account.IsLoggedIn) throw new InvalidOperationException("当前账号或服务器已变化。");
await App.Accounts.SaveEditorAsync(body);
}, Notify);
OverviewActions.AppliedAsync = () => RunAsync(async () => { await App.Accounts.RefreshAsync(); await LoadInventoryAsync(); });
}
public void Attach()
{
if (attached) return; attached = true;
Account.PropertyChanged += AccountChanged;
}
public void Detach() { attached = false; Account.PropertyChanged -= AccountChanged; OverviewActions.CancelPending(); }
private void ResetOverview() => OverviewActions.Reset(Account.IsLoggedIn && Account.EditingEnabled ? new(State.ServerUrl, Account.AccountId, Account.DisplayName) : null);
private async void AccountChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName is not (nameof(PlayerAccountState.EditingRevision) or nameof(PlayerAccountState.IsLoggedIn) or nameof(PlayerAccountState.AccountId))) return;
ResetOverview();
if (Account.IsLoggedIn && Account.EditingEnabled) await OverviewActions.RefreshAsync();
}
protected override async Task LoadAsync(object? parameter)
{
await base.LoadAsync(parameter);
Currency ??= Currencies[0]; Category ??= Categories[0];
ResetOverview();
if (Account.IsLoggedIn) await LoadInventoryAsync();
await OverviewActions.RefreshAsync();
}
private async Task LoadInventoryAsync()
{
var selected = Category?.Key ?? "";
var result = await App.Accounts.InventoryAsync(Query.Trim(), selected, offset);
Items.Clear();
foreach (var item in result.Items) Items.Add(new(item.Name, InventoryCategory.Label(item.Category), item.Type,
string.Join(" · ", new[] { item.Xp > 0 ? $"经验 {item.Xp:N0}" : item.Id.Length > 0 ? "已拥有装备" : "仓库物品", item.Tags }.Where(x => !string.IsNullOrEmpty(x))), item.Count.ToString("N0")));
Categories.Clear(); Categories.Add(new("", "全部物品"));
foreach (var value in result.Categories) Categories.Add(new(value, InventoryCategory.Label(value)));
Category = Categories.FirstOrDefault(x => x.Key == selected) ?? Categories[0];
HasPrevious = offset > 0; HasMore = offset + result.Items.Length < result.Total;
Summary = result.Total == 0 ? "没有匹配的物品。新账号可先进入游戏领取初始装备。" : $"共 {result.Total:N0} 项 · 第 {offset / 100 + 1} 页";
}
[RelayCommand] private Task SearchAsync() => RunAsync(async () => { offset = 0; await LoadInventoryAsync(); });
[RelayCommand] private Task NextAsync() => RunAsync(async () => { if (HasMore) { offset += 100; await LoadInventoryAsync(); } });
[RelayCommand] private Task PreviousAsync() => RunAsync(async () => { offset = Math.Max(0, offset - 100); await LoadInventoryAsync(); });
[RelayCommand] private Task GrantAsync() => RunAsync(async () =>
{
if (!double.IsFinite(Amount) || Amount != Math.Truncate(Amount) || Amount is < -10_000_000 or > 10_000_000 or 0) throw new InvalidOperationException("请输入 -10,000,000 至 10,000,000 的非零整数。");
var currency = Currency ?? throw new InvalidOperationException("请选择货币类型。");
if (Amount < 0 && !await Interaction.ConfirmAsync("减少货币", $"将从当前账号扣除 {-Amount:N0} {currency.Name}。")) return;
await App.Accounts.GrantAsync(currency.Key, (long)Amount);
Notify($"已调整 {Amount:+#,##0;-#,##0} {currency.Name}");
});
[RelayCommand] private void OpenAccount() => Navigation.NavigateTo<AccountPage>();
}