diff --git a/LibationLauncher/LibationLauncher.csproj b/LibationLauncher/LibationLauncher.csproj index 15767c49..f25092f8 100644 --- a/LibationLauncher/LibationLauncher.csproj +++ b/LibationLauncher/LibationLauncher.csproj @@ -13,7 +13,7 @@ win-x64 - 5.1.9.13 + 5.1.10.1 diff --git a/LibationLauncher/Program.cs b/LibationLauncher/Program.cs index a49cdbc6..d6670493 100644 --- a/LibationLauncher/Program.cs +++ b/LibationLauncher/Program.cs @@ -277,8 +277,8 @@ namespace LibationLauncher { // appsettings.json var appSettingsKey = UNSAFE_MigrationHelper.LIBATION_FILES_KEY; - if (UNSAFE_MigrationHelper.AppSettings_TryGet(appSettingsKey, out var value)) - UNSAFE_MigrationHelper.AppSettings_Update(appSettingsKey, translatePath(value)); + if (UNSAFE_MigrationHelper.APPSETTINGS_TryGet(appSettingsKey, out var value)) + UNSAFE_MigrationHelper.APPSETTINGS_Update(appSettingsKey, translatePath(value)); } } @@ -421,8 +421,7 @@ namespace LibationLauncher try { - LibationWinForms.BookLiberation.ProcessorAutomationController.DownloadFileAsync(zipUrl, selectedPath).GetAwaiter().GetResult(); - MessageBox.Show("File downloaded"); + LibationWinForms.BookLiberation.ProcessorAutomationController.DownloadFile(zipUrl, selectedPath, true); } catch (Exception ex) { diff --git a/LibationLauncher/UNSAFE_MigrationHelper.cs b/LibationLauncher/UNSAFE_MigrationHelper.cs index d4f830c7..23fbec52 100644 --- a/LibationLauncher/UNSAFE_MigrationHelper.cs +++ b/LibationLauncher/UNSAFE_MigrationHelper.cs @@ -12,44 +12,44 @@ namespace LibationLauncher internal static class UNSAFE_MigrationHelper { #region appsettings.json - public const string APPSETTINGS_JSON = "appsettings.json"; + private const string APPSETTINGS_JSON = "appsettings.json"; - public static bool AppSettingsJson_Exists => File.Exists(APPSETTINGS_JSON); + public static bool APPSETTINGS_Json_Exists => File.Exists(APPSETTINGS_JSON); - public static bool AppSettings_TryGet(string key, out string value) + public static bool APPSETTINGS_TryGet(string key, out string value) { bool success = false; JToken val = null; - process_AppSettingsJson(jObj => success = jObj.TryGetValue(key, out val), false); + process_APPSETTINGS_Json(jObj => success = jObj.TryGetValue(key, out val), false); value = success ? val.Value() : null; return success; } /// only insert if not exists - public static void AppSettings_Insert(string key, string value) - => process_AppSettingsJson(jObj => jObj.TryAdd(key, value)); + public static void APPSETTINGS_Insert(string key, string value) + => process_APPSETTINGS_Json(jObj => jObj.TryAdd(key, value)); /// only update if exists - public static void AppSettings_Update(string key, string value) - => process_AppSettingsJson(jObj => { + public static void APPSETTINGS_Update(string key, string value) + => process_APPSETTINGS_Json(jObj => { if (jObj.ContainsKey(key)) jObj[key] = value; }); /// only delete if exists - public static void AppSettings_Delete(string key) - => process_AppSettingsJson(jObj => { + public static void APPSETTINGS_Delete(string key) + => process_APPSETTINGS_Json(jObj => { if (jObj.ContainsKey(key)) jObj.Remove(key); }); /// True: save if contents changed. False: no not attempt save - private static void process_AppSettingsJson(Action action, bool save = true) + private static void process_APPSETTINGS_Json(Action action, bool save = true) { // only insert if not exists - if (!AppSettingsJson_Exists) + if (!APPSETTINGS_Json_Exists) return; var startingContents = File.ReadAllText(APPSETTINGS_JSON); @@ -73,13 +73,13 @@ namespace LibationLauncher #region Settings.json public const string LIBATION_FILES_KEY = "LibationFiles"; - public const string SETTINGS_JSON = "Settings.json"; + private const string SETTINGS_JSON = "Settings.json"; public static string SettingsJsonPath { get { - var success = AppSettings_TryGet(LIBATION_FILES_KEY, out var value); + var success = APPSETTINGS_TryGet(LIBATION_FILES_KEY, out var value); return !success || value is null ? null : Path.Combine(value, SETTINGS_JSON); } } diff --git a/LibationWinForms/BookLiberation/DownloadForm.cs b/LibationWinForms/BookLiberation/DownloadForm.cs index de13e067..ffdf28e0 100644 --- a/LibationWinForms/BookLiberation/DownloadForm.cs +++ b/LibationWinForms/BookLiberation/DownloadForm.cs @@ -46,7 +46,7 @@ namespace LibationWinForms.BookLiberation lastUpdateLbl.UIThread(() => lastUpdateLbl.Visible = lastDownloadProgress.AddSeconds(30) < DateTime.Now); if (lastUpdateLbl.Visible) { - var diff = lastDownloadProgress - DateTime.Now; + var diff = DateTime.Now - lastDownloadProgress; var min = (int)diff.TotalMinutes; var minText = min > 0 ? $"{min}min " : ""; diff --git a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs index e553b207..80c023f5 100644 --- a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs +++ b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using System.Windows.Forms; using DataLayer; using Dinah.Core.ErrorHandling; +using Dinah.Core.Windows.Forms; using FileLiberator; namespace LibationWinForms.BookLiberation @@ -168,21 +169,27 @@ namespace LibationWinForms.BookLiberation return downloadPdf; } - public static async Task DownloadFileAsync(string url, string destination) + public static void DownloadFile(string url, string destination, bool showDownloadCompletedDialog = false) { - var downloadFile = new DownloadFile(); - - // frustratingly copy pasta from wireUpEvents(IDownloadable downloadable) due to Completed being EventHandler var downloadDialog = new DownloadForm(); - downloadFile.DownloadBegin += (_, str) => - { - downloadDialog.UpdateFilename(str); - downloadDialog.Show(); - }; - downloadFile.DownloadProgressChanged += (_, progress) => downloadDialog.DownloadProgressChanged(progress.BytesReceived, progress.TotalBytesToReceive); - downloadFile.DownloadCompleted += (_, __) => downloadDialog.Close(); + downloadDialog.UpdateFilename(destination); + downloadDialog.Show(); - await downloadFile.PerformDownloadFileAsync(url, destination); + new System.Threading.Thread(() => { + var downloadFile = new DownloadFile(); + + downloadFile.DownloadProgressChanged += (_, progress) => downloadDialog.UIThread(() => + downloadDialog.DownloadProgressChanged(progress.BytesReceived, progress.TotalBytesToReceive) + ); + downloadFile.DownloadCompleted += (_, __) => downloadDialog.UIThread(() => + { + downloadDialog.Close(); + if (showDownloadCompletedDialog) + MessageBox.Show("File downloaded"); + }); + + downloadFile.PerformDownloadFileAsync(url, destination).GetAwaiter().GetResult(); + }).Start(); } // subscribed to Begin event because a new form should be created+processed+closed on each iteration diff --git a/LibationWinForms/Form1.cs b/LibationWinForms/Form1.cs index b6241779..e635cb3c 100644 --- a/LibationWinForms/Form1.cs +++ b/LibationWinForms/Form1.cs @@ -37,6 +37,9 @@ namespace LibationWinForms private void Form1_Load(object sender, EventArgs e) { + if (this.DesignMode) + return; + // load default/missing cover images. this will also initiate the background image downloader var format = System.Drawing.Imaging.ImageFormat.Jpeg; PictureStorage.SetDefaultImage(PictureSize._80x80, Properties.Resources.default_cover_80x80.ToBytes(format));