streamline indexing ui workflow

This commit is contained in:
Robert McRackan 2019-11-12 12:54:54 -05:00
parent 0b42b8ee49
commit 01a914c390
8 changed files with 25 additions and 151 deletions

View file

@ -1,8 +0,0 @@
namespace LibationWinForm
{
public interface IIndexLibraryDialog : IRunnableDialog
{
int TotalBooksProcessed { get; }
int NewBooksAdded { get; }
}
}

View file

@ -1,20 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LibationWinForm
{
public interface IRunnableDialog
{
IButtonControl AcceptButton { get; set; }
Control.ControlCollection Controls { get; }
Task DoMainWorkAsync();
string SuccessMessage { get; }
DialogResult ShowDialog();
DialogResult DialogResult { get; set; }
void Close();
}
}

View file

@ -1,76 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dinah.Core.Windows.Forms;
namespace LibationWinForm
{
public static class IRunnableDialogExt
{
public static DialogResult RunDialog(this IRunnableDialog dialog)
{
// hook up runner before dialog.ShowDialog for all
var acceptButton = (ButtonBase)dialog.AcceptButton;
acceptButton.Click += acceptButton_Click;
return dialog.ShowDialog();
}
// running/workflow logic is in IndexDialogRunner.Run()
private static async void acceptButton_Click(object sender, EventArgs e)
{
var form = ((Control)sender).FindForm();
var iRunnableDialog = form as IRunnableDialog;
try
{
await iRunnableDialog.Run();
}
catch (Exception ex)
{
throw new Exception("Did the database get created correctly? Including seed data. Eg: Update-Database", ex);
}
}
public static async Task Run(this IRunnableDialog dialog)
{
// get top level controls only. If Enabled, disable and push on stack
var disabledStack = disable(dialog);
// lazy-man's async. also violates the intent of async/await.
// use here for now simply for UI responsiveness
await dialog.DoMainWorkAsync().ConfigureAwait(true);
// after running, unwind and re-enable
enable(disabledStack);
MessageBox.Show(dialog.SuccessMessage);
dialog.DialogResult = DialogResult.OK;
dialog.Close();
}
static Stack<Control> disable(IRunnableDialog dialog)
{
var disableStack = new Stack<Control>();
foreach (Control ctrl in dialog.Controls)
{
if (ctrl.Enabled)
{
disableStack.Push(ctrl);
ctrl.Enabled = false;
}
}
return disableStack;
}
static void enable(Stack<Control> disabledStack)
{
while (disabledStack.Count > 0)
{
var ctrl = disabledStack.Pop();
ctrl.Enabled = true;
}
}
}
}

View file

@ -51,7 +51,6 @@
this.MinimizeBox = false;
this.Name = "IndexLibraryDialog";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Scan Library";
this.ResumeLayout(false);

View file

@ -1,40 +1,24 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms;
using ApplicationServices;
namespace LibationWinForm
{
public partial class IndexLibraryDialog : Form, IIndexLibraryDialog
public partial class IndexLibraryDialog : Form
{
public IndexLibraryDialog()
{
InitializeComponent();
var btn = new Button();
AcceptButton = btn;
btn.Location = new System.Drawing.Point(this.Size.Width + 10, 0);
// required for FindForm() to work
this.Controls.Add(btn);
this.Shown += (_, __) => AcceptButton.PerformClick();
}
List<string> successMessages { get; } = new List<string>();
public string SuccessMessage => string.Join("\r\n", successMessages);
public int NewBooksAdded { get; private set; }
public int TotalBooksProcessed { get; private set; }
public async Task DoMainWorkAsync()
public IndexLibraryDialog()
{
var callback = new Login.WinformResponder();
(TotalBooksProcessed, NewBooksAdded) = await LibraryCommands.IndexLibraryAsync(callback);
InitializeComponent();
this.Shown += IndexLibraryDialog_Shown;
}
successMessages.Add($"Total processed: {TotalBooksProcessed}");
successMessages.Add($"New: {NewBooksAdded}");
private async void IndexLibraryDialog_Shown(object sender, System.EventArgs e)
{
(TotalBooksProcessed, NewBooksAdded) = await LibraryCommands.IndexLibraryAsync(new Login.WinformResponder());
this.Close();
}
}
}