mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-21 22:03:11 -07:00
Refacto: less indent levels
This commit is contained in:
parent
c6b770eec8
commit
2cfb2f2973
2 changed files with 384 additions and 375 deletions
|
@ -483,22 +483,24 @@ namespace CalculationManager
|
|||
void CalculatorManager::MemorizeNumber()
|
||||
{
|
||||
m_savedCommands.push_back(MEMORY_COMMAND_TO_UNSIGNED_CHAR(MemoryCommand::MemorizeNumber));
|
||||
if (!(m_currentCalculatorEngine->FInErrorState()))
|
||||
{
|
||||
m_currentCalculatorEngine->ProcessCommand(IDC_STORE);
|
||||
|
||||
auto memoryObjectPtr = m_currentCalculatorEngine->PersistedMemObject();
|
||||
if (memoryObjectPtr != nullptr)
|
||||
{
|
||||
m_memorizedNumbers.insert(m_memorizedNumbers.begin(), *memoryObjectPtr);
|
||||
}
|
||||
|
||||
if (m_memorizedNumbers.size() > m_maximumMemorySize)
|
||||
{
|
||||
m_memorizedNumbers.resize(m_maximumMemorySize);
|
||||
}
|
||||
this->SetMemorizedNumbersString();
|
||||
if (m_currentCalculatorEngine->FInErrorState()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_currentCalculatorEngine->ProcessCommand(IDC_STORE);
|
||||
|
||||
auto memoryObjectPtr = m_currentCalculatorEngine->PersistedMemObject();
|
||||
if (memoryObjectPtr != nullptr)
|
||||
{
|
||||
m_memorizedNumbers.insert(m_memorizedNumbers.begin(), *memoryObjectPtr);
|
||||
}
|
||||
|
||||
if (m_memorizedNumbers.size() > m_maximumMemorySize)
|
||||
{
|
||||
m_memorizedNumbers.resize(m_maximumMemorySize);
|
||||
}
|
||||
this->SetMemorizedNumbersString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -509,11 +511,13 @@ namespace CalculationManager
|
|||
void CalculatorManager::MemorizedNumberLoad(_In_ unsigned int indexOfMemory)
|
||||
{
|
||||
SaveMemoryCommand(MemoryCommand::MemorizedNumberLoad, indexOfMemory);
|
||||
if (!(m_currentCalculatorEngine->FInErrorState()))
|
||||
{
|
||||
this->MemorizedNumberSelect(indexOfMemory);
|
||||
m_currentCalculatorEngine->ProcessCommand(IDC_RECALL);
|
||||
|
||||
if (m_currentCalculatorEngine->FInErrorState()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this->MemorizedNumberSelect(indexOfMemory);
|
||||
m_currentCalculatorEngine->ProcessCommand(IDC_RECALL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -525,24 +529,26 @@ namespace CalculationManager
|
|||
void CalculatorManager::MemorizedNumberAdd(_In_ unsigned int indexOfMemory)
|
||||
{
|
||||
SaveMemoryCommand(MemoryCommand::MemorizedNumberAdd, indexOfMemory);
|
||||
if (!(m_currentCalculatorEngine->FInErrorState()))
|
||||
{
|
||||
if (m_memorizedNumbers.empty())
|
||||
{
|
||||
this->MemorizeNumber();
|
||||
}
|
||||
else
|
||||
{
|
||||
this->MemorizedNumberSelect(indexOfMemory);
|
||||
m_currentCalculatorEngine->ProcessCommand(IDC_MPLUS);
|
||||
|
||||
this->MemorizedNumberChanged(indexOfMemory);
|
||||
|
||||
this->SetMemorizedNumbersString();
|
||||
}
|
||||
|
||||
m_displayCallback->MemoryItemChanged(indexOfMemory);
|
||||
if (m_currentCalculatorEngine->FInErrorState()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_memorizedNumbers.empty())
|
||||
{
|
||||
this->MemorizeNumber();
|
||||
}
|
||||
else
|
||||
{
|
||||
this->MemorizedNumberSelect(indexOfMemory);
|
||||
m_currentCalculatorEngine->ProcessCommand(IDC_MPLUS);
|
||||
|
||||
this->MemorizedNumberChanged(indexOfMemory);
|
||||
|
||||
this->SetMemorizedNumbersString();
|
||||
}
|
||||
|
||||
m_displayCallback->MemoryItemChanged(indexOfMemory);
|
||||
}
|
||||
|
||||
void CalculatorManager::MemorizedNumberClear(_In_ unsigned int indexOfMemory)
|
||||
|
@ -563,27 +569,29 @@ namespace CalculationManager
|
|||
void CalculatorManager::MemorizedNumberSubtract(_In_ unsigned int indexOfMemory)
|
||||
{
|
||||
SaveMemoryCommand(MemoryCommand::MemorizedNumberSubtract, indexOfMemory);
|
||||
if (!(m_currentCalculatorEngine->FInErrorState()))
|
||||
{
|
||||
// To add negative of the number on display to the memory -x = x - 2x
|
||||
if (m_memorizedNumbers.empty())
|
||||
{
|
||||
this->MemorizeNumber();
|
||||
this->MemorizedNumberSubtract(0);
|
||||
this->MemorizedNumberSubtract(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->MemorizedNumberSelect(indexOfMemory);
|
||||
m_currentCalculatorEngine->ProcessCommand(IDC_MMINUS);
|
||||
|
||||
this->MemorizedNumberChanged(indexOfMemory);
|
||||
|
||||
this->SetMemorizedNumbersString();
|
||||
}
|
||||
|
||||
m_displayCallback->MemoryItemChanged(indexOfMemory);
|
||||
if (m_currentCalculatorEngine->FInErrorState()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// To add negative of the number on display to the memory -x = x - 2x
|
||||
if (m_memorizedNumbers.empty())
|
||||
{
|
||||
this->MemorizeNumber();
|
||||
this->MemorizedNumberSubtract(0);
|
||||
this->MemorizedNumberSubtract(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->MemorizedNumberSelect(indexOfMemory);
|
||||
m_currentCalculatorEngine->ProcessCommand(IDC_MMINUS);
|
||||
|
||||
this->MemorizedNumberChanged(indexOfMemory);
|
||||
|
||||
this->SetMemorizedNumbersString();
|
||||
}
|
||||
|
||||
m_displayCallback->MemoryItemChanged(indexOfMemory);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -606,11 +614,12 @@ namespace CalculationManager
|
|||
/// <param name="indexOfMemory">Index of the target memory</param>
|
||||
void CalculatorManager::MemorizedNumberSelect(_In_ unsigned int indexOfMemory)
|
||||
{
|
||||
if (!(m_currentCalculatorEngine->FInErrorState()))
|
||||
{
|
||||
auto memoryObject = m_memorizedNumbers.at(indexOfMemory);
|
||||
m_currentCalculatorEngine->PersistedMemObject(memoryObject);
|
||||
if (m_currentCalculatorEngine->FInErrorState()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto memoryObject = m_memorizedNumbers.at(indexOfMemory);
|
||||
m_currentCalculatorEngine->PersistedMemObject(memoryObject);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -620,13 +629,14 @@ namespace CalculationManager
|
|||
/// <param name="indexOfMemory">Index of the target memory</param>
|
||||
void CalculatorManager::MemorizedNumberChanged(_In_ unsigned int indexOfMemory)
|
||||
{
|
||||
if (!(m_currentCalculatorEngine->FInErrorState()))
|
||||
if (m_currentCalculatorEngine->FInErrorState()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto memoryObject = m_currentCalculatorEngine->PersistedMemObject();
|
||||
if (memoryObject != nullptr)
|
||||
{
|
||||
auto memoryObject = m_currentCalculatorEngine->PersistedMemObject();
|
||||
if (memoryObject != nullptr)
|
||||
{
|
||||
m_memorizedNumbers.at(indexOfMemory) = *memoryObject;
|
||||
}
|
||||
m_memorizedNumbers.at(indexOfMemory) = *memoryObject;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -109,22 +109,8 @@ CategorySelectionInitializer UnitConverter::SetCurrentCategory(const Category& i
|
|||
vector<Unit>& unitVector = m_categoryToUnits[m_currentCategory];
|
||||
for (unsigned int i = 0; i < unitVector.size(); i++)
|
||||
{
|
||||
if (unitVector[i].id == m_fromType.id)
|
||||
{
|
||||
unitVector[i].isConversionSource = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
unitVector[i].isConversionSource = false;
|
||||
}
|
||||
if (unitVector[i].id == m_toType.id)
|
||||
{
|
||||
unitVector[i].isConversionTarget = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
unitVector[i].isConversionTarget = false;
|
||||
}
|
||||
unitVector[i].isConversionSource = (unitVector[i].id == m_fromType.id);
|
||||
unitVector[i].isConversionTarget = (unitVector[i].id == m_toType.id);
|
||||
}
|
||||
m_currentCategory = input;
|
||||
if (!m_currentCategory.supportsNegative && m_currentDisplay.front() == L'-')
|
||||
|
@ -156,21 +142,23 @@ Category UnitConverter::GetCurrentCategory()
|
|||
/// <param name="toType">Unit struct we are converting to</param>
|
||||
void UnitConverter::SetCurrentUnitTypes(const Unit& fromType, const Unit& toType)
|
||||
{
|
||||
if (CheckLoad())
|
||||
if (!CheckLoad())
|
||||
{
|
||||
m_fromType = fromType;
|
||||
m_toType = toType;
|
||||
Calculate();
|
||||
|
||||
UpdateCurrencySymbols();
|
||||
UpdateViewModel();
|
||||
return;
|
||||
}
|
||||
|
||||
m_fromType = fromType;
|
||||
m_toType = toType;
|
||||
Calculate();
|
||||
|
||||
UpdateCurrencySymbols();
|
||||
UpdateViewModel();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Switches the active field, indicating that we are now entering data into
|
||||
/// what was originally the return field, and storing results into what was
|
||||
/// originally the current field. We swap appropriate values,
|
||||
/// originally the current field. We swap appropriate values,
|
||||
/// but do not callback, as values have not changed.
|
||||
/// </summary>
|
||||
/// <param name="newValue">
|
||||
|
@ -181,22 +169,24 @@ void UnitConverter::SetCurrentUnitTypes(const Unit& fromType, const Unit& toType
|
|||
/// </param>
|
||||
void UnitConverter::SwitchActive(const wstring& newValue)
|
||||
{
|
||||
if (CheckLoad())
|
||||
if (!CheckLoad())
|
||||
{
|
||||
swap(m_fromType, m_toType);
|
||||
swap(m_currentHasDecimal, m_returnHasDecimal);
|
||||
m_returnDisplay = m_currentDisplay;
|
||||
m_currentDisplay = newValue;
|
||||
m_currentHasDecimal = (m_currentDisplay.find(L'.') != m_currentDisplay.npos);
|
||||
m_switchedActive = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_currencyDataLoader != nullptr && m_vmCurrencyCallback != nullptr)
|
||||
{
|
||||
shared_ptr<ICurrencyConverterDataLoader> currencyDataLoader = GetCurrencyConverterDataLoader();
|
||||
const pair<wstring, wstring> currencyRatios = currencyDataLoader->GetCurrencyRatioEquality(m_fromType, m_toType);
|
||||
swap(m_fromType, m_toType);
|
||||
swap(m_currentHasDecimal, m_returnHasDecimal);
|
||||
m_returnDisplay = m_currentDisplay;
|
||||
m_currentDisplay = newValue;
|
||||
m_currentHasDecimal = (m_currentDisplay.find(L'.') != m_currentDisplay.npos);
|
||||
m_switchedActive = true;
|
||||
|
||||
m_vmCurrencyCallback->CurrencyRatiosCallback(currencyRatios.first, currencyRatios.second);
|
||||
}
|
||||
if (m_currencyDataLoader != nullptr && m_vmCurrencyCallback != nullptr)
|
||||
{
|
||||
shared_ptr<ICurrencyConverterDataLoader> currencyDataLoader = GetCurrencyConverterDataLoader();
|
||||
const pair<wstring, wstring> currencyRatios = currencyDataLoader->GetCurrencyRatioEquality(m_fromType, m_toType);
|
||||
|
||||
m_vmCurrencyCallback->CurrencyRatiosCallback(currencyRatios.first, currencyRatios.second);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -291,55 +281,53 @@ wstring UnitConverter::ConversionDataToString(ConversionData d, const wchar_t *
|
|||
/// </summary>
|
||||
wstring UnitConverter::Serialize()
|
||||
{
|
||||
if (CheckLoad())
|
||||
{
|
||||
wstringstream out(wstringstream::out);
|
||||
const wchar_t * delimiter = L";";
|
||||
|
||||
out << UnitToString(m_fromType, delimiter) << "|";
|
||||
out << UnitToString(m_toType, delimiter) << "|";
|
||||
out << CategoryToString(m_currentCategory, delimiter) << "|";
|
||||
out << std::to_wstring(m_currentHasDecimal) << delimiter << std::to_wstring(m_returnHasDecimal) << delimiter << std::to_wstring(m_switchedActive) << delimiter;
|
||||
out << m_currentDisplay << delimiter << m_returnDisplay << delimiter << "|";
|
||||
wstringstream categoryString(wstringstream::out);
|
||||
wstringstream categoryToUnitString(wstringstream::out);;
|
||||
wstringstream unitToUnitToDoubleString(wstringstream::out);;
|
||||
for (const Category& c : m_categories)
|
||||
{
|
||||
categoryString << CategoryToString(c, delimiter) << ",";
|
||||
}
|
||||
|
||||
for (const auto& cur : m_categoryToUnits)
|
||||
{
|
||||
categoryToUnitString << CategoryToString(cur.first, delimiter) << "[";
|
||||
for (const Unit& u : cur.second)
|
||||
{
|
||||
categoryToUnitString << UnitToString(u, delimiter) << ",";
|
||||
}
|
||||
categoryToUnitString << "[" << "]";
|
||||
}
|
||||
|
||||
for (const auto& cur : m_ratioMap)
|
||||
{
|
||||
unitToUnitToDoubleString << UnitToString(cur.first, delimiter) << "[";
|
||||
for (const auto& curConversion : cur.second)
|
||||
{
|
||||
unitToUnitToDoubleString << UnitToString(curConversion.first, delimiter) << ":";
|
||||
unitToUnitToDoubleString << ConversionDataToString(curConversion.second, delimiter) << ":,";
|
||||
}
|
||||
unitToUnitToDoubleString << "[" << "]";
|
||||
}
|
||||
|
||||
out << categoryString.str() << "|";
|
||||
out << categoryToUnitString.str() << "|";
|
||||
out << unitToUnitToDoubleString.str() << "|";
|
||||
wstring test = out.str();
|
||||
return test;
|
||||
}
|
||||
else
|
||||
if (!CheckLoad())
|
||||
{
|
||||
return wstring();
|
||||
}
|
||||
|
||||
wstringstream out(wstringstream::out);
|
||||
const wchar_t * delimiter = L";";
|
||||
|
||||
out << UnitToString(m_fromType, delimiter) << "|";
|
||||
out << UnitToString(m_toType, delimiter) << "|";
|
||||
out << CategoryToString(m_currentCategory, delimiter) << "|";
|
||||
out << std::to_wstring(m_currentHasDecimal) << delimiter << std::to_wstring(m_returnHasDecimal) << delimiter << std::to_wstring(m_switchedActive) << delimiter;
|
||||
out << m_currentDisplay << delimiter << m_returnDisplay << delimiter << "|";
|
||||
wstringstream categoryString(wstringstream::out);
|
||||
wstringstream categoryToUnitString(wstringstream::out);;
|
||||
wstringstream unitToUnitToDoubleString(wstringstream::out);;
|
||||
for (const Category& c : m_categories)
|
||||
{
|
||||
categoryString << CategoryToString(c, delimiter) << ",";
|
||||
}
|
||||
|
||||
for (const auto& cur : m_categoryToUnits)
|
||||
{
|
||||
categoryToUnitString << CategoryToString(cur.first, delimiter) << "[";
|
||||
for (const Unit& u : cur.second)
|
||||
{
|
||||
categoryToUnitString << UnitToString(u, delimiter) << ",";
|
||||
}
|
||||
categoryToUnitString << "[" << "]";
|
||||
}
|
||||
|
||||
for (const auto& cur : m_ratioMap)
|
||||
{
|
||||
unitToUnitToDoubleString << UnitToString(cur.first, delimiter) << "[";
|
||||
for (const auto& curConversion : cur.second)
|
||||
{
|
||||
unitToUnitToDoubleString << UnitToString(curConversion.first, delimiter) << ":";
|
||||
unitToUnitToDoubleString << ConversionDataToString(curConversion.second, delimiter) << ":,";
|
||||
}
|
||||
unitToUnitToDoubleString << "[" << "]";
|
||||
}
|
||||
|
||||
out << categoryString.str() << "|";
|
||||
out << categoryToUnitString.str() << "|";
|
||||
out << unitToUnitToDoubleString.str() << "|";
|
||||
wstring test = out.str();
|
||||
return test;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -349,55 +337,58 @@ wstring UnitConverter::Serialize()
|
|||
void UnitConverter::DeSerialize(const wstring& serializedData)
|
||||
{
|
||||
Reset();
|
||||
|
||||
if (!serializedData.empty())
|
||||
{
|
||||
vector<wstring> outerTokens = StringToVector(serializedData, L"|");
|
||||
assert(outerTokens.size() == EXPECTEDSERIALIZEDTOKENCOUNT);
|
||||
m_fromType = StringToUnit(outerTokens[0]);
|
||||
m_toType = StringToUnit(outerTokens[1]);
|
||||
m_currentCategory = StringToCategory(outerTokens[2]);
|
||||
vector<wstring> stateDataTokens = StringToVector(outerTokens[3], L";");
|
||||
assert(stateDataTokens.size() == EXPECTEDSTATEDATATOKENCOUNT);
|
||||
m_currentHasDecimal = (stateDataTokens[0].compare(L"1") == 0);
|
||||
m_returnHasDecimal = (stateDataTokens[1].compare(L"1") == 0);
|
||||
m_switchedActive = (stateDataTokens[2].compare(L"1") == 0);
|
||||
m_currentDisplay = stateDataTokens[3];
|
||||
m_returnDisplay = stateDataTokens[4];
|
||||
vector<wstring> categoryListTokens = StringToVector(outerTokens[4], L",");
|
||||
for (wstring token : categoryListTokens)
|
||||
{
|
||||
m_categories.push_back(StringToCategory(token));
|
||||
}
|
||||
vector<wstring> unitVectorTokens = StringToVector(outerTokens[5], L"]");
|
||||
for (wstring unitVector : unitVectorTokens)
|
||||
{
|
||||
vector<wstring> mapcomponents = StringToVector(unitVector, L"[");
|
||||
assert(mapcomponents.size() == EXPECTEDMAPCOMPONENTTOKENCOUNT);
|
||||
Category key = StringToCategory(mapcomponents[0]);
|
||||
vector<wstring> units = StringToVector(mapcomponents[1], L",");
|
||||
for (wstring unit : units)
|
||||
{
|
||||
m_categoryToUnits[key].push_back(StringToUnit(unit));
|
||||
}
|
||||
}
|
||||
vector<wstring> ratioMapTokens = StringToVector(outerTokens[6], L"]");
|
||||
for (wstring token : ratioMapTokens)
|
||||
{
|
||||
vector<wstring> ratioMapComponentTokens = StringToVector(token, L"[");
|
||||
assert(ratioMapComponentTokens.size() == EXPECTEDMAPCOMPONENTTOKENCOUNT);
|
||||
Unit key = StringToUnit(ratioMapComponentTokens[0]);
|
||||
vector<wstring> ratioMapList = StringToVector(ratioMapComponentTokens[1], L",");
|
||||
for (wstring subtoken : ratioMapList)
|
||||
{
|
||||
vector<wstring> ratioMapSubComponentTokens = StringToVector(subtoken, L":");
|
||||
assert(ratioMapSubComponentTokens.size() == EXPECTEDMAPCOMPONENTTOKENCOUNT);
|
||||
Unit subkey = StringToUnit(ratioMapSubComponentTokens[0]);
|
||||
ConversionData conversion = StringToConversionData(ratioMapSubComponentTokens[1]);
|
||||
m_ratioMap[key][subkey] = conversion;
|
||||
}
|
||||
}
|
||||
UpdateViewModel();
|
||||
return;
|
||||
}
|
||||
|
||||
vector<wstring> outerTokens = StringToVector(serializedData, L"|");
|
||||
assert(outerTokens.size() == EXPECTEDSERIALIZEDTOKENCOUNT);
|
||||
m_fromType = StringToUnit(outerTokens[0]);
|
||||
m_toType = StringToUnit(outerTokens[1]);
|
||||
m_currentCategory = StringToCategory(outerTokens[2]);
|
||||
vector<wstring> stateDataTokens = StringToVector(outerTokens[3], L";");
|
||||
assert(stateDataTokens.size() == EXPECTEDSTATEDATATOKENCOUNT);
|
||||
m_currentHasDecimal = (stateDataTokens[0].compare(L"1") == 0);
|
||||
m_returnHasDecimal = (stateDataTokens[1].compare(L"1") == 0);
|
||||
m_switchedActive = (stateDataTokens[2].compare(L"1") == 0);
|
||||
m_currentDisplay = stateDataTokens[3];
|
||||
m_returnDisplay = stateDataTokens[4];
|
||||
vector<wstring> categoryListTokens = StringToVector(outerTokens[4], L",");
|
||||
for (wstring token : categoryListTokens)
|
||||
{
|
||||
m_categories.push_back(StringToCategory(token));
|
||||
}
|
||||
vector<wstring> unitVectorTokens = StringToVector(outerTokens[5], L"]");
|
||||
for (wstring unitVector : unitVectorTokens)
|
||||
{
|
||||
vector<wstring> mapcomponents = StringToVector(unitVector, L"[");
|
||||
assert(mapcomponents.size() == EXPECTEDMAPCOMPONENTTOKENCOUNT);
|
||||
Category key = StringToCategory(mapcomponents[0]);
|
||||
vector<wstring> units = StringToVector(mapcomponents[1], L",");
|
||||
for (wstring unit : units)
|
||||
{
|
||||
m_categoryToUnits[key].push_back(StringToUnit(unit));
|
||||
}
|
||||
}
|
||||
vector<wstring> ratioMapTokens = StringToVector(outerTokens[6], L"]");
|
||||
for (wstring token : ratioMapTokens)
|
||||
{
|
||||
vector<wstring> ratioMapComponentTokens = StringToVector(token, L"[");
|
||||
assert(ratioMapComponentTokens.size() == EXPECTEDMAPCOMPONENTTOKENCOUNT);
|
||||
Unit key = StringToUnit(ratioMapComponentTokens[0]);
|
||||
vector<wstring> ratioMapList = StringToVector(ratioMapComponentTokens[1], L",");
|
||||
for (wstring subtoken : ratioMapList)
|
||||
{
|
||||
vector<wstring> ratioMapSubComponentTokens = StringToVector(subtoken, L":");
|
||||
assert(ratioMapSubComponentTokens.size() == EXPECTEDMAPCOMPONENTTOKENCOUNT);
|
||||
Unit subkey = StringToUnit(ratioMapSubComponentTokens[0]);
|
||||
ConversionData conversion = StringToConversionData(ratioMapSubComponentTokens[1]);
|
||||
m_ratioMap[key][subkey] = conversion;
|
||||
}
|
||||
}
|
||||
UpdateViewModel();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -406,21 +397,23 @@ void UnitConverter::DeSerialize(const wstring& serializedData)
|
|||
/// <param name="userPreferences">wstring holding the serialized data. If it does not have expected number of parameters, we will ignore it</param>
|
||||
void UnitConverter::RestoreUserPreferences(const wstring& userPreferences)
|
||||
{
|
||||
if (!userPreferences.empty())
|
||||
if (userPreferences.empty())
|
||||
{
|
||||
vector<wstring> outerTokens = StringToVector(userPreferences, L"|");
|
||||
if (outerTokens.size() == 3)
|
||||
{
|
||||
m_fromType = StringToUnit(outerTokens[0]);
|
||||
m_toType = StringToUnit(outerTokens[1]);
|
||||
m_currentCategory = StringToCategory(outerTokens[2]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
vector<wstring> outerTokens = StringToVector(userPreferences, L"|");
|
||||
if (outerTokens.size() == 3)
|
||||
{
|
||||
m_fromType = StringToUnit(outerTokens[0]);
|
||||
m_toType = StringToUnit(outerTokens[1]);
|
||||
m_currentCategory = StringToCategory(outerTokens[2]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the Category and Associated Units in the converter and returns it as a string
|
||||
/// </summary>
|
||||
/// </summary>
|
||||
wstring UnitConverter::SaveUserPreferences()
|
||||
{
|
||||
wstringstream out(wstringstream::out);
|
||||
|
@ -505,142 +498,144 @@ void UnitConverter::SendCommand(Command command)
|
|||
{
|
||||
if (CheckLoad())
|
||||
{
|
||||
//TODO: Localization of characters
|
||||
bool clearFront = false;
|
||||
if (m_currentDisplay == L"0")
|
||||
return;
|
||||
}
|
||||
|
||||
//TODO: Localization of characters
|
||||
bool clearFront = false;
|
||||
if (m_currentDisplay == L"0")
|
||||
{
|
||||
clearFront = true;
|
||||
}
|
||||
bool clearBack = false;
|
||||
if ((m_currentHasDecimal && m_currentDisplay.size() - 1 >= MAXIMUMDIGITSALLOWED) || (!m_currentHasDecimal && m_currentDisplay.size() >= MAXIMUMDIGITSALLOWED))
|
||||
{
|
||||
clearBack = true;
|
||||
}
|
||||
if (command != Command::Negate && m_switchedActive)
|
||||
{
|
||||
ClearValues();
|
||||
m_switchedActive = false;
|
||||
clearFront = true;
|
||||
clearBack = false;
|
||||
}
|
||||
switch (command)
|
||||
{
|
||||
case Command::Zero:
|
||||
m_currentDisplay += L"0";
|
||||
break;
|
||||
|
||||
case Command::One:
|
||||
m_currentDisplay += L"1";
|
||||
break;
|
||||
|
||||
case Command::Two:
|
||||
m_currentDisplay += L"2";
|
||||
break;
|
||||
|
||||
case Command::Three:
|
||||
m_currentDisplay += L"3";
|
||||
break;
|
||||
|
||||
case Command::Four:
|
||||
m_currentDisplay += L"4";
|
||||
break;
|
||||
|
||||
case Command::Five:
|
||||
m_currentDisplay += L"5";
|
||||
break;
|
||||
|
||||
case Command::Six:
|
||||
m_currentDisplay += L"6";
|
||||
break;
|
||||
|
||||
case Command::Seven:
|
||||
m_currentDisplay += L"7";
|
||||
break;
|
||||
|
||||
case Command::Eight:
|
||||
m_currentDisplay += L"8";
|
||||
break;
|
||||
|
||||
case Command::Nine:
|
||||
m_currentDisplay += L"9";
|
||||
break;
|
||||
|
||||
case Command::Decimal:
|
||||
clearFront = false;
|
||||
clearBack = false;
|
||||
if (!m_currentHasDecimal)
|
||||
{
|
||||
clearFront = true;
|
||||
m_currentDisplay += L".";
|
||||
m_currentHasDecimal = true;
|
||||
}
|
||||
bool clearBack = false;
|
||||
if ((m_currentHasDecimal && m_currentDisplay.size() - 1 >= MAXIMUMDIGITSALLOWED) || (!m_currentHasDecimal && m_currentDisplay.size() >= MAXIMUMDIGITSALLOWED))
|
||||
break;
|
||||
|
||||
case Command::Backspace:
|
||||
clearFront = false;
|
||||
clearBack = false;
|
||||
if ((m_currentDisplay.front() != '-' && m_currentDisplay.size() > 1) || m_currentDisplay.size() > 2)
|
||||
{
|
||||
clearBack = true;
|
||||
}
|
||||
if (command != Command::Negate && m_switchedActive)
|
||||
{
|
||||
ClearValues();
|
||||
m_switchedActive = false;
|
||||
clearFront = true;
|
||||
clearBack = false;
|
||||
}
|
||||
switch (command)
|
||||
{
|
||||
case Command::Zero:
|
||||
m_currentDisplay += L"0";
|
||||
break;
|
||||
|
||||
case Command::One:
|
||||
m_currentDisplay += L"1";
|
||||
break;
|
||||
|
||||
case Command::Two:
|
||||
m_currentDisplay += L"2";
|
||||
break;
|
||||
|
||||
case Command::Three:
|
||||
m_currentDisplay += L"3";
|
||||
break;
|
||||
|
||||
case Command::Four:
|
||||
m_currentDisplay += L"4";
|
||||
break;
|
||||
|
||||
case Command::Five:
|
||||
m_currentDisplay += L"5";
|
||||
break;
|
||||
|
||||
case Command::Six:
|
||||
m_currentDisplay += L"6";
|
||||
break;
|
||||
|
||||
case Command::Seven:
|
||||
m_currentDisplay += L"7";
|
||||
break;
|
||||
|
||||
case Command::Eight:
|
||||
m_currentDisplay += L"8";
|
||||
break;
|
||||
|
||||
case Command::Nine:
|
||||
m_currentDisplay += L"9";
|
||||
break;
|
||||
|
||||
case Command::Decimal:
|
||||
clearFront = false;
|
||||
clearBack = false;
|
||||
if (!m_currentHasDecimal)
|
||||
if (m_currentDisplay.back() == '.')
|
||||
{
|
||||
m_currentDisplay += L".";
|
||||
m_currentHasDecimal = true;
|
||||
m_currentHasDecimal = false;
|
||||
}
|
||||
break;
|
||||
m_currentDisplay.pop_back();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_currentDisplay = L"0";
|
||||
m_currentHasDecimal = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case Command::Backspace:
|
||||
clearFront = false;
|
||||
clearBack = false;
|
||||
if ((m_currentDisplay.front() != '-' && m_currentDisplay.size() > 1) || m_currentDisplay.size() > 2)
|
||||
case Command::Negate:
|
||||
clearFront = false;
|
||||
clearBack = false;
|
||||
if (m_currentCategory.supportsNegative)
|
||||
{
|
||||
if (m_currentDisplay.front() == '-')
|
||||
{
|
||||
if (m_currentDisplay.back() == '.')
|
||||
{
|
||||
m_currentHasDecimal = false;
|
||||
}
|
||||
m_currentDisplay.pop_back();
|
||||
m_currentDisplay.erase(0, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_currentDisplay = L"0";
|
||||
m_currentHasDecimal = false;
|
||||
m_currentDisplay.insert(0, 1, '-');
|
||||
}
|
||||
break;
|
||||
|
||||
case Command::Negate:
|
||||
clearFront = false;
|
||||
clearBack = false;
|
||||
if (m_currentCategory.supportsNegative)
|
||||
{
|
||||
if (m_currentDisplay.front() == '-')
|
||||
{
|
||||
m_currentDisplay.erase(0, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_currentDisplay.insert(0, 1, '-');
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Command::Clear:
|
||||
clearFront = false;
|
||||
clearBack = false;
|
||||
ClearValues();
|
||||
break;
|
||||
|
||||
case Command::Reset:
|
||||
clearFront = false;
|
||||
clearBack = false;
|
||||
ClearValues();
|
||||
Reset();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
if (clearFront)
|
||||
{
|
||||
m_currentDisplay.erase(0, 1);
|
||||
}
|
||||
if (clearBack)
|
||||
{
|
||||
m_currentDisplay.erase(m_currentDisplay.size() - 1, 1);
|
||||
m_vmCallback->MaxDigitsReached();
|
||||
}
|
||||
case Command::Clear:
|
||||
clearFront = false;
|
||||
clearBack = false;
|
||||
ClearValues();
|
||||
break;
|
||||
|
||||
Calculate();
|
||||
case Command::Reset:
|
||||
clearFront = false;
|
||||
clearBack = false;
|
||||
ClearValues();
|
||||
Reset();
|
||||
break;
|
||||
|
||||
UpdateViewModel();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (clearFront)
|
||||
{
|
||||
m_currentDisplay.erase(0, 1);
|
||||
}
|
||||
if (clearBack)
|
||||
{
|
||||
m_currentDisplay.erase(m_currentDisplay.size() - 1, 1);
|
||||
m_vmCallback->MaxDigitsReached();
|
||||
}
|
||||
|
||||
Calculate();
|
||||
|
||||
UpdateViewModel();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -748,13 +743,13 @@ vector<tuple<wstring, Unit>> UnitConverter::CalculateSuggested()
|
|||
//Sort the resulting list by absolute magnitude, breaking ties by choosing the positive value
|
||||
sort(intermediateVector.begin(), intermediateVector.end(), []
|
||||
(SuggestedValueIntermediate first, SuggestedValueIntermediate second)
|
||||
{
|
||||
if (abs(first.magnitude) == abs(second.magnitude))
|
||||
{
|
||||
return first.magnitude > second.magnitude;
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
if (abs(first.magnitude) == abs(second.magnitude))
|
||||
{
|
||||
return first.magnitude > second.magnitude;
|
||||
}
|
||||
else
|
||||
{
|
||||
return abs(first.magnitude) < abs(second.magnitude);
|
||||
}
|
||||
});
|
||||
|
@ -844,47 +839,49 @@ void UnitConverter::Reset()
|
|||
ClearValues();
|
||||
m_switchedActive = false;
|
||||
|
||||
if (!m_categories.empty())
|
||||
if (m_categories.empty())
|
||||
{
|
||||
m_currentCategory = m_categories[0];
|
||||
return;
|
||||
}
|
||||
|
||||
m_categoryToUnits.clear();
|
||||
m_ratioMap.clear();
|
||||
bool readyCategoryFound = false;
|
||||
for (const Category& category : m_categories)
|
||||
m_currentCategory = m_categories[0];
|
||||
|
||||
m_categoryToUnits.clear();
|
||||
m_ratioMap.clear();
|
||||
bool readyCategoryFound = false;
|
||||
for (const Category& category : m_categories)
|
||||
{
|
||||
shared_ptr<IConverterDataLoader> activeDataLoader = GetDataLoaderForCategory(category);
|
||||
if (activeDataLoader == nullptr)
|
||||
{
|
||||
shared_ptr<IConverterDataLoader> activeDataLoader = GetDataLoaderForCategory(category);
|
||||
if (activeDataLoader == nullptr)
|
||||
{
|
||||
// The data loader is different depending on the category, e.g. currency data loader
|
||||
// is different from the static data loader.
|
||||
// If there is no data loader for this category, continue.
|
||||
continue;
|
||||
}
|
||||
|
||||
vector<Unit> units = activeDataLoader->LoadOrderedUnits(category);
|
||||
m_categoryToUnits[category] = units;
|
||||
|
||||
// Just because the units are empty, doesn't mean the user can't select this category,
|
||||
// we just want to make sure we don't let an unready category be the default.
|
||||
if (!units.empty())
|
||||
{
|
||||
for (Unit u : units)
|
||||
{
|
||||
m_ratioMap[u] = activeDataLoader->LoadOrderedRatios(u);
|
||||
}
|
||||
|
||||
if (!readyCategoryFound)
|
||||
{
|
||||
m_currentCategory = category;
|
||||
readyCategoryFound = true;
|
||||
}
|
||||
}
|
||||
// The data loader is different depending on the category, e.g. currency data loader
|
||||
// is different from the static data loader.
|
||||
// If there is no data loader for this category, continue.
|
||||
continue;
|
||||
}
|
||||
|
||||
InitializeSelectedUnits();
|
||||
Calculate();
|
||||
vector<Unit> units = activeDataLoader->LoadOrderedUnits(category);
|
||||
m_categoryToUnits[category] = units;
|
||||
|
||||
// Just because the units are empty, doesn't mean the user can't select this category,
|
||||
// we just want to make sure we don't let an unready category be the default.
|
||||
if (!units.empty())
|
||||
{
|
||||
for (Unit u : units)
|
||||
{
|
||||
m_ratioMap[u] = activeDataLoader->LoadOrderedRatios(u);
|
||||
}
|
||||
|
||||
if (!readyCategoryFound)
|
||||
{
|
||||
m_currentCategory = category;
|
||||
readyCategoryFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
InitializeSelectedUnits();
|
||||
Calculate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -1029,22 +1026,24 @@ void UnitConverter::Calculate()
|
|||
/// <param name="input">wstring to trim</param>
|
||||
void UnitConverter::TrimString(wstring& returnString)
|
||||
{
|
||||
if (returnString.find(L'.') != m_returnDisplay.npos)
|
||||
if (returnString.find(L'.') == m_returnDisplay.npos)
|
||||
{
|
||||
wstring::iterator iter;
|
||||
for (iter = returnString.end() - 1; ;iter--)
|
||||
return;
|
||||
}
|
||||
|
||||
wstring::iterator iter;
|
||||
for (iter = returnString.end() - 1; ;iter--)
|
||||
{
|
||||
if (*iter != L'0')
|
||||
{
|
||||
if (*iter != L'0')
|
||||
{
|
||||
returnString.erase(iter + 1, returnString.end());
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (*(returnString.end()-1) == L'.')
|
||||
{
|
||||
returnString.erase(returnString.end()-1, returnString.end());
|
||||
returnString.erase(iter + 1, returnString.end());
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (*(returnString.end()-1) == L'.')
|
||||
{
|
||||
returnString.erase(returnString.end()-1, returnString.end());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue