From e6a80f28eb132aba69564a858879ae1bf45cbc2e Mon Sep 17 00:00:00 2001 From: Seulgi Kim Date: Sun, 7 Apr 2019 00:50:18 -0700 Subject: [PATCH] Only restore from saved units if they are valid in the current available units. --- src/CalcManager/UnitConverter.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/CalcManager/UnitConverter.cpp b/src/CalcManager/UnitConverter.cpp index 0548719a..6c87406a 100644 --- a/src/CalcManager/UnitConverter.cpp +++ b/src/CalcManager/UnitConverter.cpp @@ -403,12 +403,29 @@ void UnitConverter::RestoreUserPreferences(const wstring& userPreferences) } vector outerTokens = StringToVector(userPreferences, L"|"); - if (outerTokens.size() == 3) + if (outerTokens.size() != 3) { - m_fromType = StringToUnit(outerTokens[0]); - m_toType = StringToUnit(outerTokens[1]); - m_currentCategory = StringToCategory(outerTokens[2]); + return; } + auto fromType = StringToUnit(outerTokens[0]); + auto toType = StringToUnit(outerTokens[1]); + m_currentCategory = StringToCategory(outerTokens[2]); + + // Only restore from the saved units if they are valid in the current available units. + auto itr = m_categoryToUnits.find(m_currentCategory); + if (itr != m_categoryToUnits.end()) + { + auto curUnits = itr->second; + if (find(curUnits.begin(), curUnits.end(), fromType) != curUnits.end()) + { + m_fromType = fromType; + } + if (find(curUnits.begin(), curUnits.end(), toType) != curUnits.end()) + { + m_toType = toType; + } + } + } ///