set snapshot

This commit is contained in:
Tian Liao 2024-10-16 18:18:44 +08:00
commit 2535c75501
4 changed files with 85 additions and 26 deletions

View file

@ -37,7 +37,11 @@ namespace
int Command;
};
CalculatorApp::ViewModel::ICalcManagerIExprCommand ^ CreateExprCommand(const IExpressionCommand* exprCmd) {
} // namespace
namespace CalculatorApp::ViewModel
{
ICalcManagerIExprCommand ^ CreateExprCommand(const IExpressionCommand* exprCmd) {
switch (exprCmd->GetCommandType())
{
case CalculationManager::CommandType::UnaryCommand:
@ -81,10 +85,7 @@ namespace
throw std::logic_error{ "unhandled command type." };
}
}
} // namespace
namespace CalculatorApp::ViewModel
{
CalcManagerHistoryToken::CalcManagerHistoryToken(Platform::String ^ opCodeName, int cmdIndex)
{
assert(opCodeName != nullptr && "opCodeName is mandatory.");
@ -95,14 +96,16 @@ namespace CalculatorApp::ViewModel
CalcManagerHistoryItem::CalcManagerHistoryItem(const CalculationManager::HISTORYITEM& item)
{
Tokens = ref new Platform::Collections::Vector<CalcManagerHistoryToken ^>();
Commands = ref new Platform::Collections::Vector<ICalcManagerIExprCommand ^>();
if (item.historyItemVector.spTokens != nullptr)
assert(item.historyItemVector.spTokens != nullptr && "spTokens shall not be null.");
for (auto& [opCode, cmdIdx] : *item.historyItemVector.spTokens)
{
for (auto& [opCode, cmdIdx] : *item.historyItemVector.spTokens)
{
Tokens->Append(ref new CalcManagerHistoryToken(ref new Platform::String(opCode.c_str()), cmdIdx));
// Commands->Append();
}
Tokens->Append(ref new CalcManagerHistoryToken(ref new Platform::String(opCode.c_str()), cmdIdx));
}
Commands = ref new Platform::Collections::Vector<ICalcManagerIExprCommand ^>();
assert(item.historyItemVector.spCommands != nullptr && "spCommands shall not be null.");
for (auto& cmd : *item.historyItemVector.spCommands)
{
Commands->Append(CreateExprCommand(cmd.get()));
}
Expression = ref new Platform::String(item.historyItemVector.expression.c_str());
Result = ref new Platform::String(item.historyItemVector.result.c_str());
@ -113,6 +116,7 @@ namespace CalculatorApp::ViewModel
auto& items = calcMgr.GetHistoryItems();
if (!items.empty())
{
HistoryItems = ref new Platform::Collections::Vector<CalcManagerHistoryItem ^>();
for (auto& item : items)
{
HistoryItems->Append(ref new CalcManagerHistoryItem(*item));
@ -127,12 +131,25 @@ namespace CalculatorApp::ViewModel
IsError = isError;
}
ExpressionDisplaySnapshot::ExpressionDisplaySnapshot(const std::vector<CalcHistoryToken>& tokens)
ExpressionDisplaySnapshot::ExpressionDisplaySnapshot(
const std::vector<CalcHistoryToken>& tokens,
const std::vector<std::shared_ptr<IExpressionCommand>>& commands)
{
Tokens = ref new Platform::Collections::Vector<CalcManagerHistoryToken ^>();
for (auto& [opCode, cmdIdx] : tokens)
{
Tokens->Append(ref new CalcManagerHistoryToken(ref new Platform::String(opCode.c_str()), cmdIdx));
}
Commands = ref new Platform::Collections::Vector<ICalcManagerIExprCommand ^>();
for (auto& cmd : commands)
{
Commands->Append(CreateExprCommand(cmd.get()));
}
}
}
std::vector<std::shared_ptr<CalculationManager::HISTORYITEM>> ToUnderlying(Windows::Foundation::Collections::IVector<CalcManagerHistoryItem ^> ^ items)
{
return {}; // TODO
}
} // namespace CalculatorApp::ViewModel

View file

@ -2,6 +2,9 @@
// Licensed under the MIT License.
#pragma once
#include <vector>
#include <memory>
#include "CalcManager/CalculatorManager.h"
namespace CalculatorApp::ViewModel
@ -56,19 +59,23 @@ public
ref struct ExpressionDisplaySnapshot sealed
{
property Windows::Foundation::Collections::IVector<CalcManagerHistoryToken ^> ^ Tokens;
// TODO: commands
property Windows::Foundation::Collections::IVector<ICalcManagerIExprCommand ^> ^ Commands;
internal :;
using CalcHistoryToken = std::pair<std::wstring, int>;
explicit ExpressionDisplaySnapshot(const std::vector<CalcHistoryToken>& tokens);
explicit ExpressionDisplaySnapshot(const std::vector<CalcHistoryToken>& tokens, const std::vector<std::shared_ptr<IExpressionCommand>>& commands);
};
public
ref struct StandardCalculatorSnapshot sealed
{
property CalcManagerSnapshot ^ CalcManager; // mandatory
property PrimaryDisplaySnapshot ^ PrimaryDisplay; // mandatory
property ExpressionDisplaySnapshot ^ ExpressionDisplay; // optional
property Windows::Foundation::Collections::IVector<ICalcManagerIExprCommand ^> ^ Commands; // mandatory
property CalcManagerSnapshot ^ CalcManager; // mandatory
property PrimaryDisplaySnapshot ^ PrimaryDisplay; // mandatory
property ExpressionDisplaySnapshot ^ ExpressionDisplay; // optional
property Windows::Foundation::Collections::IVector<ICalcManagerIExprCommand ^> ^ DisplayCommands; // mandatory
};
ICalcManagerIExprCommand ^ CreateExprCommand(const IExpressionCommand* exprCmd);
std::vector<std::shared_ptr<CalculationManager::HISTORYITEM>> ToUnderlying(Windows::Foundation::Collections::IVector<CalcManagerHistoryItem ^> ^ items);
} // namespace CalculatorApp::ViewModel

View file

@ -115,7 +115,7 @@ namespace CalculatorResourceKeys
StringReference DisplayCopied(L"Display_Copied");
}
StandardCalculatorViewModel::StandardCalculatorViewModel()
StandardCalculatorViewModel::StandardCalculatorViewModel(StandardCalculatorSnapshot ^ snapshot)
: m_DisplayValue(L"0")
, m_DecimalDisplayValue(L"0")
, m_HexDisplayValue(L"0")
@ -183,6 +183,33 @@ StandardCalculatorViewModel::StandardCalculatorViewModel()
IsNegateEnabled = true;
IsDecimalEnabled = true;
AreProgrammerRadixOperatorsVisible = false;
if (snapshot != nullptr)
{
if (snapshot->CalcManager->HistoryItems != nullptr)
{
m_standardCalculatorManager.SetHistoryItems(ToUnderlying(snapshot->CalcManager->HistoryItems));
}
std::vector<int> commands;
if (snapshot->ExpressionDisplay != nullptr && snapshot->ExpressionDisplay->Tokens->GetAt(snapshot->ExpressionDisplay->Tokens->Size))
{
// commands = GetCommandsFromExpressionCommands(Snapshot->ExpressionDisplay->Commands);
}
if (commands.empty() && snapshot->DisplayCommands->Size > 0)
{
// commands = GetCommandsFromExpressionCommands(snapshot->DisplayCommands);
}
for (auto cmd : commands)
{
m_standardCalculatorManager.SendCommand(static_cast<Command>(cmd));
}
if (snapshot->ExpressionDisplay != nullptr)
{
// SetExpressionDisplay();
}
// SetPrimaryDisplay();
}
}
String ^ StandardCalculatorViewModel::LocalizeDisplayValue(_In_ wstring const& displayValue)
@ -1792,9 +1819,15 @@ StandardCalculatorSnapshot ^ StandardCalculatorViewModel::GetSnapshot() const
auto result = ref new StandardCalculatorSnapshot();
result->CalcManager = ref new CalcManagerSnapshot(m_standardCalculatorManager);
result->PrimaryDisplay = ref new PrimaryDisplaySnapshot(m_DisplayValue, m_IsInError);
result->ExpressionDisplay = ref new ExpressionDisplaySnapshot(*m_tokens);
result->ExpressionDisplay = ref new ExpressionDisplaySnapshot(*m_tokens, *m_commands);
result->DisplayCommands = ref new Platform::Collections::Vector<ICalcManagerIExprCommand ^>();
for (auto cmd : m_standardCalculatorManager.GetDisplayCommandsSnapshot())
{
result->DisplayCommands->Append(CreateExprCommand(cmd.get()));
}
return nullptr;
}
// StandardCalculatorSnapshot StandardCalculatorViewModel::GetStandardCalculatorSnapshot() const
//{
// StandardCalculatorSnapshot snapshot;

