Updated RemoveBooksDialog to use latest GridEntry

This commit is contained in:
Michael Bucari-Tovo 2021-08-09 23:11:37 -06:00
parent 2ef746a94c
commit c7454ea5d2
5 changed files with 97 additions and 93 deletions

View File

@ -39,7 +39,7 @@ namespace ApplicationServices
var libraryItems = await scanAccountsAsync(loginCallbackFactoryFunc, accounts); var libraryItems = await scanAccountsAsync(loginCallbackFactoryFunc, accounts);
Log.Logger.Information($"GetAllLibraryItems: Total count {libraryItems.Count}"); Log.Logger.Information($"GetAllLibraryItems: Total count {libraryItems.Count}");
var missingBookList = existingLibrary.Where(b => libraryItems.Count(i => i.DtoItem.Asin == b.Book.AudibleProductId) == 0).ToList(); var missingBookList = existingLibrary.Where(b => !libraryItems.Any(i => i.DtoItem.Asin == b.Book.AudibleProductId)).ToList();
return missingBookList; return missingBookList;
} }

View File

@ -30,17 +30,17 @@ namespace LibationWinForms.Dialogs
private void InitializeComponent() private void InitializeComponent()
{ {
this.components = new System.ComponentModel.Container(); this.components = new System.ComponentModel.Container();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.gridEntryBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.btnRemoveBooks = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.removeDataGridViewCheckBoxColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn(); this.removeDataGridViewCheckBoxColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.coverDataGridViewImageColumn = new System.Windows.Forms.DataGridViewImageColumn(); this.coverDataGridViewImageColumn = new System.Windows.Forms.DataGridViewImageColumn();
this.titleDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.titleDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.authorsDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.authorsDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.miscDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.miscDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.DatePurchased = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.purchaseDateGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.gridEntryBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.btnRemoveBooks = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.gridEntryBindingSource)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.gridEntryBindingSource)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
@ -58,16 +58,16 @@ namespace LibationWinForms.Dialogs
this.titleDataGridViewTextBoxColumn, this.titleDataGridViewTextBoxColumn,
this.authorsDataGridViewTextBoxColumn, this.authorsDataGridViewTextBoxColumn,
this.miscDataGridViewTextBoxColumn, this.miscDataGridViewTextBoxColumn,
this.DatePurchased}); this.purchaseDateGridViewTextBoxColumn});
this.dataGridView1.DataSource = this.gridEntryBindingSource; this.dataGridView1.DataSource = this.gridEntryBindingSource;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window; dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle1.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); dataGridViewCellStyle2.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText; dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight; dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText; dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True; dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle1; this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle2;
this.dataGridView1.Location = new System.Drawing.Point(0, 0); this.dataGridView1.Location = new System.Drawing.Point(0, 0);
this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersVisible = false; this.dataGridView1.RowHeadersVisible = false;
@ -75,32 +75,6 @@ namespace LibationWinForms.Dialogs
this.dataGridView1.Size = new System.Drawing.Size(800, 409); this.dataGridView1.Size = new System.Drawing.Size(800, 409);
this.dataGridView1.TabIndex = 0; this.dataGridView1.TabIndex = 0;
// //
// gridEntryBindingSource
//
this.gridEntryBindingSource.AllowNew = false;
this.gridEntryBindingSource.DataSource = typeof(LibationWinForms.Dialogs.RemovableGridEntry);
//
// btnRemoveBooks
//
this.btnRemoveBooks.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnRemoveBooks.Location = new System.Drawing.Point(570, 419);
this.btnRemoveBooks.Name = "btnRemoveBooks";
this.btnRemoveBooks.Size = new System.Drawing.Size(218, 23);
this.btnRemoveBooks.TabIndex = 1;
this.btnRemoveBooks.Text = "Remove Selected Books from Libation";
this.btnRemoveBooks.UseVisualStyleBackColor = true;
this.btnRemoveBooks.Click += new System.EventHandler(this.btnRemoveBooks_Click);
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 423);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(169, 15);
this.label1.TabIndex = 2;
this.label1.Text = "{0} books selected for removal.";
//
// removeDataGridViewCheckBoxColumn // removeDataGridViewCheckBoxColumn
// //
this.removeDataGridViewCheckBoxColumn.DataPropertyName = "Remove"; this.removeDataGridViewCheckBoxColumn.DataPropertyName = "Remove";
@ -109,6 +83,7 @@ namespace LibationWinForms.Dialogs
this.removeDataGridViewCheckBoxColumn.HeaderText = "Remove"; this.removeDataGridViewCheckBoxColumn.HeaderText = "Remove";
this.removeDataGridViewCheckBoxColumn.MinimumWidth = 60; this.removeDataGridViewCheckBoxColumn.MinimumWidth = 60;
this.removeDataGridViewCheckBoxColumn.Name = "removeDataGridViewCheckBoxColumn"; this.removeDataGridViewCheckBoxColumn.Name = "removeDataGridViewCheckBoxColumn";
this.removeDataGridViewCheckBoxColumn.Resizable = System.Windows.Forms.DataGridViewTriState.False;
this.removeDataGridViewCheckBoxColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic; this.removeDataGridViewCheckBoxColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.removeDataGridViewCheckBoxColumn.TrueValue = "True"; this.removeDataGridViewCheckBoxColumn.TrueValue = "True";
this.removeDataGridViewCheckBoxColumn.Width = 60; this.removeDataGridViewCheckBoxColumn.Width = 60;
@ -146,13 +121,39 @@ namespace LibationWinForms.Dialogs
this.miscDataGridViewTextBoxColumn.ReadOnly = true; this.miscDataGridViewTextBoxColumn.ReadOnly = true;
this.miscDataGridViewTextBoxColumn.Width = 150; this.miscDataGridViewTextBoxColumn.Width = 150;
// //
// DatePurchased // purchaseDateGridViewTextBoxColumn
// //
this.DatePurchased.DataPropertyName = "DatePurchased"; this.purchaseDateGridViewTextBoxColumn.DataPropertyName = "PurchaseDate";
this.DatePurchased.HeaderText = "Date Purchased"; this.purchaseDateGridViewTextBoxColumn.HeaderText = "Purchase Date";
this.DatePurchased.Name = "DatePurchased"; this.purchaseDateGridViewTextBoxColumn.Name = "purchaseDateGridViewTextBoxColumn";
this.DatePurchased.ReadOnly = true; this.purchaseDateGridViewTextBoxColumn.ReadOnly = true;
this.DatePurchased.Width = 120; this.purchaseDateGridViewTextBoxColumn.Resizable = System.Windows.Forms.DataGridViewTriState.False;
//
// gridEntryBindingSource
//
this.gridEntryBindingSource.AllowNew = false;
this.gridEntryBindingSource.DataSource = typeof(LibationWinForms.Dialogs.RemovableGridEntry);
//
// btnRemoveBooks
//
this.btnRemoveBooks.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnRemoveBooks.Location = new System.Drawing.Point(570, 419);
this.btnRemoveBooks.Name = "btnRemoveBooks";
this.btnRemoveBooks.Size = new System.Drawing.Size(218, 23);
this.btnRemoveBooks.TabIndex = 1;
this.btnRemoveBooks.Text = "Remove Selected Books from Libation";
this.btnRemoveBooks.UseVisualStyleBackColor = true;
this.btnRemoveBooks.Click += new System.EventHandler(this.btnRemoveBooks_Click);
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 423);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(178, 15);
this.label1.TabIndex = 2;
this.label1.Text = "{0} book{1} selected for removal.";
// //
// RemoveBooksDialog // RemoveBooksDialog
// //
@ -183,6 +184,6 @@ namespace LibationWinForms.Dialogs
private System.Windows.Forms.DataGridViewTextBoxColumn titleDataGridViewTextBoxColumn; private System.Windows.Forms.DataGridViewTextBoxColumn titleDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn authorsDataGridViewTextBoxColumn; private System.Windows.Forms.DataGridViewTextBoxColumn authorsDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn miscDataGridViewTextBoxColumn; private System.Windows.Forms.DataGridViewTextBoxColumn miscDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn DatePurchased; private System.Windows.Forms.DataGridViewTextBoxColumn purchaseDateGridViewTextBoxColumn;
} }
} }

