Skip to content

Commit 2b0521e

Browse files
authored
Resolve an existing defect when restoring from snapshots (#2249)
* fix bug * fix a comment
1 parent 80015d2 commit 2b0521e

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

src/CalcViewModel/StandardCalculatorViewModel.cpp

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,30 +1813,53 @@ void CalculatorApp::ViewModel::StandardCalculatorViewModel::Snapshot::set(Calcul
18131813
m_standardCalculatorManager.SetHistoryItems(ToUnderlying(snapshot->CalcManager->HistoryItems));
18141814
}
18151815

1816-
std::vector<int> commands;
1817-
if (snapshot->ExpressionDisplay != nullptr && snapshot->ExpressionDisplay->Tokens->GetAt(snapshot->ExpressionDisplay->Tokens->Size - 1)->OpCodeName == L"=")
1818-
{
1819-
commands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->ExpressionDisplay->Commands));
1820-
}
1821-
if (commands.empty() && snapshot->DisplayCommands->Size > 0)
1822-
{
1823-
commands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->DisplayCommands));
1824-
}
1825-
for (auto cmd : commands)
1816+
if (snapshot->ExpressionDisplay != nullptr)
18261817
{
1827-
m_standardCalculatorManager.SendCommand(static_cast<Command>(cmd));
1818+
if (snapshot->DisplayCommands->Size == 0)
1819+
{
1820+
// use case: the current expression was evaluated before. load from history.
1821+
assert(!snapshot->PrimaryDisplay->IsError);
1822+
using RawTokenCollection = std::vector<std::pair<std::wstring, int>>;
1823+
RawTokenCollection rawTokens;
1824+
for (CalculatorApp::ViewModel::Snapshot::CalcManagerToken ^ token : snapshot->ExpressionDisplay->Tokens)
1825+
{
1826+
rawTokens.push_back(std::pair{ token->OpCodeName->Data(), token->CommandIndex });
1827+
}
1828+
auto tokens = std::make_shared<RawTokenCollection>(rawTokens);
1829+
auto commands = std::make_shared<std::vector<std::shared_ptr<IExpressionCommand>>>(ToUnderlying(snapshot->ExpressionDisplay->Commands));
1830+
SetHistoryExpressionDisplay(tokens, commands);
1831+
SetExpressionDisplay(tokens, commands);
1832+
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, false);
1833+
}
1834+
else
1835+
{
1836+
// use case: the current expression was not evaluated before, or it was an error.
1837+
auto displayCommands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->DisplayCommands));
1838+
for (auto cmd : displayCommands)
1839+
{
1840+
m_standardCalculatorManager.SendCommand(static_cast<Command>(cmd));
1841+
}
1842+
if (snapshot->PrimaryDisplay->IsError)
1843+
{
1844+
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, true);
1845+
}
1846+
}
18281847
}
1829-
if (snapshot->ExpressionDisplay != nullptr)
1848+
else
18301849
{
1831-
using RawTokenCollection = std::vector<std::pair<std::wstring, int>>;
1832-
RawTokenCollection rawTokens;
1833-
for (CalculatorApp::ViewModel::Snapshot::CalcManagerToken ^ token : snapshot->ExpressionDisplay->Tokens)
1850+
if (snapshot->PrimaryDisplay->IsError)
18341851
{
1835-
rawTokens.push_back(std::pair{ token->OpCodeName->Data(), token->CommandIndex });
1852+
// use case: user copy-pasted an invalid expression to Calculator and caused an error.
1853+
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, true);
1854+
}
1855+
else
1856+
{
1857+
// use case: there was no expression but user was inputing some numbers (including negative numbers).
1858+
auto commands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->DisplayCommands));
1859+
for (auto cmd : commands)
1860+
{
1861+
m_standardCalculatorManager.SendCommand(static_cast<Command>(cmd));
1862+
}
18361863
}
1837-
SetExpressionDisplay(
1838-
std::make_shared<RawTokenCollection>(rawTokens),
1839-
std::make_shared<std::vector<std::shared_ptr<IExpressionCommand>>>(ToUnderlying(snapshot->ExpressionDisplay->Commands)));
18401864
}
1841-
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, snapshot->PrimaryDisplay->IsError);
18421865
}

0 commit comments

Comments
 (0)