Files
Wino-Mail/Wino.Mail.WinUI/Dialogs/AccountReorderDialog.xaml.cs
T

55 lines
1.7 KiB
C#
Raw Normal View History

2025-11-14 18:51:48 +01:00
using System;
using System.Collections.ObjectModel;
2025-09-29 11:16:14 +02:00
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml.Controls;
using Wino.Core.Domain.Interfaces;
using Wino.Mail.WinUI;
namespace Wino.Dialogs;
public sealed partial class AccountReorderDialog : ContentDialog
{
2025-11-14 18:51:48 +01:00
public ObservableCollection<IAccountProviderDetailViewModel> Accounts { get; set; } = null!;
2025-09-29 11:16:14 +02:00
private int count;
private bool isOrdering = false;
2025-11-14 18:51:48 +01:00
private readonly IAccountService? _accountService = App.Current.Services.GetService<IAccountService>();
2025-09-29 11:16:14 +02:00
2025-11-14 18:51:48 +01:00
public AccountReorderDialog(ObservableCollection<IAccountProviderDetailViewModel>? accounts)
2025-09-29 11:16:14 +02:00
{
2025-11-14 18:51:48 +01:00
Accounts = accounts ?? throw new ArgumentNullException(nameof(accounts));
2025-09-29 11:16:14 +02:00
count = accounts.Count;
InitializeComponent();
}
private void DialogOpened(ContentDialog sender, ContentDialogOpenedEventArgs args)
{
Accounts.CollectionChanged -= AccountsChanged;
Accounts.CollectionChanged += AccountsChanged;
}
private void DialogClosed(ContentDialog sender, ContentDialogClosedEventArgs args) => Accounts.CollectionChanged -= AccountsChanged;
2025-11-14 18:51:48 +01:00
private async void AccountsChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
2025-09-29 11:16:14 +02:00
{
if (count - 1 == Accounts.Count)
isOrdering = true;
if (count == Accounts.Count && isOrdering)
{
// Order is completed. Apply changes.
var dict = Accounts.ToDictionary(a => a.StartupEntityId, a => Accounts.IndexOf(a));
2025-11-14 18:51:48 +01:00
if (_accountService != null)
await _accountService.UpdateAccountOrdersAsync(dict);
2025-09-29 11:16:14 +02:00
isOrdering = false;
}
}
}