Files
hDM/src/DownloadManager.WPF/Views/Dialogs/CategoryManagerDialog.xaml.cs
2026-05-09 11:38:04 +03:00

87 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using ModernWpf.Controls;
using DownloadManager.Core.Services;
using DownloadManager.Core.Models;
using System.Windows;
using System.Linq;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
namespace DownloadManager.WPF.Views.Dialogs;
public partial class CategoryManagerDialog : ContentDialog
{
private readonly ICategoryService _categoryService;
private ObservableCollection<DownloadCategory> _categories = new();
public CategoryManagerDialog(ICategoryService categoryService)
{
InitializeComponent();
_categoryService = categoryService;
CategoryList.ItemsSource = _categories;
_ = LoadCategoriesAsync();
}
private async Task LoadCategoriesAsync()
{
_categories.Clear();
var items = await _categoryService.GetCategoriesAsync();
foreach (var item in items)
{
_categories.Add(item);
}
}
private void CategoryList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (CategoryList.SelectedItem is DownloadCategory selected)
{
NameBox.Text = selected.Name;
ExtensionsBox.Text = selected.Extensions;
UpdateBtn.IsEnabled = true;
DeleteBtn.IsEnabled = true;
}
else
{
NameBox.Text = "";
ExtensionsBox.Text = "";
UpdateBtn.IsEnabled = false;
DeleteBtn.IsEnabled = false;
}
}
private async void AddButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(NameBox.Text)) return;
var cat = new DownloadCategory
{
Name = NameBox.Text,
Extensions = ExtensionsBox.Text,
SavePath = "" // Varsayılanı boş bırakıyoruz
};
await _categoryService.AddCategoryAsync(cat);
await LoadCategoriesAsync();
CategoryList.SelectedItem = _categories.FirstOrDefault(c => c.Name == cat.Name);
}
private async void UpdateButton_Click(object sender, RoutedEventArgs e)
{
if (CategoryList.SelectedItem is DownloadCategory selected && !string.IsNullOrWhiteSpace(NameBox.Text))
{
selected.Name = NameBox.Text;
selected.Extensions = ExtensionsBox.Text;
await _categoryService.UpdateCategoryAsync(selected);
await LoadCategoriesAsync();
CategoryList.SelectedItem = _categories.FirstOrDefault(c => c.Id == selected.Id);
}
}
private async void DeleteButton_Click(object sender, RoutedEventArgs e)
{
if (CategoryList.SelectedItem is DownloadCategory selected)
{
await _categoryService.DeleteCategoryAsync(selected.Id);
await LoadCategoriesAsync();
}
}
}