Use AvaloniaList properties

This commit is contained in:
Mbucari 2023-03-28 13:29:07 -06:00
parent 2afb5365dd
commit e365ba7296

View File

@ -6,7 +6,7 @@ using Avalonia.Platform.Storage;
using ReactiveUI; using ReactiveUI;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.Specialized;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -19,7 +19,6 @@ namespace LibationAvalonia.Dialogs
public class AccountDto : ViewModels.ViewModelBase public class AccountDto : ViewModels.ViewModelBase
{ {
private string _accountId; private string _accountId;
private Locale _selectedLocale;
public IReadOnlyList<Locale> Locales => AccountsDialog.Locales; public IReadOnlyList<Locale> Locales => AccountsDialog.Locales;
public bool LibraryScan { get; set; } = true; public bool LibraryScan { get; set; } = true;
public string AccountId public string AccountId
@ -31,17 +30,19 @@ namespace LibationAvalonia.Dialogs
this.RaisePropertyChanged(nameof(IsDefault)); this.RaisePropertyChanged(nameof(IsDefault));
} }
} }
public Locale SelectedLocale
{ public Locale SelectedLocale { get; set; }
get => _selectedLocale;
set
{
this.RaiseAndSetIfChanged(ref _selectedLocale, value);
this.RaisePropertyChanged(nameof(IsDefault));
}
}
public string AccountName { get; set; } public string AccountName { get; set; }
public bool IsDefault => string.IsNullOrEmpty(AccountId) && SelectedLocale is null; public bool IsDefault => string.IsNullOrEmpty(AccountId);
public AccountDto() { }
public AccountDto(Account account)
{
LibraryScan = account.LibraryScan;
AccountId = account.AccountId;
SelectedLocale = Locales.Single(l => l.Name == account.Locale.Name);
AccountName = account.AccountName;
}
} }
private static string GetAudibleCliAppDataPath() private static string GetAudibleCliAppDataPath()
@ -56,58 +57,41 @@ namespace LibationAvalonia.Dialogs
// here: copy strings and dispose of persister // here: copy strings and dispose of persister
// only persist in 'save' step // only persist in 'save' step
using var persister = AudibleApiStorage.GetAccountsSettingsPersister(); using var persister = AudibleApiStorage.GetAccountsSettingsPersister();
var accounts = persister.AccountsSettings.Accounts;
if (accounts.Any()) Accounts.CollectionChanged += Accounts_CollectionChanged;
{ Accounts.AddRange(persister.AccountsSettings.Accounts.Select(a => new AccountDto(a)));
foreach (var account in accounts)
AddAccountToGrid(account);
}
DataContext = this; DataContext = this;
addBlankAccount(); addBlankAccount();
} }
private void addBlankAccount()
{
var newBlank = new AccountDto(); private void Accounts_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
newBlank.PropertyChanged += AccountDto_PropertyChanged; {
Accounts.Insert(Accounts.Count, newBlank); if (e.Action is NotifyCollectionChangedAction.Add && e.NewItems?.Count > 0)
{
foreach (var newItem in e.NewItems.OfType<AccountDto>())
newItem.PropertyChanged += AccountDto_PropertyChanged;
}
else if (e.Action is NotifyCollectionChangedAction.Remove && e.OldItems?.Count > 0)
{
foreach (var oldItem in e.OldItems.OfType<AccountDto>())
oldItem.PropertyChanged -= AccountDto_PropertyChanged;
}
} }
private void AddAccountToGrid(Account account) private void addBlankAccount() => Accounts.Insert(Accounts.Count, new AccountDto());
{
AccountDto accountDto = new()
{
LibraryScan = account.LibraryScan,
AccountId = account.AccountId,
SelectedLocale = Locales.Single(l => l.Name == account.Locale.Name),
AccountName = account.AccountName,
};
accountDto.PropertyChanged += AccountDto_PropertyChanged;
//ObservableCollection doesn't fire CollectionChanged on Add, so use Insert instead
Accounts.Insert(Accounts.Count, accountDto);
}
private void AccountDto_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) private void AccountDto_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{ {
if (Accounts.Any(a => a.IsDefault)) if (!Accounts.Any(a => a.IsDefault))
return;
addBlankAccount(); addBlankAccount();
} }
public void DeleteButton_Clicked(object sender, Avalonia.Interactivity.RoutedEventArgs e) public void DeleteButton_Clicked(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{ {
if (e.Source is Button expBtn && expBtn.DataContext is AccountDto acc) if (e.Source is Button expBtn && expBtn.DataContext is AccountDto acc)
{
var index = Accounts.IndexOf(acc);
if (index < 0) return;
acc.PropertyChanged -= AccountDto_PropertyChanged;
Accounts.Remove(acc); Accounts.Remove(acc);
} }
}
public async void ImportButton_Clicked(object sender, Avalonia.Interactivity.RoutedEventArgs e) public async void ImportButton_Clicked(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{ {
@ -151,7 +135,7 @@ namespace LibationAvalonia.Dialogs
persister.AccountsSettings.Add(account); persister.AccountsSettings.Add(account);
AddAccountToGrid(account); Accounts.Add(new AccountDto(account));
} }
catch (Exception ex) catch (Exception ex)
{ {