Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve an existing defect when restoring from snapshots #2249

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}