View file

@ -65,7 +65,6 @@ namespace CalculatorApp
[Windows::UI::Xaml::Data::Bindable] public ref class StandardCalculatorViewModel sealed : public Windows::UI::Xaml::Data::INotifyPropertyChanged
{
public:
StandardCalculatorViewModel();
void UpdateOperand(int pos, Platform::String ^ text);
OBSERVABLE_OBJECT_CALLBACK(OnPropertyChanged);
@ -269,10 +268,12 @@ namespace CalculatorApp
}
}
property StandardCalculatorSnapshot ^ Snapshot { StandardCalculatorSnapshot ^ get() { return GetSnapshot(); } }
property StandardCalculatorSnapshot
^ Snapshot { StandardCalculatorSnapshot ^ get() { return GetSnapshot(); } }
// Used by unit tests
void ResetCalcManager(bool clearMemory);
// Used by unit tests
void
ResetCalcManager(bool clearMemory);
void SendCommandToCalcManager(int command);
public:
@ -325,7 +326,8 @@ namespace CalculatorApp
return m_CurrentAngleType;
}
// void SetStandardCalculatorSnapshot(const StandardCalculatorSnapshot& state);
internal :;
explicit StandardCalculatorViewModel(StandardCalculatorSnapshot ^ snapshot = nullptr);
private:
StandardCalculatorSnapshot ^ GetSnapshot() const;