diff --git a/Source/HangoverAvalonia/App.axaml b/Source/HangoverAvalonia/App.axaml index 0534c7e8..6908440c 100644 --- a/Source/HangoverAvalonia/App.axaml +++ b/Source/HangoverAvalonia/App.axaml @@ -6,7 +6,30 @@ - - - + + + + + + + + + + + + + + diff --git a/Source/HangoverAvalonia/ViewModels/MainVM.Deleted.cs b/Source/HangoverAvalonia/ViewModels/MainVM.Deleted.cs index 2c376fd7..827445d0 100644 --- a/Source/HangoverAvalonia/ViewModels/MainVM.Deleted.cs +++ b/Source/HangoverAvalonia/ViewModels/MainVM.Deleted.cs @@ -1,41 +1,8 @@ -using ApplicationServices; -using DataLayer; -using ReactiveUI; -using System.Collections.Generic; +namespace HangoverAvalonia.ViewModels; -namespace HangoverAvalonia.ViewModels +public partial class MainVM { - public partial class MainVM - { - private List _deletedBooks; - public List DeletedBooks { get => _deletedBooks; set => this.RaiseAndSetIfChanged(ref _deletedBooks, value); } - public string CheckedCountText => $"Checked : {_checkedBooksCount} of {_totalBooksCount}"; + public TrashBinViewModel TrashBinViewModel { get; } = new(); - private int _totalBooksCount = 0; - private int _checkedBooksCount = 0; - public int CheckedBooksCount - { - get => _checkedBooksCount; - set - { - if (_checkedBooksCount != value) - { - _checkedBooksCount = value; - this.RaisePropertyChanged(nameof(CheckedCountText)); - } - } - } - private void Load_deletedVM() - { - reload(); - } - - public void reload() - { - DeletedBooks = DbContexts.GetContext().GetDeletedLibraryBooks(); - _checkedBooksCount = 0; - _totalBooksCount = DeletedBooks.Count; - this.RaisePropertyChanged(nameof(CheckedCountText)); - } - } + private void Load_deletedVM() { } } diff --git a/Source/HangoverAvalonia/ViewModels/TrashBinViewModel.cs b/Source/HangoverAvalonia/ViewModels/TrashBinViewModel.cs new file mode 100644 index 00000000..22b5d15b --- /dev/null +++ b/Source/HangoverAvalonia/ViewModels/TrashBinViewModel.cs @@ -0,0 +1,117 @@ +using ApplicationServices; +using Avalonia.Collections; +using DataLayer; +using ReactiveUI; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Threading.Tasks; + +namespace HangoverAvalonia.ViewModels; + +public class TrashBinViewModel : ViewModelBase, IDisposable +{ + public AvaloniaList DeletedBooks { get; } + public string CheckedCountText => $"Checked : {_checkedBooksCount} of {_totalBooksCount}"; + + private bool _controlsEnabled = true; + public bool ControlsEnabled { get => _controlsEnabled; set => this.RaiseAndSetIfChanged(ref _controlsEnabled, value); } + + private bool? everythingChecked = false; + public bool? EverythingChecked + { + get => everythingChecked; + set + { + everythingChecked = value ?? false; + + if (everythingChecked is true) + CheckAll(); + else if (everythingChecked is false) + UncheckAll(); + } + } + + private int _totalBooksCount = 0; + private int _checkedBooksCount = -1; + public int CheckedBooksCount + { + get => _checkedBooksCount; + set + { + _checkedBooksCount = value; + this.RaisePropertyChanged(nameof(CheckedCountText)); + + everythingChecked + = _checkedBooksCount == 0 || _totalBooksCount == 0 ? false + : _checkedBooksCount == _totalBooksCount ? true + : null; + + this.RaisePropertyChanged(nameof(EverythingChecked)); + } + } + + public IEnumerable CheckedBooks => DeletedBooks.Where(i => i.IsChecked).Select(i => i.Item).Cast(); + + public TrashBinViewModel() + { + DeletedBooks = new() + { + ResetBehavior = ResetBehavior.Remove + }; + + tracker = DeletedBooks.TrackItemPropertyChanged(CheckboxPropertyChanged); + Reload(); + } + + public void CheckAll() + { + foreach (var item in DeletedBooks) + item.IsChecked = true; + } + + public void UncheckAll() + { + foreach (var item in DeletedBooks) + item.IsChecked = false; + } + + public async Task RestoreCheckedAsync() + { + ControlsEnabled = false; + var qtyChanges = await Task.Run(CheckedBooks.RestoreBooks); + if (qtyChanges > 0) + Reload(); + ControlsEnabled = true; + } + + public async Task PermanentlyDeleteCheckedAsync() + { + ControlsEnabled = false; + var qtyChanges = await Task.Run(CheckedBooks.PermanentlyDeleteBooks); + if (qtyChanges > 0) + Reload(); + ControlsEnabled = true; + } + + public void Reload() + { + var deletedBooks = DbContexts.GetContext().GetDeletedLibraryBooks(); + + DeletedBooks.Clear(); + DeletedBooks.AddRange(deletedBooks.Select(lb => new CheckBoxViewModel { Item = lb })); + + _totalBooksCount = DeletedBooks.Count; + CheckedBooksCount = 0; + } + + private IDisposable tracker; + private void CheckboxPropertyChanged(Tuple e) + { + if (e.Item2.PropertyName == nameof(CheckBoxViewModel.IsChecked)) + CheckedBooksCount = DeletedBooks.Count(b => b.IsChecked); + } + + public void Dispose() => tracker?.Dispose(); +} diff --git a/Source/HangoverAvalonia/Views/MainWindow.Deleted.cs b/Source/HangoverAvalonia/Views/MainWindow.Deleted.cs index 2c2341e4..101d0375 100644 --- a/Source/HangoverAvalonia/Views/MainWindow.Deleted.cs +++ b/Source/HangoverAvalonia/Views/MainWindow.Deleted.cs @@ -1,40 +1,12 @@ -using ApplicationServices; -using DataLayer; -using HangoverAvalonia.Controls; -using System.Linq; +namespace HangoverAvalonia.Views; -namespace HangoverAvalonia.Views +public partial class MainWindow { - public partial class MainWindow + private void deletedTab_VisibleChanged(bool isVisible) { - private void deletedTab_VisibleChanged(bool isVisible) - { - if (!isVisible) - return; + if (!isVisible) + return; - if (_viewModel.DeletedBooks.Count == 0) - _viewModel.reload(); - } - public void Deleted_CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs args) - { - _viewModel.CheckedBooksCount = deletedCbl.CheckedItems.Count(); - } - public void Deleted_CheckAll_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e) - { - foreach (var item in deletedCbl.Items) - deletedCbl.SetItemChecked(item, true); - } - public void Deleted_UncheckAll_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e) - { - foreach (var item in deletedCbl.Items) - deletedCbl.SetItemChecked(item, false); - } - public void Deleted_Save_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e) - { - var libraryBooksToRestore = deletedCbl.CheckedItems.Cast().ToList(); - var qtyChanges = libraryBooksToRestore.RestoreBooks(); - if (qtyChanges > 0) - _viewModel.reload(); - } + _viewModel.TrashBinViewModel.Reload(); } } diff --git a/Source/HangoverAvalonia/Views/MainWindow.axaml b/Source/HangoverAvalonia/Views/MainWindow.axaml index 1b849ddf..f960db00 100644 --- a/Source/HangoverAvalonia/Views/MainWindow.axaml +++ b/Source/HangoverAvalonia/Views/MainWindow.axaml @@ -15,13 +15,11 @@ - -