Refactored StreamBaseForm

This commit is contained in:
Michael Bucari-Tovo 2021-08-12 13:36:13 -06:00
parent 97730d1793
commit 2c20d03506

View File

@ -29,10 +29,12 @@ namespace LibationWinForms.BookLiberation
OnUnsubscribeAll(this, EventArgs.Empty); OnUnsubscribeAll(this, EventArgs.Empty);
Streamable.StreamingBegin += ShowFormHandler;
Streamable.StreamingBegin += OnStreamingBegin; Streamable.StreamingBegin += OnStreamingBegin;
Streamable.StreamingCompleted += OnStreamingCompleted;
Streamable.StreamingProgressChanged += OnStreamingProgressChanged; Streamable.StreamingProgressChanged += OnStreamingProgressChanged;
Streamable.StreamingTimeRemaining += OnStreamingTimeRemaining; Streamable.StreamingTimeRemaining += OnStreamingTimeRemaining;
Streamable.StreamingCompleted += OnStreamingCompleted;
Streamable.StreamingCompleted += CloseFormHandler;
FormClosed += OnUnsubscribeAll; FormClosed += OnUnsubscribeAll;
} }
@ -41,14 +43,15 @@ namespace LibationWinForms.BookLiberation
{ {
FormClosed -= OnUnsubscribeAll; FormClosed -= OnUnsubscribeAll;
Streamable.StreamingBegin -= ShowFormHandler;
Streamable.StreamingBegin -= OnStreamingBegin; Streamable.StreamingBegin -= OnStreamingBegin;
Streamable.StreamingCompleted -= OnStreamingCompleted;
Streamable.StreamingProgressChanged -= OnStreamingProgressChanged; Streamable.StreamingProgressChanged -= OnStreamingProgressChanged;
Streamable.StreamingTimeRemaining -= OnStreamingTimeRemaining; Streamable.StreamingTimeRemaining -= OnStreamingTimeRemaining;
Streamable.StreamingCompleted -= OnStreamingCompleted;
Streamable.StreamingCompleted -= CloseFormHandler;
} }
#region IStreamable event handlers private void ShowFormHandler(object sender, string beginString)
public virtual void OnStreamingBegin(object sender, string beginString)
{ {
//If StreamingBegin is fired from a worker thread, the window will be created on //If StreamingBegin is fired from a worker thread, the window will be created on
//that UI thread. We need to make certain that we show the window on the same //that UI thread. We need to make certain that we show the window on the same
@ -70,9 +73,18 @@ namespace LibationWinForms.BookLiberation
else else
show(); show();
} }
/// <summary>
/// If the form was shown using Show (not ShowDialog), Form.Close calls Form.Dispose
/// </summary>
private void CloseFormHandler(object sender, string completedString) => this.UIThread(() => Close());
#region IStreamable event handlers
public virtual void OnStreamingBegin(object sender, string beginString) { }
public virtual void OnStreamingProgressChanged(object sender, DownloadProgress downloadProgress) { } public virtual void OnStreamingProgressChanged(object sender, DownloadProgress downloadProgress) { }
public virtual void OnStreamingTimeRemaining(object sender, TimeSpan timeRemaining) { } public virtual void OnStreamingTimeRemaining(object sender, TimeSpan timeRemaining) { }
public virtual void OnStreamingCompleted(object sender, string completedString) => this.UIThread(() => Close()); public virtual void OnStreamingCompleted(object sender, string completedString) { }
#endregion #endregion
} }
} }