Skip to content

Commit

Permalink
Resolve an existing defect when restoring from snapshots (#2249)
Browse files Browse the repository at this point in the history
* fix bug

* fix a comment
  • Loading branch information
tian-lt authored Nov 18, 2024
1 parent 80015d2 commit 2b0521e
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions src/CalcViewModel/StandardCalculatorViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1813,30 +1813,53 @@ void CalculatorApp::ViewModel::StandardCalculatorViewModel::Snapshot::set(Calcul
m_standardCalculatorManager.SetHistoryItems(ToUnderlying(snapshot->CalcManager->HistoryItems));
}

std::vector<int> commands;
if (snapshot->ExpressionDisplay != nullptr && snapshot->ExpressionDisplay->Tokens->GetAt(snapshot->ExpressionDisplay->Tokens->Size - 1)->OpCodeName == L"=")
{
commands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->ExpressionDisplay->Commands));
}
if (commands.empty() && snapshot->DisplayCommands->Size > 0)
{
commands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->DisplayCommands));
}
for (auto cmd : commands)
if (snapshot->ExpressionDisplay != nullptr)
{
m_standardCalculatorManager.SendCommand(static_cast<Command>(cmd));
if (snapshot->DisplayCommands->Size == 0)
{
// use case: the current expression was evaluated before. load from history.
assert(!snapshot->PrimaryDisplay->IsError);
using RawTokenCollection = std::vector<std::pair<std::wstring, int>>;
RawTokenCollection rawTokens;
for (CalculatorApp::ViewModel::Snapshot::CalcManagerToken ^ token : snapshot->ExpressionDisplay->Tokens)
{
rawTokens.push_back(std::pair{ token->OpCodeName->Data(), token->CommandIndex });
}
auto tokens = std::make_shared<RawTokenCollection>(rawTokens);
auto commands = std::make_shared<std::vector<std::shared_ptr<IExpressionCommand>>>(ToUnderlying(snapshot->ExpressionDisplay->Commands));
SetHistoryExpressionDisplay(tokens, commands);
SetExpressionDisplay(tokens, commands);
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, false);
}
else
{
// use case: the current expression was not evaluated before, or it was an error.
auto displayCommands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->DisplayCommands));
for (auto cmd : displayCommands)
{
m_standardCalculatorManager.SendCommand(static_cast<Command>(cmd));
}
if (snapshot->PrimaryDisplay->IsError)
{
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, true);
}
}
}
if (snapshot->ExpressionDisplay != nullptr)
else
{
using RawTokenCollection = std::vector<std::pair<std::wstring, int>>;
RawTokenCollection rawTokens;
for (CalculatorApp::ViewModel::Snapshot::CalcManagerToken ^ token : snapshot->ExpressionDisplay->Tokens)
if (snapshot->PrimaryDisplay->IsError)
{
rawTokens.push_back(std::pair{ token->OpCodeName->Data(), token->CommandIndex });
// use case: user copy-pasted an invalid expression to Calculator and caused an error.
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, true);
}
else
{
// use case: there was no expression but user was inputing some numbers (including negative numbers).
auto commands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->DisplayCommands));
for (auto cmd : commands)
{
m_standardCalculatorManager.SendCommand(static_cast<Command>(cmd));
}
}
SetExpressionDisplay(
std::make_shared<RawTokenCollection>(rawTokens),
std::make_shared<std::vector<std::shared_ptr<IExpressionCommand>>>(ToUnderlying(snapshot->ExpressionDisplay->Commands)));
}
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, snapshot->PrimaryDisplay->IsError);
}

0 comments on commit 2b0521e

Please sign in to comment.