Skip to content

Commit

Permalink
add/modify grid without reload
Browse files Browse the repository at this point in the history
  • Loading branch information
rmcrackan committed May 4, 2022
1 parent 0bde86e commit 128face
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
1 change: 1 addition & 0 deletions ApplicationServices/LibraryCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ private static async Task<int> importIntoDbAsync(List<ImportItem> importItems)
int qtyChanges = saveChanges(context);
logTime("importIntoDbAsync -- post SaveChanges");

// this is any changes at all to the database, not just new books
if (qtyChanges > 0)
await Task.Run(() => finalizeLibrarySizeChange());
logTime("importIntoDbAsync -- post finalizeLibrarySizeChange");
Expand Down
14 changes: 10 additions & 4 deletions LibationWinForms/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,22 @@ private void setGrid()
{
SuspendLayout();
{
if (productsGrid != null)
// previous non-null grid with zero-count removes columns. remove/re-add grid to get columns back
if (productsGrid?.Count == 0)
{
gridPanel.Controls.Remove(productsGrid);
productsGrid.VisibleCountChanged -= setVisibleCount;
productsGrid.Dispose();
productsGrid = null;
}

if (productsGrid is null)
{
productsGrid = new ProductsGrid { Dock = DockStyle.Fill };
productsGrid.VisibleCountChanged += setVisibleCount;
gridPanel.UIThreadSync(() => gridPanel.Controls.Add(productsGrid));
}

productsGrid = new ProductsGrid { Dock = DockStyle.Fill };
productsGrid.VisibleCountChanged += setVisibleCount;
gridPanel.UIThreadSync(() => gridPanel.Controls.Add(productsGrid));
productsGrid.Display();
}
ResumeLayout();
Expand Down
70 changes: 49 additions & 21 deletions LibationWinForms/grid/ProductsGrid.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -107,25 +108,18 @@ private static void Details_Click(GridEntry liveGridEntry)

#endregion

private SortableBindingList<GridEntry> bindingList;
#region UI display functions

/// <summary>Insert ad hoc library books to top of grid</summary>
public void AddToTop(DataLayer.LibraryBook libraryBook) => bindingList.Insert(0, toGridEntry(libraryBook));
public int Count { get; private set; }

#region UI display functions
private SortableBindingList<GridEntry> bindingList;

private bool hasBeenDisplayed = false;
public void Display()
{
if (hasBeenDisplayed)
return;
hasBeenDisplayed = true;

//
// transform into sorted GridEntry.s BEFORE binding
//
var lib = DbContexts.GetLibrary_Flat_NoTracking();

Count = lib.Count;

// if no data. hide all columns. return
if (!lib.Any())
{
Expand All @@ -134,25 +128,59 @@ public void Display()
return;
}

var orderedGridEntries = lib
.Select(lb => toGridEntry(lb))
var orderedBooks = lib
// default load order
.OrderByDescending(ge => (DateTime)ge.GetMemberValue(nameof(ge.PurchaseDate)))
.OrderByDescending(lb => lb.DateAdded)
//// more advanced example: sort by author, then series, then title
//.OrderBy(ge => ge.Authors)
// .ThenBy(ge => ge.Series)
// .ThenBy(ge => ge.Title)
//.OrderBy(lb => lb.Book.AuthorNames)
// .ThenBy(lb => lb.Book.SeriesSortable)
// .ThenBy(lb => lb.Book.TitleSortable)
.ToList();

// BIND
bindingList = new SortableBindingList<GridEntry>(orderedGridEntries);
gridEntryBindingSource.DataSource = bindingList;
if (bindingList is null)
bindToGrid(orderedBooks);
else
updateGrid(orderedBooks);

// FILTER
Filter();
}

private GridEntry toGridEntry(DataLayer.LibraryBook libraryBook)
private void bindToGrid(List<DataLayer.LibraryBook> orderedBooks)
{
bindingList = new SortableBindingList<GridEntry>(orderedBooks.Select(lb => toGridEntry(lb)));
gridEntryBindingSource.DataSource = bindingList;
}

private void updateGrid(List<DataLayer.LibraryBook> orderedBooks)
{
for (var i = orderedBooks.Count - 1; i >= 0; i--)
{
var libraryBook = orderedBooks[i];
var existingItem = bindingList.FirstOrDefault(i => i.AudibleProductId == libraryBook.Book.AudibleProductId);

// add new to top
if (existingItem is null)
bindingList.Insert(0, toGridEntry(libraryBook));
// update existing
else
existingItem.UpdateLibraryBook(libraryBook);
}

// remove deleted from grid. note: actual deletion from db must still occur via the RemoveBook feature. deleting from audible will not trigger this
var oldIds = bindingList.Select(ge => ge.AudibleProductId).ToList();
var newIds = orderedBooks.Select(lb => lb.Book.AudibleProductId).ToList();
var remove = oldIds.Except(newIds).ToList();
foreach (var id in remove)
{
var oldItem = bindingList.FirstOrDefault(ge => ge.AudibleProductId == id);
if (oldItem is not null)
bindingList.Remove(oldItem);
}
}

private GridEntry toGridEntry(DataLayer.LibraryBook libraryBook)
{
var entry = new GridEntry(libraryBook);
entry.Committed += Filter;
Expand Down

0 comments on commit 128face

Please sign in to comment.