View File

@ -6,12 +6,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Drawing;
using System.Linq; using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using Dinah.Core.DataBinding;
using System.Runtime.CompilerServices;
using Dinah.Core.Drawing;
using System.Collections; using System.Collections;
namespace LibationWinForms.Dialogs namespace LibationWinForms.Dialogs
@ -22,7 +18,7 @@ namespace LibationWinForms.Dialogs
private Account[] _accounts { get; } private Account[] _accounts { get; }
private List<LibraryBook> _libraryBooks; private List<LibraryBook> _libraryBooks;
private SortableBindingList<RemovableGridEntry> _removableGridEntries; private SortableBindingList2<RemovableGridEntry> _removableGridEntries;
private string _labelFormat; private string _labelFormat;
private int SelectedCount => SelectedEntries?.Count() ?? 0; private int SelectedCount => SelectedEntries?.Count() ?? 0;
private IEnumerable<RemovableGridEntry> SelectedEntries => _removableGridEntries?.Where(b => b.Remove); private IEnumerable<RemovableGridEntry> SelectedEntries => _removableGridEntries?.Where(b => b.Remove);
@ -31,6 +27,7 @@ namespace LibationWinForms.Dialogs
{ {
_libraryBooks = DbContexts.GetContext().GetLibrary_Flat_NoTracking(); _libraryBooks = DbContexts.GetContext().GetLibrary_Flat_NoTracking();
_accounts = accounts; _accounts = accounts;
InitializeComponent(); InitializeComponent();
_labelFormat = label1.Text; _labelFormat = label1.Text;
@ -40,10 +37,10 @@ namespace LibationWinForms.Dialogs
var orderedGridEntries = _libraryBooks var orderedGridEntries = _libraryBooks
.Select(lb => new RemovableGridEntry(lb)) .Select(lb => new RemovableGridEntry(lb))
.OrderByDescending(ge => ge.PurchaseDate) .OrderByDescending(ge => (DateTime)ge.GetMemberValue(nameof(ge.PurchaseDate)))
.ToList(); .ToList();
_removableGridEntries = orderedGridEntries.ToSortableBindingList(); _removableGridEntries = new SortableBindingList2<RemovableGridEntry>(orderedGridEntries);
gridEntryBindingSource.DataSource = _removableGridEntries; gridEntryBindingSource.DataSource = _removableGridEntries;
dataGridView1.Enabled = false; dataGridView1.Enabled = false;
@ -52,9 +49,7 @@ namespace LibationWinForms.Dialogs
private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{ {
if (e.ColumnIndex == 0) if (e.ColumnIndex == 0)
{
UpdateSelection(); UpdateSelection();
}
} }
private async void RemoveBooksDialog_Shown(object sender, EventArgs e) private async void RemoveBooksDialog_Shown(object sender, EventArgs e)
@ -65,9 +60,9 @@ namespace LibationWinForms.Dialogs
{ {
var rmovedBooks = await LibraryCommands.FindInactiveBooks((account) => new WinformResponder(account), _libraryBooks, _accounts); var rmovedBooks = await LibraryCommands.FindInactiveBooks((account) => new WinformResponder(account), _libraryBooks, _accounts);
var removable = _removableGridEntries.Where(rge => rmovedBooks.Count(rb => rb.Book.AudibleProductId == rge.AudibleProductId) == 1); var removable = _removableGridEntries.Where(rge => rmovedBooks.Any(rb => rb.Book.AudibleProductId == rge.AudibleProductId));
if (removable.Count() == 0) if (!removable.Any())
return; return;
foreach (var r in removable) foreach (var r in removable)
@ -90,15 +85,18 @@ namespace LibationWinForms.Dialogs
private void btnRemoveBooks_Click(object sender, EventArgs e) private void btnRemoveBooks_Click(object sender, EventArgs e)
{ {
var selected = SelectedEntries.ToList(); var selectedBooks = SelectedEntries.ToList();
if (selected.Count == 0) return; if (selectedBooks.Count == 0) return;
string titles = string.Join("\r\n", selected.Select(rge => "-" + rge.Title)); string titles = string.Join("\r\n", selectedBooks.Select(rge => "-" + rge.Title));
string thisThese = selectedBooks.Count > 1 ? "these" : "this";
string bookBooks = selectedBooks.Count > 1 ? "books" : "book";
var result = MessageBox.Show( var result = MessageBox.Show(
this, this,
$"Are you sure you want to remove the following {selected.Count} books from Libation's library?\r\n\r\n{titles}", $"Are you sure you want to remove {thisThese} {selectedBooks.Count} {bookBooks} from Libation's library?\r\n\r\n{titles}",
"Remove books from Libation?", "Remove books from Libation?",
MessageBoxButtons.YesNo, MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxIcon.Question,
@ -110,14 +108,16 @@ namespace LibationWinForms.Dialogs
var libBooks = context.GetLibrary_Flat_NoTracking(); var libBooks = context.GetLibrary_Flat_NoTracking();
var removeLibraryBooks = libBooks.Where(lb => selected.Count(rge => rge.AudibleProductId == lb.Book.AudibleProductId) == 1).ToArray(); var removeLibraryBooks = libBooks.Where(lb => selectedBooks.Any(rge => rge.AudibleProductId == lb.Book.AudibleProductId)).ToArray();
context.Library.RemoveRange(removeLibraryBooks); context.Library.RemoveRange(removeLibraryBooks);
context.SaveChanges(); context.SaveChanges();
BooksRemoved = true;
foreach (var rEntry in selected) foreach (var rEntry in selectedBooks)
_removableGridEntries.Remove(rEntry); _removableGridEntries.Remove(rEntry);
BooksRemoved = removeLibraryBooks.Length > 0;
UpdateSelection(); UpdateSelection();
} }
} }
@ -125,44 +125,47 @@ namespace LibationWinForms.Dialogs
{ {
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending); dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
var selectedCount = SelectedCount; var selectedCount = SelectedCount;
label1.Text = string.Format(_labelFormat, selectedCount); label1.Text = string.Format(_labelFormat, selectedCount, selectedCount != 1 ? "s" : string.Empty);
btnRemoveBooks.Enabled = selectedCount > 0; btnRemoveBooks.Enabled = selectedCount > 0;
} }
} }
class CompareBool : IComparer
{
public int Compare(object x, object y)
{
var rge1 = x as RemovableGridEntry;
var rge2 = y as RemovableGridEntry;
return rge1.Remove.CompareTo(rge2.Remove);
}
}
internal class RemovableGridEntry : GridEntry internal class RemovableGridEntry : GridEntry
{ {
private static readonly IComparer BoolComparer = new ObjectComparer<bool>();
private bool _remove = false;
public RemovableGridEntry(LibraryBook libraryBook) : base(libraryBook) { }
public bool Remove public bool Remove
{ {
get get
{ {
return _remove; return _remove;
} }
set set
{ {
if (_remove != value) if (_remove != value)
{ {
_remove = value; _remove = value;
NotifyPropertyChanged(); NotifyPropertyChanged();
} }
} }
}
private bool _remove = false;
public RemovableGridEntry(LibraryBook libraryBook) :base(libraryBook)
{
} }
public override object GetMemberValue(string propertyName)
{
if (propertyName == nameof(Remove))
return Remove;
return base.GetMemberValue(propertyName);
}
public override IComparer GetComparer(Type propertyType)
{
if (propertyType == typeof(bool))
return BoolComparer;
return base.GetComparer(propertyType);
}
} }
} }

View File

@ -57,7 +57,7 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="DatePurchased.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="purchaseDateGridViewTextBoxColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
<metadata name="gridEntryBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="gridEntryBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">

View File

@ -126,8 +126,8 @@ namespace LibationWinForms
private Dictionary<string, Func<object>> _compareValues { get; } private Dictionary<string, Func<object>> _compareValues { get; }
private static Dictionary<Type, IComparer> _objectComparers; private static Dictionary<Type, IComparer> _objectComparers;
public object GetMemberValue(string propertyName) => _compareValues[propertyName](); public virtual object GetMemberValue(string propertyName) => _compareValues[propertyName]();
public IComparer GetComparer(Type propertyType) => _objectComparers[propertyType]; public virtual IComparer GetComparer(Type propertyType) => _objectComparers[propertyType];
/// <summary> /// <summary>
/// Instantiate comparers for every type needed to sort columns. /// Instantiate comparers for every type needed to sort columns.