db persistence shouldn't be a side effect. the client should say when to persist in some fairly explicit way
This commit is contained in:
parent
7af890d897
commit
dd5e162c10
@ -58,11 +58,7 @@ namespace LibationWinForms
|
|||||||
public string Category { get; private set; }
|
public string Category { get; private set; }
|
||||||
public string Misc { get; private set; }
|
public string Misc { get; private set; }
|
||||||
public string Description { get; private set; }
|
public string Description { get; private set; }
|
||||||
public string DisplayTags
|
public string DisplayTags => string.Join("\r\n", Book.UserDefinedItem.TagsEnumerated);
|
||||||
{
|
|
||||||
get => string.Join("\r\n", Book.UserDefinedItem.TagsEnumerated);
|
|
||||||
set => Book.UserDefinedItem.Tags = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// these 2 values being in 1 field is the trick behind getting the liberated+pdf 'stoplight' icon to draw. See: LiberateDataGridViewImageButtonCell.Paint
|
// these 2 values being in 1 field is the trick behind getting the liberated+pdf 'stoplight' icon to draw. See: LiberateDataGridViewImageButtonCell.Paint
|
||||||
public (LiberatedStatus BookStatus, LiberatedStatus? PdfStatus) Liberate
|
public (LiberatedStatus BookStatus, LiberatedStatus? PdfStatus) Liberate
|
||||||
@ -77,18 +73,10 @@ namespace LibationWinForms
|
|||||||
}
|
}
|
||||||
return (_bookStatus, _pdfStatus);
|
return (_bookStatus, _pdfStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_bookStatus = value.BookStatus;
|
|
||||||
_pdfStatus = value.PdfStatus;
|
|
||||||
LibraryBook.Book.UserDefinedItem.BookStatus = value.BookStatus;
|
|
||||||
LibraryBook.Book.UserDefinedItem.PdfStatus = value.PdfStatus;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public event EventHandler<string> LibraryBookUpdated;
|
public event EventHandler LibraryBookUpdated;
|
||||||
public event EventHandler Committed;
|
public event EventHandler Committed;
|
||||||
|
|
||||||
// alias
|
// alias
|
||||||
@ -156,7 +144,7 @@ namespace LibationWinForms
|
|||||||
UserDefinedItem.ItemChanged += UserDefinedItem_ItemChanged;
|
UserDefinedItem.ItemChanged += UserDefinedItem_ItemChanged;
|
||||||
|
|
||||||
// this will never have a value when triggered by ctor b/c nothing can subscribe to the event until after ctor is complete
|
// this will never have a value when triggered by ctor b/c nothing can subscribe to the event until after ctor is complete
|
||||||
LibraryBookUpdated?.Invoke(this, AudibleProductId);
|
LibraryBookUpdated?.Invoke(this, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PictureStorage_PictureCached(object sender, PictureCachedEventArgs e)
|
private void PictureStorage_PictureCached(object sender, PictureCachedEventArgs e)
|
||||||
@ -196,38 +184,30 @@ namespace LibationWinForms
|
|||||||
NotifyPropertyChanged(nameof(Liberate));
|
NotifyPropertyChanged(nameof(Liberate));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!suspendCommit)
|
|
||||||
Commit();
|
|
||||||
}
|
|
||||||
private bool suspendCommit = false;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Begin editing the model, suspending commits until <see cref="EndEdit"/> is called.
|
|
||||||
/// </summary>
|
|
||||||
public void BeginEdit() => suspendCommit = true;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Save all edits to the database.
|
|
||||||
/// </summary>
|
|
||||||
public void EndEdit()
|
|
||||||
{
|
|
||||||
Commit();
|
|
||||||
suspendCommit = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Commit()
|
/// <summary>Save edits to the database</summary>
|
||||||
|
public void Commit(string newTags, LiberatedStatus bookStatus, LiberatedStatus? pdfStatus)
|
||||||
{
|
{
|
||||||
// We don't want LiberatedStatus.PartialDownload to be a persistent status.
|
// validate
|
||||||
// If display/icon status is PartialDownload then save NotLiberated to db then restore PartialDownload for display
|
if (DisplayTags.EqualsInsensitive(newTags) &&
|
||||||
var displayStatus = Book.UserDefinedItem.BookStatus;
|
Liberate.BookStatus == bookStatus &&
|
||||||
var saveStatus = displayStatus == LiberatedStatus.PartialDownload ? LiberatedStatus.NotLiberated : displayStatus;
|
Liberate.PdfStatus == pdfStatus)
|
||||||
Book.UserDefinedItem.BookStatus = saveStatus;
|
return;
|
||||||
|
|
||||||
|
// set
|
||||||
|
Book.UserDefinedItem.Tags = newTags;
|
||||||
|
|
||||||
|
_bookStatus = bookStatus;
|
||||||
|
_pdfStatus = pdfStatus;
|
||||||
|
|
||||||
|
Book.UserDefinedItem.BookStatus = bookStatus;
|
||||||
|
Book.UserDefinedItem.PdfStatus = pdfStatus;
|
||||||
|
|
||||||
|
// save
|
||||||
LibraryCommands.UpdateUserDefinedItem(Book);
|
LibraryCommands.UpdateUserDefinedItem(Book);
|
||||||
|
|
||||||
Book.UserDefinedItem.BookStatus = displayStatus;
|
// notify
|
||||||
|
|
||||||
Committed?.Invoke(this, null);
|
Committed?.Invoke(this, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -114,15 +114,8 @@ namespace LibationWinForms
|
|||||||
private static void Details_Click(GridEntry liveGridEntry)
|
private static void Details_Click(GridEntry liveGridEntry)
|
||||||
{
|
{
|
||||||
var bookDetailsForm = new BookDetailsDialog(liveGridEntry.LibraryBook);
|
var bookDetailsForm = new BookDetailsDialog(liveGridEntry.LibraryBook);
|
||||||
if (bookDetailsForm.ShowDialog() != DialogResult.OK)
|
if (bookDetailsForm.ShowDialog() == DialogResult.OK)
|
||||||
return;
|
liveGridEntry.Commit(bookDetailsForm.NewTags, bookDetailsForm.BookLiberatedStatus, bookDetailsForm.PdfLiberatedStatus);
|
||||||
|
|
||||||
liveGridEntry.BeginEdit();
|
|
||||||
|
|
||||||
liveGridEntry.DisplayTags = bookDetailsForm.NewTags;
|
|
||||||
liveGridEntry.Liberate = (bookDetailsForm.BookLiberatedStatus, bookDetailsForm.PdfLiberatedStatus);
|
|
||||||
|
|
||||||
liveGridEntry.EndEdit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -203,7 +196,7 @@ namespace LibationWinForms
|
|||||||
{
|
{
|
||||||
var entry = new GridEntry(libraryBook);
|
var entry = new GridEntry(libraryBook);
|
||||||
entry.Committed += Filter;
|
entry.Committed += Filter;
|
||||||
entry.LibraryBookUpdated += (sender, productId) => _dataGridView.InvalidateRow(_dataGridView.GetRowIdOfBoundItem((GridEntry)sender));
|
entry.LibraryBookUpdated += (sender, _) => _dataGridView.InvalidateRow(_dataGridView.GetRowIdOfBoundItem((GridEntry)sender));
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,14 +229,19 @@ namespace LibationWinForms
|
|||||||
|
|
||||||
// Causes repainting of the DataGridView
|
// Causes repainting of the DataGridView
|
||||||
bindingContext.ResumeBinding();
|
bindingContext.ResumeBinding();
|
||||||
VisibleCountChanged?.Invoke(this, _dataGridView.AsEnumerable().Count(r => r.Visible));
|
VisibleCountChanged?.Invoke(this, GetVisible().Count());
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region DataGridView Macro
|
internal IEnumerable<DataLayer.LibraryBook> GetVisible()
|
||||||
|
=> _dataGridView
|
||||||
|
.AsEnumerable()
|
||||||
|
.Where(row => row.Visible)
|
||||||
|
.Select(row => ((GridEntry)row.DataBoundItem).LibraryBook)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
private GridEntry getGridEntry(int rowIndex) => _dataGridView.GetBoundItem<GridEntry>(rowIndex);
|
private GridEntry getGridEntry(int rowIndex) => _dataGridView.GetBoundItem<GridEntry>(rowIndex);
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Column Customizations
|
#region Column Customizations
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user