Skip to content

Commit

Permalink
Fix AccountService.Account Reference Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
natsurainko committed Jun 18, 2024
1 parent 818266b commit 3869d41
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 20 deletions.
10 changes: 5 additions & 5 deletions Natsurainko.FluentLauncher/Services/Accounts/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ public void AddAccount(Account account)
_accounts.Add(account);
}

public bool RemoveAccount(Account account)
public bool RemoveAccount(Account account, bool dontActive = false)
{
bool result = _accounts.Remove(account);

if (ActiveAccount == account)
if (ActiveAccount == account && !dontActive)
this.ActivateAccount(_accounts.Count != 0 ? _accounts[0] : null);

return result;
Expand Down Expand Up @@ -182,11 +182,11 @@ public async Task RefreshAccount(Account account)
throw new Exception($"{account} does not exist in AccountService");

bool isActiveAccount = ActiveAccount == oldAccount;
RemoveAccount(oldAccount);
AddAccount(account);
RemoveAccount(oldAccount, true);
AddAccount(refreshedAccount);

if (isActiveAccount)
ActivateAccount(account);
ActivateAccount(refreshedAccount);

// Cache skin
await Task.Run(() => _skinCacheService.TryCacheSkin(refreshedAccount));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.UI.Xaml.Media.Imaging;
using Nrk.FluentCore.Authentication;
using Nrk.FluentCore.Management.Downloader.Data;
using Nrk.FluentCore.Resources;
using Nrk.FluentCore.Utils;
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public Account Account
set { SetValue(AccountProperty, value); }
}

// Using a DependencyProperty as the backing store for Account. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AccountProperty =
DependencyProperty.Register("Account", typeof(Account), typeof(SkinHeadControlBehavior), new PropertyMetadata(null, OnAccountChanged));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal partial class SwitchAccountDialogViewModel : SettingsViewModelBase, ISe

#endregion

public ReadOnlyObservableCollection<Account> Accounts { get; init; }
public ReadOnlyObservableCollection<Account> Accounts { get; set; }

private readonly AccountService _accountService;

Expand Down
11 changes: 2 additions & 9 deletions Natsurainko.FluentLauncher/ViewModels/OOBE/OOBEViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace Natsurainko.FluentLauncher.ViewModels.OOBE;

internal partial class OOBEViewModel : ObservableRecipient, INavigationAware, ISettingsViewModel, IRecipient<ActiveAccountChangedMessage>
internal partial class OOBEViewModel : ObservableRecipient, INavigationAware, ISettingsViewModel
{
#region Dependencies

Expand Down Expand Up @@ -318,14 +318,7 @@ public void OpenJavaMirrorsDialog(HyperlinkButton parameter)
[NotifyCanExecuteChangedFor(nameof(NextCommand))]
private Account activeAccount;

bool processingActiveAccountChangedMessage = false;

public void Receive(ActiveAccountChangedMessage message)
{
processingActiveAccountChangedMessage = true;
ActiveAccount = message.Value;
processingActiveAccountChangedMessage = false;
}
public bool processingActiveAccountChangedMessage = false;

partial void OnActiveAccountChanged(Account value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
PrimaryButtonText="Confirm"
SecondaryButtonText="Cancel"
Style="{ThemeResource DefaultContentDialogStyle}"
Unloaded="ContentDialog_Unloaded"
mc:Ignorable="d">

<Grid>
Expand All @@ -23,7 +24,10 @@
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListView ItemsSource="{Binding Accounts}" SelectedItem="{Binding ActiveAccount, Mode=TwoWay}">
<ListView
x:Name="ListView"
ItemsSource="{Binding Accounts}"
SelectedItem="{Binding ActiveAccount, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="0,10">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ public SwitchAccountDialog()
{
this.InitializeComponent();
}

private void ContentDialog_Unloaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
ListView.ItemsSource = null; // Unload Binding to AccountService.Accounts
}
}
}
6 changes: 5 additions & 1 deletion Natsurainko.FluentLauncher/Views/OOBE/AccountPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:Natsurainko.FluentLauncher.ViewModels.OOBE"
d:DataContext="{d:DesignInstance vm:OOBEViewModel}"
Loaded="Page_Loaded"
mc:Ignorable="d">

<Grid HorizontalAlignment="Center">
Expand Down Expand Up @@ -64,7 +65,10 @@
Width="300"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<ListView ItemsSource="{Binding Accounts}" SelectedItem="{Binding ActiveAccount, Mode=TwoWay}">
<ListView
x:Name="ListView"
ItemsSource="{Binding Accounts}"
SelectedItem="{Binding ActiveAccount, Mode=TwoWay}">
<ListView.Header>
<TextBlock
x:Uid="OOBE_AccountPage_H"
Expand Down
26 changes: 26 additions & 0 deletions Natsurainko.FluentLauncher/Views/OOBE/AccountPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml.Controls;
using Natsurainko.FluentLauncher.Services.UI.Messaging;
using Natsurainko.FluentLauncher.ViewModels.OOBE;

namespace Natsurainko.FluentLauncher.Views.OOBE;

Expand All @@ -22,4 +25,27 @@ private void Grid_PointerExited(object sender, Microsoft.UI.Xaml.Input.PointerRo
var button = (Button)grid.FindName("DeleteButton");
button.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
}

private void Page_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
var vm = this.DataContext as OOBEViewModel;

WeakReferenceMessenger.Default.Register<ActiveAccountChangedMessage>(vm!, (r, m) =>
{
OOBEViewModel vm = (r as OOBEViewModel)!;
vm.processingActiveAccountChangedMessage = true;
vm.ActiveAccount = m.Value;
vm.processingActiveAccountChangedMessage = false;
});

void Page_Unloaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
WeakReferenceMessenger.Default.Unregister<ActiveAccountChangedMessage>(vm!);

ListView.ItemsSource = null; // Unload Binding to AccountService.Accounts
}

this.Unloaded += Page_Unloaded;
}
}

0 comments on commit 3869d41

Please sign in to comment.