From 2535c75501bc39f8bbcb5e376a5f7c864bf42f73 Mon Sep 17 00:00:00 2001 From: Tian Liao Date: Wed, 16 Oct 2024 18:18:44 +0800 Subject: [PATCH] set snapshot --- src/CalcViewModel/Snapshots.cpp | 43 +++++++++++++------ src/CalcViewModel/Snapshots.h | 19 +++++--- .../StandardCalculatorViewModel.cpp | 37 +++++++++++++++- .../StandardCalculatorViewModel.h | 12 +++--- 4 files changed, 85 insertions(+), 26 deletions(-) diff --git a/src/CalcViewModel/Snapshots.cpp b/src/CalcViewModel/Snapshots.cpp index 87fc31db..ca6dc339 100644 --- a/src/CalcViewModel/Snapshots.cpp +++ b/src/CalcViewModel/Snapshots.cpp @@ -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(); - Commands = ref new Platform::Collections::Vector(); - 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(); + 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(); for (auto& item : items) { HistoryItems->Append(ref new CalcManagerHistoryItem(*item)); @@ -127,12 +131,25 @@ namespace CalculatorApp::ViewModel IsError = isError; } - ExpressionDisplaySnapshot::ExpressionDisplaySnapshot(const std::vector& tokens) + ExpressionDisplaySnapshot::ExpressionDisplaySnapshot( + const std::vector& tokens, + const std::vector>& commands) { Tokens = ref new Platform::Collections::Vector(); for (auto& [opCode, cmdIdx] : tokens) { Tokens->Append(ref new CalcManagerHistoryToken(ref new Platform::String(opCode.c_str()), cmdIdx)); } + + Commands = ref new Platform::Collections::Vector(); + for (auto& cmd : commands) + { + Commands->Append(CreateExprCommand(cmd.get())); + } } -} + + std::vector> ToUnderlying(Windows::Foundation::Collections::IVector ^ items) + { + return {}; // TODO + } +} // namespace CalculatorApp::ViewModel diff --git a/src/CalcViewModel/Snapshots.h b/src/CalcViewModel/Snapshots.h index ab62dda0..507fc032 100644 --- a/src/CalcViewModel/Snapshots.h +++ b/src/CalcViewModel/Snapshots.h @@ -2,6 +2,9 @@ // Licensed under the MIT License. #pragma once +#include +#include + #include "CalcManager/CalculatorManager.h" namespace CalculatorApp::ViewModel @@ -56,19 +59,23 @@ public ref struct ExpressionDisplaySnapshot sealed { property Windows::Foundation::Collections::IVector ^ Tokens; - // TODO: commands + property Windows::Foundation::Collections::IVector ^ Commands; internal :; using CalcHistoryToken = std::pair; - explicit ExpressionDisplaySnapshot(const std::vector& tokens); + explicit ExpressionDisplaySnapshot(const std::vector& tokens, const std::vector>& commands); }; public ref struct StandardCalculatorSnapshot sealed { - property CalcManagerSnapshot ^ CalcManager; // mandatory - property PrimaryDisplaySnapshot ^ PrimaryDisplay; // mandatory - property ExpressionDisplaySnapshot ^ ExpressionDisplay; // optional - property Windows::Foundation::Collections::IVector ^ Commands; // mandatory + property CalcManagerSnapshot ^ CalcManager; // mandatory + property PrimaryDisplaySnapshot ^ PrimaryDisplay; // mandatory + property ExpressionDisplaySnapshot ^ ExpressionDisplay; // optional + property Windows::Foundation::Collections::IVector ^ DisplayCommands; // mandatory }; + + ICalcManagerIExprCommand ^ CreateExprCommand(const IExpressionCommand* exprCmd); + std::vector> ToUnderlying(Windows::Foundation::Collections::IVector ^ items); + } // namespace CalculatorApp::ViewModel diff --git a/src/CalcViewModel/StandardCalculatorViewModel.cpp b/src/CalcViewModel/StandardCalculatorViewModel.cpp index d39e5848..38889fe1 100644 --- a/src/CalcViewModel/StandardCalculatorViewModel.cpp +++ b/src/CalcViewModel/StandardCalculatorViewModel.cpp @@ -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 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(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(); + for (auto cmd : m_standardCalculatorManager.GetDisplayCommandsSnapshot()) + { + result->DisplayCommands->Append(CreateExprCommand(cmd.get())); + } return nullptr; } + // StandardCalculatorSnapshot StandardCalculatorViewModel::GetStandardCalculatorSnapshot() const //{ // StandardCalculatorSnapshot snapshot; diff --git a/src/CalcViewModel/StandardCalculatorViewModel.h b/src/CalcViewModel/StandardCalculatorViewModel.h index 93fe41f7..5e17eee4 100644 --- a/src/CalcViewModel/StandardCalculatorViewModel.h +++ b/src/CalcViewModel/StandardCalculatorViewModel.h @@ -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;