using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using XFEWarframe.Player.Views.Dialogs; using XFEWarframe.Player.Models; using XFEWarframe.Player.Views; namespace XFEWarframe.Player.ViewModels; public partial class MainPageViewModel : PlayerViewModelBase { private readonly DispatcherTimer timer = new() { Interval = TimeSpan.FromSeconds(3) }; private bool refreshing; public MainPageViewModel() => timer.Tick += UpdateStatus; public void StartUpdating() => timer.Start(); public void StopUpdating() => timer.Stop(); private async void UpdateStatus(object? sender, object args) { if (refreshing || !State.IsIdle || Lifetime.IsCancellationRequested) return; refreshing = true; try { Service.RefreshSummary(); await Service.ProbeServerAsync(); } catch (Exception) { /* The shared state already carries the connection failure. */ } finally { refreshing = false; } } public PlayerAccountState Account => App.Accounts.State; public IReadOnlyList Languages { get; } = [new("简体中文", "zh"), new("English", "en"), new("繁體中文", "tc"), new("日本語", "ja")]; [ObservableProperty] public partial LanguageOption? SelectedLanguage { get; set; } partial void OnSelectedLanguageChanged(LanguageOption? value) { if (value is not null) State.Language = value.Code; } protected override async Task LoadAsync(object? parameter) { await base.LoadAsync(parameter); try { await App.Accounts.InitializeAsync(); if (Account.IsLoggedIn) await App.Accounts.RefreshAsync(); } catch (Exception) { /* The launcher remains available while the account server is offline. */ } SelectedLanguage = Languages.FirstOrDefault(x => x.Code == State.Language) ?? Languages[0]; } [RelayCommand] private Task PlayAsync() => RunAsync(async () => { if (State.AutoLoginToGame && !Account.IsLoggedIn) { await App.Accounts.InitializeAsync(); if (!Account.IsLoggedIn) { var result = await Interaction.ShowAsync(new LoginDialog()); if (result == ContentDialogResult.Secondary) { if (await Interaction.ShowAsync(new RegisterDialog()) == ContentDialogResult.Primary) Notify("注册成功,请登录你的账号"); return; } if (result != ContentDialogResult.Primary || !Account.IsLoggedIn) return; } } await Service.PlayAsync(); }); [RelayCommand] private Task RefreshServerAsync() => RunAsync(async () => { await Service.ProbeServerAsync(); await App.Accounts.InitializeAsync(); if (Account.IsLoggedIn) await App.Accounts.RefreshAsync(); }); [RelayCommand] private void OpenInstall() => Navigation.NavigateTo(); [RelayCommand] private void OpenAccount() => Navigation.NavigateTo(); [RelayCommand] private void OpenInventory() => Navigation.NavigateTo(); [RelayCommand] private void Cancel() => Service.Cancel(); }