live update newly downloaded and cached images

This commit is contained in:
Robert McRackan 2019-11-19 11:22:41 -05:00
parent 80b0ef600d
commit b0139c47be
2 changed files with 39 additions and 4 deletions

View File

@ -38,6 +38,8 @@ namespace FileManager
timer.Elapsed += (_, __) => timerDownload(); timer.Elapsed += (_, __) => timerDownload();
} }
public static event EventHandler<string> PictureCached;
private static Dictionary<PictureDefinition, byte[]> cache { get; } = new Dictionary<PictureDefinition, byte[]>(); private static Dictionary<PictureDefinition, byte[]> cache { get; } = new Dictionary<PictureDefinition, byte[]>();
public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def) public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def)
{ {
@ -86,6 +88,8 @@ namespace FileManager
var bytes = downloadBytes(def); var bytes = downloadBytes(def);
saveFile(def, bytes); saveFile(def, bytes);
cache[def] = bytes; cache[def] = bytes;
PictureCached?.Invoke(nameof(PictureStorage), def.PictureId);
} }
finally finally
{ {

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
@ -6,6 +7,7 @@ using ApplicationServices;
using DataLayer; using DataLayer;
using Dinah.Core.Collections.Generic; using Dinah.Core.Collections.Generic;
using Dinah.Core.DataBinding; using Dinah.Core.DataBinding;
using Dinah.Core.Windows.Forms;
namespace LibationWinForm namespace LibationWinForm
{ {
@ -32,7 +34,10 @@ namespace LibationWinForm
public ProductsGrid() public ProductsGrid()
{ {
InitializeComponent(); InitializeComponent();
Disposed += (_, __) => context?.Dispose(); Disposed += (_, __) => context?.Dispose();
manageLiveImageUpdateSubscriptions();
} }
private bool hasBeenDisplayed = false; private bool hasBeenDisplayed = false;
@ -232,12 +237,38 @@ namespace LibationWinForm
dataGridView.Rows[r].Visible = productIds.Contains(getGridEntry(r).GetBook().AudibleProductId); dataGridView.Rows[r].Visible = productIds.Contains(getGridEntry(r).GetBook().AudibleProductId);
} }
currencyManager.ResumeBinding(); currencyManager.ResumeBinding();
VisibleCountChanged?.Invoke(this, dataGridView.Rows.Cast<DataGridViewRow>().Count(r => r.Visible)); VisibleCountChanged?.Invoke(this, dataGridView.AsEnumerable().Count(r => r.Visible));
var luceneSearchString_debug = searchResults.SearchString; var luceneSearchString_debug = searchResults.SearchString;
} }
#endregion #endregion
private GridEntry getGridEntry(int rowIndex) => (GridEntry)dataGridView.Rows[rowIndex].DataBoundItem; #region live update newly downloaded and cached images
} private void manageLiveImageUpdateSubscriptions()
{
FileManager.PictureStorage.PictureCached += crossThreadImageUpdate;
Disposed += (_, __) => FileManager.PictureStorage.PictureCached -= crossThreadImageUpdate;
}
private void crossThreadImageUpdate(object _, string pictureId)
=> dataGridView.UIThread(() => updateRowImage(pictureId));
private void updateRowImage(string pictureId)
{
var rowId = getRowId((ge) => ge.GetBook().PictureId == pictureId);
if (rowId > -1)
dataGridView.InvalidateRow(rowId);
}
#endregion
private GridEntry getGridEntry(int rowIndex) => (GridEntry)dataGridView.Rows[rowIndex].DataBoundItem;
private int getRowId(Func<GridEntry, bool> func)
{
for (var r = 0; r < dataGridView.RowCount; r++)
if (func(getGridEntry(r)))
return r;
return -1;
}
}
} }