From 4ece8d18a7502fbab4fc0b3dc56ea76365f74b75 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Tue, 26 Jul 2016 12:54:16 -0700 Subject: [PATCH] Make LoadCacheable collector return instance of T rather than string - Issue #1195, #1188 --- ApplicationView/MainApplicationWidget.cs | 18 +++++++++--------- SettingsManagement/OemSettings.cs | 4 +++- SlicerConfiguration/Settings/ProfileManager.cs | 11 ++++++----- .../Settings/SettingsHelpers.cs | 1 + .../RetrievePublicProfileTest.cs | 4 ++-- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/ApplicationView/MainApplicationWidget.cs b/ApplicationView/MainApplicationWidget.cs index f55094730..ed6f57973 100644 --- a/ApplicationView/MainApplicationWidget.cs +++ b/ApplicationView/MainApplicationWidget.cs @@ -48,6 +48,8 @@ using System.Threading.Tasks; namespace MatterHackers.MatterControl { + using OemProfileDictionary = Dictionary>; + public abstract class ApplicationView : GuiWidget { public abstract void AddElements(); @@ -179,8 +181,8 @@ namespace MatterHackers.MatterControl public static Func>> GetProfileHistory; public static Func GetPrinterProfile; public static Func SyncPrinterProfiles; - public static Func> GetPublicProfileList; - public static Func> DownloadPublicProfileAsync; + public static Func> GetPublicProfileList; + public static Func> DownloadPublicProfileAsync; public SlicePresetsWindow EditMaterialPresetsWindow { get; set; } @@ -221,7 +223,7 @@ namespace MatterHackers.MatterControl /// /// The custom collector function to load the content /// - public async static Task LoadCacheableAsync(string cacheKey, string cacheScope, Func> collector, string staticDataFallbackPath = null) where T : class + public async static Task LoadCacheableAsync(string cacheKey, string cacheScope, Func> collector, string staticDataFallbackPath = null) where T : class { string cacheDirectory = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache", cacheScope); string cachePath = Path.Combine(cacheDirectory, cacheKey); @@ -232,15 +234,13 @@ namespace MatterHackers.MatterControl try { // Try to update the document - string documentText = await collector(); + T item = await collector(); - if (!string.IsNullOrEmpty(documentText)) + if (item != null) { - var results = JsonConvert.DeserializeObject(documentText); - // update cache on success - File.WriteAllText(cachePath, documentText); - return results; + File.WriteAllText(cachePath, JsonConvert.SerializeObject(item)); + return item; } } catch diff --git a/SettingsManagement/OemSettings.cs b/SettingsManagement/OemSettings.cs index d21765f23..45ffe8f59 100644 --- a/SettingsManagement/OemSettings.cs +++ b/SettingsManagement/OemSettings.cs @@ -172,7 +172,9 @@ namespace MatterHackers.MatterControl.SettingsManagement if (!File.Exists(cachePath)) { - string profileJson = await ApplicationController.DownloadPublicProfileAsync(profileKey); + var profile = await ApplicationController.DownloadPublicProfileAsync(profileKey); + + string profileJson = JsonConvert.SerializeObject(profile); ; if (!string.IsNullOrEmpty(profileJson)) { File.WriteAllText(cachePath, profileJson); diff --git a/SlicerConfiguration/Settings/ProfileManager.cs b/SlicerConfiguration/Settings/ProfileManager.cs index f520474a7..ab706050f 100644 --- a/SlicerConfiguration/Settings/ProfileManager.cs +++ b/SlicerConfiguration/Settings/ProfileManager.cs @@ -382,14 +382,15 @@ namespace MatterHackers.MatterControl.SlicerConfiguration "profiles", async () => { - string responseText = null; - if(!File.Exists(cachePath)) + if(File.Exists(cachePath)) + { + return null; + } + else { // If the cache file for the current deviceToken does not exist, attempt to download it - responseText = await ApplicationController.DownloadPublicProfileAsync(deviceToken); + return await ApplicationController.DownloadPublicProfileAsync(deviceToken); } - - return responseText; }, Path.Combine("Profiles",make, model + ProfileManager.ProfileExtension)); } diff --git a/SlicerConfiguration/Settings/SettingsHelpers.cs b/SlicerConfiguration/Settings/SettingsHelpers.cs index 2de7f8ade..d3929c3dd 100644 --- a/SlicerConfiguration/Settings/SettingsHelpers.cs +++ b/SlicerConfiguration/Settings/SettingsHelpers.cs @@ -85,6 +85,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public const string active_theme_index = nameof(active_theme_index); public const string show_reset_connection = nameof(show_reset_connection); public const string start_gcode = nameof(start_gcode); + public const string oem_profile_token = nameof(oem_profile_token); }; public class SettingsHelpers diff --git a/Tests/MatterControl.AutomationTests/RetrievePublicProfileTest.cs b/Tests/MatterControl.AutomationTests/RetrievePublicProfileTest.cs index 536fe5a8f..38e297f80 100644 --- a/Tests/MatterControl.AutomationTests/RetrievePublicProfileTest.cs +++ b/Tests/MatterControl.AutomationTests/RetrievePublicProfileTest.cs @@ -67,9 +67,9 @@ namespace MatterControl.Tests.MatterControl } // Test will fail until mechanism can be created that exposes MHWebservices to vanilla MatterControl or until these tests are moved to MCCentral - string recievedPrinterProfile = await ApplicationController.DownloadPublicProfileAsync(deviceToken); + var recievedPrinterProfile = await ApplicationController.DownloadPublicProfileAsync(deviceToken); - Assert.IsNotNullOrEmpty(recievedPrinterProfile); + Assert.IsNotNull(recievedPrinterProfile); //Assert.AreEqual(expectedProfilePath, recievedProfilePath,"Received Profile path does not match expected path."); //Assert.IsTrue(File.Exists(expectedProfilePath));