mirror of
https://github.com/ckaczor/FeedCenter.git
synced 2026-01-14 09:58:53 -05:00
129 lines
3.6 KiB
C#
129 lines
3.6 KiB
C#
using System.ComponentModel;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Input;
|
|
using FeedCenter.Accounts;
|
|
|
|
namespace FeedCenter.Options;
|
|
|
|
public partial class AccountsOptionsPanel
|
|
{
|
|
private readonly FeedCenterEntities _entities;
|
|
|
|
public AccountsOptionsPanel(Window parentWindow, FeedCenterEntities entities) : base(parentWindow, entities)
|
|
{
|
|
_entities = entities;
|
|
|
|
InitializeComponent();
|
|
}
|
|
|
|
public override string CategoryName => Properties.Resources.optionCategoryAccounts;
|
|
|
|
public override void LoadPanel()
|
|
{
|
|
base.LoadPanel();
|
|
|
|
var collectionViewSource = new CollectionViewSource { Source = _entities.Accounts };
|
|
collectionViewSource.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
|
|
collectionViewSource.IsLiveSortingRequested = true;
|
|
collectionViewSource.View.Filter = item =>
|
|
{
|
|
if (item is not Account account)
|
|
return false;
|
|
|
|
// Filter out local accounts
|
|
return account.Type != AccountType.Local;
|
|
};
|
|
|
|
AccountDataGrid.ItemsSource = collectionViewSource.View;
|
|
AccountDataGrid.SelectedIndex = 0;
|
|
|
|
SetAccountButtonStates();
|
|
}
|
|
|
|
private void SetAccountButtonStates()
|
|
{
|
|
AddAccountButton.IsEnabled = true;
|
|
|
|
EditAccountButton.IsEnabled = AccountDataGrid.SelectedItem != null;
|
|
DeleteAccountButton.IsEnabled = AccountDataGrid.SelectedItem != null;
|
|
}
|
|
|
|
private void AddAccount()
|
|
{
|
|
var account = new Account(AccountType.Miniflux);
|
|
|
|
var accountWindow = new AccountWindow(_entities);
|
|
|
|
var result = accountWindow.Display(account, Window.GetWindow(this), true);
|
|
|
|
if (!result.HasValue || !result.Value)
|
|
return;
|
|
|
|
AccountDataGrid.SelectedItem = account;
|
|
|
|
SetAccountButtonStates();
|
|
}
|
|
|
|
private void EditSelectedAccount()
|
|
{
|
|
if (AccountDataGrid.SelectedItem == null)
|
|
return;
|
|
|
|
var account = (Account) AccountDataGrid.SelectedItem;
|
|
|
|
var accountWindow = new AccountWindow(_entities);
|
|
|
|
accountWindow.Display(account, Window.GetWindow(this), false);
|
|
}
|
|
|
|
private void DeleteSelectedAccount()
|
|
{
|
|
var account = (Account) AccountDataGrid.SelectedItem;
|
|
|
|
if (MessageBox.Show(ParentWindow, string.Format(Properties.Resources.ConfirmDeleteAccount, account.Name),
|
|
Properties.Resources.ConfirmDeleteTitle, MessageBoxButton.YesNo, MessageBoxImage.Question,
|
|
MessageBoxResult.No) == MessageBoxResult.No)
|
|
return;
|
|
|
|
var index = AccountDataGrid.SelectedIndex;
|
|
|
|
if (index == AccountDataGrid.Items.Count - 1)
|
|
AccountDataGrid.SelectedIndex = index - 1;
|
|
else
|
|
AccountDataGrid.SelectedIndex = index + 1;
|
|
|
|
_entities.SaveChanges(() => _entities.Accounts.Remove(account));
|
|
|
|
SetAccountButtonStates();
|
|
}
|
|
|
|
private void HandleAddAccountButtonClick(object sender, RoutedEventArgs e)
|
|
{
|
|
AddAccount();
|
|
}
|
|
|
|
private void HandleEditAccountButtonClick(object sender, RoutedEventArgs e)
|
|
{
|
|
EditSelectedAccount();
|
|
}
|
|
|
|
private void HandleDeleteAccountButtonClick(object sender, RoutedEventArgs e)
|
|
{
|
|
DeleteSelectedAccount();
|
|
}
|
|
|
|
private void HandleAccountDataGridSelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
SetAccountButtonStates();
|
|
}
|
|
|
|
private void HandleAccountDataGridRowMouseDoubleClick(object sender, MouseButtonEventArgs e)
|
|
{
|
|
if (!EditAccountButton.IsEnabled)
|
|
return;
|
|
|
|
EditSelectedAccount();
|
|
}
|
|
} |