Refactor LoadProfileAsync to reuse LoadWithoutRecovery

This commit is contained in:
John Lewin 2016-08-11 11:23:05 -07:00
parent 6079694be5
commit ea381ef7b1

View file

@ -257,51 +257,38 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
/// <returns></returns> /// <returns></returns>
public static async Task<PrinterSettings> LoadProfileAsync(string profileID, bool useActiveInstance = true) public static async Task<PrinterSettings> LoadProfileAsync(string profileID, bool useActiveInstance = true)
{ {
var printerInfo = ProfileManager.Instance[profileID];
// Only load profiles by ID that are defined in the profiles document
if (printerInfo == null)
{
return null;
}
if (useActiveInstance && ActiveSliceSettings.Instance?.ID == profileID) if (useActiveInstance && ActiveSliceSettings.Instance?.ID == profileID)
{ {
return ActiveSliceSettings.Instance; return ActiveSliceSettings.Instance;
} }
PrinterSettings printerSettings; // Only load profiles by ID that are defined in the profiles document
var printerInfo = ProfileManager.Instance[profileID];
string profilePath = Path.Combine(ProfilesPath, profileID + ProfileManager.ProfileExtension); if (printerInfo == null)
if (!File.Exists(profilePath))
{ {
// Attempt to load from MCWS if missing on disk return null;
printerSettings = await ApplicationController.GetPrinterProfileAsync(printerInfo, null);
if (printerSettings != null)
{
// If successful, persist downloaded profile
printerSettings.Save();
}
return printerSettings;
} }
// LoadOrRecoverProfile - if exists on disk, attempt to load or fall back using recovery logic // Attempt to load from disk, pull from the web or fall back using recovery logic
printerSettings = PrinterSettings.LoadFile(profilePath, performMigrations: true); PrinterSettings printerSettings = LoadWithoutRecovery(profileID);
if (printerSettings != null) if (printerSettings != null)
{ {
return printerSettings; return printerSettings;
} }
else else
{ {
int delayDuration = MatterControlApplication.IsLoading ? 4 : 0; // Attempt to load from MCWS if missing on disk
printerSettings = await ApplicationController.GetPrinterProfileAsync?.Invoke (printerInfo, null);
// Schedule a recovery rather than blocking until the MCWS and/or OemProfile restore complete if (printerSettings != null)
UiThread.RunOnIdle(() => PrinterSettings.RecoverProfile(printerInfo), delayDuration); {
// If successful, persist downloaded profile and return
// Return a short lived profile which should be reset after recovery printerSettings.Save();
return ProfileManager.LoadEmptyProfile(); return printerSettings;
}
} }
// Otherwise attempt to recover to a working profile
return await PrinterSettings.RecoverProfile(printerInfo);
} }
internal static bool ImportFromExisting(string settingsFilePath) internal static bool ImportFromExisting(string settingsFilePath)