Make LoadCacheable collector return instance of T rather than string

- Issue #1195, #1188
This commit is contained in:
John Lewin 2016-07-26 12:54:16 -07:00
parent b1c51490a8
commit 4ece8d18a7
5 changed files with 21 additions and 17 deletions

View file

@ -48,6 +48,8 @@ using System.Threading.Tasks;
namespace MatterHackers.MatterControl namespace MatterHackers.MatterControl
{ {
using OemProfileDictionary = Dictionary<string, Dictionary<string, string>>;
public abstract class ApplicationView : GuiWidget public abstract class ApplicationView : GuiWidget
{ {
public abstract void AddElements(); public abstract void AddElements();
@ -179,8 +181,8 @@ namespace MatterHackers.MatterControl
public static Func<string, Task<Dictionary<string, string>>> GetProfileHistory; public static Func<string, Task<Dictionary<string, string>>> GetProfileHistory;
public static Func<PrinterInfo,string, Task> GetPrinterProfile; public static Func<PrinterInfo,string, Task> GetPrinterProfile;
public static Func<Task> SyncPrinterProfiles; public static Func<Task> SyncPrinterProfiles;
public static Func<Task<string>> GetPublicProfileList; public static Func<Task<OemProfileDictionary>> GetPublicProfileList;
public static Func<string, Task<string>> DownloadPublicProfileAsync; public static Func<string, Task<PrinterSettings>> DownloadPublicProfileAsync;
public SlicePresetsWindow EditMaterialPresetsWindow { get; set; } public SlicePresetsWindow EditMaterialPresetsWindow { get; set; }
@ -221,7 +223,7 @@ namespace MatterHackers.MatterControl
/// </summary> /// </summary>
/// <param name="collector">The custom collector function to load the content</param> /// <param name="collector">The custom collector function to load the content</param>
/// <returns></returns> /// <returns></returns>
public async static Task<T> LoadCacheableAsync<T>(string cacheKey, string cacheScope, Func<Task<string>> collector, string staticDataFallbackPath = null) where T : class public async static Task<T> LoadCacheableAsync<T>(string cacheKey, string cacheScope, Func<Task<T>> collector, string staticDataFallbackPath = null) where T : class
{ {
string cacheDirectory = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache", cacheScope); string cacheDirectory = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache", cacheScope);
string cachePath = Path.Combine(cacheDirectory, cacheKey); string cachePath = Path.Combine(cacheDirectory, cacheKey);
@ -232,15 +234,13 @@ namespace MatterHackers.MatterControl
try try
{ {
// Try to update the document // Try to update the document
string documentText = await collector(); T item = await collector();
if (!string.IsNullOrEmpty(documentText)) if (item != null)
{ {
var results = JsonConvert.DeserializeObject<T>(documentText);
// update cache on success // update cache on success
File.WriteAllText(cachePath, documentText); File.WriteAllText(cachePath, JsonConvert.SerializeObject(item));
return results; return item;
} }
} }
catch catch

View file

@ -172,7 +172,9 @@ namespace MatterHackers.MatterControl.SettingsManagement
if (!File.Exists(cachePath)) 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)) if (!string.IsNullOrEmpty(profileJson))
{ {
File.WriteAllText(cachePath, profileJson); File.WriteAllText(cachePath, profileJson);

View file

@ -382,14 +382,15 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
"profiles", "profiles",
async () => 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 // 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)); Path.Combine("Profiles",make, model + ProfileManager.ProfileExtension));
} }

View file

@ -85,6 +85,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
public const string active_theme_index = nameof(active_theme_index); public const string active_theme_index = nameof(active_theme_index);
public const string show_reset_connection = nameof(show_reset_connection); public const string show_reset_connection = nameof(show_reset_connection);
public const string start_gcode = nameof(start_gcode); public const string start_gcode = nameof(start_gcode);
public const string oem_profile_token = nameof(oem_profile_token);
}; };
public class SettingsHelpers public class SettingsHelpers

View file

@ -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 // 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.AreEqual(expectedProfilePath, recievedProfilePath,"Received Profile path does not match expected path.");
//Assert.IsTrue(File.Exists(expectedProfilePath)); //Assert.IsTrue(File.Exists(expectedProfilePath));