diff --git a/src/CalcManager/CalculatorManager.cpp b/src/CalcManager/CalculatorManager.cpp index 8026366fb..e26ad4f83 100644 --- a/src/CalcManager/CalculatorManager.cpp +++ b/src/CalcManager/CalculatorManager.cpp @@ -554,30 +554,6 @@ namespace CalculationManager return m_currentDegreeMode; } - void CalculatorManager::SetHistory(_In_ CalculatorMode eMode, _In_ vector> const& history) - { - CalculatorHistory* pHistory = nullptr; - - switch (eMode) - { - case CalculatorMode::Standard: - pHistory = m_pStdHistory.get(); - break; - case CalculatorMode::Scientific: - pHistory = m_pSciHistory.get(); - break; - } - - if (pHistory) - { - pHistory->ClearHistory(); - for (auto const& historyItem : history) - { - pHistory->AddItem(historyItem); - } - } - } - wstring CalculatorManager::GetResultForRadix(uint32_t radix, int32_t precision, bool groupDigitsPerRadix) { return m_currentCalculatorEngine ? m_currentCalculatorEngine->GetCurrentResultForRadix(radix, precision, groupDigitsPerRadix) : L""; diff --git a/src/CalcManager/CalculatorManager.h b/src/CalcManager/CalculatorManager.h index be537dd56..88a8457b1 100644 --- a/src/CalcManager/CalculatorManager.h +++ b/src/CalcManager/CalculatorManager.h @@ -117,7 +117,6 @@ namespace CalculationManager return m_pHistory->MaxHistorySize(); } CalculationManager::Command GetCurrentDegreeMode(); - void SetHistory(_In_ CalculatorMode eMode, _In_ std::vector> const& history); void SetInHistoryItemLoadMode(_In_ bool isHistoryItemLoadMode); }; } diff --git a/src/CalcViewModel/ApplicationViewModel.cpp b/src/CalcViewModel/ApplicationViewModel.cpp index c3b4a4c2d..817147ea6 100644 --- a/src/CalcViewModel/ApplicationViewModel.cpp +++ b/src/CalcViewModel/ApplicationViewModel.cpp @@ -232,7 +232,6 @@ task ApplicationViewModel::HandleToggleAlwaysOnTop(float width, float heig localSettings->Values->Insert(HeightLocalSettings, height); bool success = co_await ApplicationView::GetForCurrentView()->TryEnterViewModeAsync(ApplicationViewMode::Default); - CalculatorViewModel->AreHistoryShortcutsEnabled = success; CalculatorViewModel->HistoryVM->AreHistoryShortcutsEnabled = success; CalculatorViewModel->IsAlwaysOnTop = !success; IsAlwaysOnTop = !success; @@ -261,7 +260,6 @@ task ApplicationViewModel::HandleToggleAlwaysOnTop(float width, float heig } bool success = co_await ApplicationView::GetForCurrentView()->TryEnterViewModeAsync(ApplicationViewMode::CompactOverlay, compactOptions); - CalculatorViewModel->AreHistoryShortcutsEnabled = !success; CalculatorViewModel->HistoryVM->AreHistoryShortcutsEnabled = !success; CalculatorViewModel->IsAlwaysOnTop = success; IsAlwaysOnTop = success; diff --git a/src/CalcViewModel/Common/Utils.cpp b/src/CalcViewModel/Common/Utils.cpp index 220c9bf72..6bcc527dd 100644 --- a/src/CalcViewModel/Common/Utils.cpp +++ b/src/CalcViewModel/Common/Utils.cpp @@ -72,67 +72,6 @@ bool Utils::IsLastCharacterTarget(_In_ wstring const& input, _In_ wchar_t target return !input.empty() && input.back() == target; } -void Utils::SerializeCommandsAndTokens( - _In_ shared_ptr>> const& tokens, - _In_ shared_ptr>> const& commands, - DataWriter ^ writer) -{ - // Save the size of the commands vector - writer->WriteUInt32(static_cast(commands->size())); - - SerializeCommandVisitor cmdVisitor(writer); - for (const auto& exprCmd : *commands) - { - CalculationManager::CommandType commandType = exprCmd->GetCommandType(); - writer->WriteInt32(static_cast(commandType)); - exprCmd->Accept(cmdVisitor); - } - - writer->WriteUInt32(static_cast(tokens->size())); - - for (const auto& eachToken : *tokens) - { - auto stringData = ref new Platform::String(eachToken.first.c_str()); - auto intData = eachToken.second; - writer->WriteUInt32(writer->MeasureString(stringData)); - writer->WriteString(stringData); - writer->WriteInt32(intData); - } -} - -const shared_ptr>> Utils::DeserializeCommands(DataReader ^ reader) -{ - auto commandVector = make_shared>>(); - auto commandVectorSize = reader->ReadUInt32(); - - CommandDeserializer cmdDeserializer(reader); - for (unsigned int i = 0; i < commandVectorSize; ++i) - { - auto commandTypeInt = reader->ReadInt32(); - CalculationManager::CommandType commandType = static_cast(commandTypeInt); - shared_ptr exprCmd = cmdDeserializer.Deserialize(commandType); - commandVector->push_back(exprCmd); - } - - return commandVector; -} - -const shared_ptr>> Utils::DeserializeTokens(DataReader ^ reader) -{ - auto tokenVector = make_shared>>(); - auto tokensSize = reader->ReadUInt32(); - - for (unsigned int i = 0; i < tokensSize; ++i) - { - auto stringDataLen = reader->ReadUInt32(); - auto stringData = reader->ReadString(stringDataLen); - auto intData = reader->ReadInt32(); - tokenVector->emplace_back(stringData->Data(), intData); - } - - return tokenVector; -} - DateTime Utils::GetUniversalSystemTime() { SYSTEMTIME sysTime = {}; diff --git a/src/CalcViewModel/Common/Utils.h b/src/CalcViewModel/Common/Utils.h index ed798ad9b..553e43f39 100644 --- a/src/CalcViewModel/Common/Utils.h +++ b/src/CalcViewModel/Common/Utils.h @@ -386,13 +386,6 @@ namespace Utils double GetDoubleFromWstring(std::wstring input); int GetWindowId(); void RunOnUIThreadNonblocking(std::function&& function, _In_ Windows::UI::Core::CoreDispatcher ^ currentDispatcher); - void SerializeCommandsAndTokens( - _In_ std::shared_ptr>> const& tokens, - _In_ std::shared_ptr>> const& commands, - Windows::Storage::Streams::DataWriter ^ writer); - - const std::shared_ptr>> DeserializeCommands(Windows::Storage::Streams::DataReader ^ reader); - const std::shared_ptr>> DeserializeTokens(Windows::Storage::Streams::DataReader ^ reader); Windows::Foundation::DateTime GetUniversalSystemTime(); bool IsDateTimeOlderThan(Windows::Foundation::DateTime dateTime, const long long duration); diff --git a/src/CalcViewModel/HistoryViewModel.cpp b/src/CalcViewModel/HistoryViewModel.cpp index 37f721322..b6ca448c6 100644 --- a/src/CalcViewModel/HistoryViewModel.cpp +++ b/src/CalcViewModel/HistoryViewModel.cpp @@ -21,6 +21,7 @@ using namespace Windows::Security::Cryptography; using namespace Windows::Foundation::Collections; static StringReference HistoryVectorLengthKey{ L"HistoryVectorLength" }; +static StringReference ItemsSizeKey{ L"ItemsCount" }; namespace CalculatorApp::ViewModel::HistoryResourceKeys { @@ -34,7 +35,6 @@ HistoryViewModel::HistoryViewModel(_In_ CalculationManager::CalculatorManager* c AreHistoryShortcutsEnabled = true; Items = ref new Platform::Collections::Vector(); - ItemSize = 0; } // this will reload Items with the history list based on current mode @@ -75,7 +75,7 @@ void HistoryViewModel::ReloadHistory(_In_ ViewMode currentMode) } Items = historyListVM; - UpdateItemSize(); + RaisePropertyChanged(ItemsSizeKey); } void HistoryViewModel::OnHistoryItemAdded(_In_ unsigned int addedItemIndex) @@ -101,8 +101,7 @@ void HistoryViewModel::OnHistoryItemAdded(_In_ unsigned int addedItemIndex) assert(addedItemIndex <= m_calculatorManager->MaxHistorySize() && addedItemIndex >= 0); Items->InsertAt(0, item); - UpdateItemSize(); - SaveHistory(); + RaisePropertyChanged(ItemsSizeKey); } void HistoryViewModel::SetCalculatorDisplay(CalculatorDisplay& calculatorDisplay) @@ -115,7 +114,7 @@ void HistoryViewModel::ShowItem(_In_ HistoryItemViewModel ^ e) { unsigned int index; Items->IndexOf(e, &index); - TraceLogger::GetInstance()->LogHistoryItemLoad((ViewMode)m_currentMode, ItemSize, (int)(index)); + TraceLogger::GetInstance()->LogHistoryItemLoad((ViewMode)m_currentMode, Items->Size, (int)(index)); HistoryItemClicked(e); } @@ -126,15 +125,8 @@ void HistoryViewModel::DeleteItem(_In_ HistoryItemViewModel ^ e) { if (m_calculatorManager->RemoveHistoryItem(itemIndex)) { - // Keys for the history container are index based. - // SaveHistory() re-inserts the items anyway, so it's faster to just clear out the container. - CalculationManager::CalculatorMode currentMode = m_currentMode; - ApplicationDataContainer ^ historyContainer = GetHistoryContainer(currentMode); - historyContainer->Values->Clear(); - Items->RemoveAt(itemIndex); - UpdateItemSize(); - SaveHistory(); + RaisePropertyChanged(ItemsSizeKey); } } } @@ -147,16 +139,14 @@ void HistoryViewModel::OnHideCommand(_In_ Platform::Object ^ e) void HistoryViewModel::OnClearCommand(_In_ Platform::Object ^ e) { - if (AreHistoryShortcutsEnabled == true) + if (AreHistoryShortcutsEnabled) { m_calculatorManager->ClearHistory(); if (Items->Size > 0) { - CalculationManager::CalculatorMode currentMode = m_currentMode; - ClearHistoryContainer(currentMode); Items->Clear(); - UpdateItemSize(); + RaisePropertyChanged(ItemsSizeKey); } if (m_localizedHistoryCleared == nullptr) @@ -167,210 +157,7 @@ void HistoryViewModel::OnClearCommand(_In_ Platform::Object ^ e) } } -// this method restores history vector per mode -void HistoryViewModel::RestoreHistory(_In_ CalculationManager::CalculatorMode cMode) -{ - ApplicationDataContainer ^ historyContainer = GetHistoryContainer(cMode); - std::shared_ptr>> historyVector = - std::make_shared>>(); - auto historyVectorLength = static_cast(historyContainer->Values->Lookup(HistoryVectorLengthKey)); - bool failure = false; - - if (historyVectorLength > 0) - { - for (int i = 0; i < historyVectorLength; ++i) - { - try - { - // deserialize each item - auto item = DeserializeHistoryItem(i.ToString(), historyContainer); - std::shared_ptr Item = std::make_shared(item); - historyVector->push_back(Item); - } - catch (Platform::Exception ^ e) - { - failure = true; - break; - } - } - - if (!failure) - { - // if task has been cancelled set history to 0 - m_calculatorManager->SetHistory(cMode, *historyVector); - - // update length once again for consistency between stored number of items and length - UpdateHistoryVectorLength(static_cast(historyVector->size()), cMode); - } - else - { - // in case of failure do not show any item - UpdateHistoryVectorLength(0, cMode); - } - } -} - -Platform::String ^ HistoryViewModel::GetHistoryContainerKey(_In_ CalculationManager::CalculatorMode cMode) -{ - Platform::ValueType ^ modeValue = static_cast(cMode); - return Platform::String::Concat(modeValue->ToString(), L"_History"); -} - -ApplicationDataContainer ^ HistoryViewModel::GetHistoryContainer(_In_ CalculationManager::CalculatorMode cMode) -{ - ApplicationDataContainer ^ localSettings = ApplicationData::Current->LocalSettings; - ApplicationDataContainer ^ historyContainer; - - // naming container based on mode - Platform::String ^ historyContainerKey = GetHistoryContainerKey(cMode); - - if (localSettings->Containers->HasKey(historyContainerKey)) - { - historyContainer = localSettings->Containers->Lookup(historyContainerKey); - } - else - { - // create container for adding data - historyContainer = localSettings->CreateContainer(historyContainerKey, ApplicationDataCreateDisposition::Always); - int initialHistoryVectorLength = 0; - historyContainer->Values->Insert(HistoryVectorLengthKey, initialHistoryVectorLength); - } - - return historyContainer; -} - -void HistoryViewModel::ClearHistoryContainer(_In_ CalculationManager::CalculatorMode cMode) -{ - ApplicationDataContainer ^ localSettings = ApplicationData::Current->LocalSettings; - localSettings->DeleteContainer(GetHistoryContainerKey(cMode)); -} - -// this method will be used to update the history item length -void HistoryViewModel::UpdateHistoryVectorLength(_In_ int newValue, _In_ CalculationManager::CalculatorMode cMode) -{ - ApplicationDataContainer ^ historyContainer = GetHistoryContainer(cMode); - historyContainer->Values->Remove(HistoryVectorLengthKey); - historyContainer->Values->Insert(HistoryVectorLengthKey, newValue); -} - -void HistoryViewModel::ClearHistory() -{ - ClearHistoryContainer(CalculationManager::CalculatorMode::Standard); - ClearHistoryContainer(CalculationManager::CalculatorMode::Scientific); -} - unsigned long long HistoryViewModel::GetMaxItemSize() { return static_cast(m_calculatorManager->MaxHistorySize()); } - -void HistoryViewModel::SaveHistory() -{ - ApplicationDataContainer ^ historyContainer = GetHistoryContainer(m_currentMode); - auto const& currentHistoryVector = m_calculatorManager->GetHistoryItems(m_currentMode); - bool failure = false; - int index = 0; - Platform::String ^ serializedHistoryItem; - - for (auto const& item : currentHistoryVector) - { - try - { - serializedHistoryItem = SerializeHistoryItem(item); - historyContainer->Values->Insert(index.ToString(), serializedHistoryItem); - } - catch (Platform::Exception ^) - { - failure = true; - break; - } - - ++index; - } - - if (!failure) - { - // insertion is successful - UpdateHistoryVectorLength(static_cast(currentHistoryVector.size()), m_currentMode); - } - else - { - UpdateHistoryVectorLength(0, m_currentMode); - } -} - -// this serializes a history item into a base64 encoded string -Platform::String ^ HistoryViewModel::SerializeHistoryItem(_In_ std::shared_ptr const& item) -{ - DataWriter ^ writer = ref new DataWriter(); - auto expr = item->historyItemVector.expression; - auto result = item->historyItemVector.result; - auto platformExpr = ref new Platform::String(expr.c_str()); - writer->WriteUInt32(writer->MeasureString(platformExpr)); - writer->WriteString(platformExpr); - auto platformResult = ref new Platform::String(result.c_str()); - writer->WriteUInt32(writer->MeasureString(platformResult)); - writer->WriteString(platformResult); - - Utils::SerializeCommandsAndTokens(item->historyItemVector.spTokens, item->historyItemVector.spCommands, writer); - - IBuffer ^ buffer = writer->DetachBuffer(); - if (buffer == nullptr) - { - throw ref new Platform::Exception(E_POINTER, ref new Platform::String(L"History Item is NULL")); - } - - return CryptographicBuffer::EncodeToBase64String(buffer); -} - -CalculationManager::HISTORYITEM -HistoryViewModel::DeserializeHistoryItem(_In_ Platform::String ^ historyItemKey, _In_ ApplicationDataContainer ^ historyContainer) -{ - CalculationManager::HISTORYITEM historyItem; - if (historyContainer->Values->HasKey(historyItemKey)) - { - Object ^ historyItemValues = historyContainer->Values->Lookup(historyItemKey); - - if (historyItemValues == nullptr) - { - throw ref new Platform::Exception(E_POINTER, ref new Platform::String(L"History Item is NULL")); - } - - String ^ historyData = safe_cast(historyItemValues); - IBuffer ^ buffer = CryptographicBuffer::DecodeFromBase64String(historyData); - - if (buffer == nullptr) - { - throw ref new Platform::Exception(E_POINTER, ref new Platform::String(L"History Item is NULL")); - } - - DataReader ^ reader = DataReader::FromBuffer(buffer); - auto exprLen = reader->ReadUInt32(); - auto expression = reader->ReadString(exprLen); - historyItem.historyItemVector.expression = expression->Data(); - - auto resultLen = reader->ReadUInt32(); - auto result = reader->ReadString(resultLen); - historyItem.historyItemVector.result = result->Data(); - - historyItem.historyItemVector.spCommands = Utils::DeserializeCommands(reader); - historyItem.historyItemVector.spTokens = Utils::DeserializeTokens(reader); - } - else - { - throw ref new Platform::Exception(E_ACCESSDENIED, ref new Platform::String(L"History Item not found")); - } - return historyItem; -} - -bool HistoryViewModel::IsValid(_In_ CalculationManager::HISTORYITEM item) -{ - return ( - !item.historyItemVector.expression.empty() && !item.historyItemVector.result.empty() && (bool)item.historyItemVector.spCommands - && (bool)item.historyItemVector.spTokens); -} - -void HistoryViewModel::UpdateItemSize() -{ - ItemSize = Items->Size; -} diff --git a/src/CalcViewModel/HistoryViewModel.h b/src/CalcViewModel/HistoryViewModel.h index cc393219a..9b40efed1 100644 --- a/src/CalcViewModel/HistoryViewModel.h +++ b/src/CalcViewModel/HistoryViewModel.h @@ -26,10 +26,16 @@ namespace CalculatorApp { public: OBSERVABLE_OBJECT(); - OBSERVABLE_PROPERTY_RW(int, ItemSize); - OBSERVABLE_PROPERTY_RW(Windows::UI::Xaml::Interop::IBindableObservableVector ^, Items); + OBSERVABLE_PROPERTY_R(Windows::Foundation::Collections::IObservableVector ^, Items); OBSERVABLE_PROPERTY_RW(bool, AreHistoryShortcutsEnabled); - OBSERVABLE_PROPERTY_RW(CalculatorApp::Common::Automation::NarratorAnnouncement ^, HistoryAnnouncement); + OBSERVABLE_PROPERTY_R(CalculatorApp::Common::Automation::NarratorAnnouncement ^, HistoryAnnouncement); + property int ItemsCount + { + int get() + { + return Items->Size; + } + } void OnHistoryItemAdded(_In_ unsigned int addedItemIndex); @@ -42,7 +48,6 @@ namespace CalculatorApp event HideHistoryClickedHandler ^ HideHistoryClicked; event HistoryItemClickedHandler ^ HistoryItemClicked; void ShowItem(_In_ CalculatorApp::ViewModel::HistoryItemViewModel ^ e); - void ClearHistory(); internal : HistoryViewModel(_In_ CalculationManager::CalculatorManager* calculatorManager); void SetCalculatorDisplay(CalculatorDisplay& calculatorDisplay); @@ -51,27 +56,11 @@ namespace CalculatorApp void DeleteItem(_In_ CalculatorApp::ViewModel::HistoryItemViewModel ^ e); - // store history in app data functions - Platform::String ^ SerializeHistoryItem(_In_ std::shared_ptr const& item); - void SaveHistory(); - private: CalculationManager::CalculatorManager* const m_calculatorManager; CalculatorDisplay m_calculatorDisplay; CalculationManager::CalculatorMode m_currentMode; Platform::String ^ m_localizedHistoryCleared; - - void RestoreHistory(_In_ CalculationManager::CalculatorMode cMode); - CalculationManager::HISTORYITEM - DeserializeHistoryItem(_In_ Platform::String ^ historyItemKey, _In_ Windows::Storage::ApplicationDataContainer ^ historyContainer); - Windows::Storage::ApplicationDataContainer ^ GetHistoryContainer(_In_ CalculationManager::CalculatorMode cMode); - Platform::String ^ GetHistoryContainerKey(_In_ CalculationManager::CalculatorMode cMode); - void ClearHistoryContainer(_In_ CalculationManager::CalculatorMode cMode); - void UpdateHistoryVectorLength(_In_ int newValue, _In_ CalculationManager::CalculatorMode cMode); - bool IsValid(_In_ CalculationManager::HISTORYITEM item); - - friend class CalculatorDisplay; - void UpdateItemSize(); }; } } diff --git a/src/CalcViewModel/StandardCalculatorViewModel.cpp b/src/CalcViewModel/StandardCalculatorViewModel.cpp index 3ed4e6f03..38124e44b 100644 --- a/src/CalcViewModel/StandardCalculatorViewModel.cpp +++ b/src/CalcViewModel/StandardCalculatorViewModel.cpp @@ -130,7 +130,6 @@ StandardCalculatorViewModel::StandardCalculatorViewModel() IsOperandEnabled = true; IsNegateEnabled = true; IsDecimalEnabled = true; - AreHistoryShortcutsEnabled = true; AreProgrammerRadixOperatorsEnabled = false; } diff --git a/src/CalcViewModel/StandardCalculatorViewModel.h b/src/CalcViewModel/StandardCalculatorViewModel.h index d118c5aab..fd57c7d02 100644 --- a/src/CalcViewModel/StandardCalculatorViewModel.h +++ b/src/CalcViewModel/StandardCalculatorViewModel.h @@ -76,7 +76,6 @@ namespace CalculatorApp OBSERVABLE_PROPERTY_R(CalculatorApp::Common::NumberBase, CurrentRadixType); OBSERVABLE_PROPERTY_R(bool, AreTokensUpdated); OBSERVABLE_PROPERTY_R(bool, AreAlwaysOnTopResultsUpdated); - OBSERVABLE_PROPERTY_RW(bool, AreHistoryShortcutsEnabled); OBSERVABLE_PROPERTY_R(bool, AreProgrammerRadixOperatorsEnabled); OBSERVABLE_PROPERTY_R(bool, IsInputEmpty); OBSERVABLE_PROPERTY_R(CalculatorApp::Common::Automation::NarratorAnnouncement ^, Announcement); diff --git a/src/Calculator/Views/HistoryList.xaml b/src/Calculator/Views/HistoryList.xaml index 64f113a0e..6fc5281d1 100644 --- a/src/Calculator/Views/HistoryList.xaml +++ b/src/Calculator/Views/HistoryList.xaml @@ -137,7 +137,7 @@ Foreground="{ThemeResource SystemControlPageTextBaseHighBrush}" FontWeight="SemiLight" TextWrapping="Wrap" - Visibility="{Binding ItemSize, Converter={StaticResource ItemSizeToVisibilityConverter}}"/> + Visibility="{x:Bind Model.ItemsCount, Converter={StaticResource ItemSizeToVisibilityConverter}, Mode=OneWay}"/> + Visibility="{x:Bind Model.ItemsCount, Mode=OneWay, Converter={StaticResource ItemSizeToVisibilityNegationConverter}}"> @@ -166,7 +166,7 @@ AutomationProperties.AutomationId="ClearHistory" Command="{x:Bind Model.ClearCommand, Mode=OneWay}" Content="" - Visibility="{x:Bind Model.ItemSize, Mode=OneWay, Converter={StaticResource ItemSizeToVisibilityNegationConverter}}"/> + Visibility="{x:Bind Model.ItemsCount, Mode=OneWay, Converter={StaticResource ItemSizeToVisibilityNegationConverter}}"/> diff --git a/src/Calculator/Views/MainPage.xaml.cpp b/src/Calculator/Views/MainPage.xaml.cpp index 0454af690..df8356066 100644 --- a/src/Calculator/Views/MainPage.xaml.cpp +++ b/src/Calculator/Views/MainPage.xaml.cpp @@ -89,11 +89,6 @@ MainPage::MainPage() void MainPage::OnNavigatedTo(NavigationEventArgs ^ e) { - if (m_model->CalculatorViewModel) - { - m_model->CalculatorViewModel->HistoryVM->ClearHistory(); - } - ViewMode initialMode = ViewMode::Standard; if (e->Parameter != nullptr) { @@ -134,7 +129,6 @@ void MainPage::OnAppPropertyChanged(_In_ Platform::Object ^ sender, _In_ Windows if (newValue == ViewMode::Standard) { EnsureCalculator(); - m_model->CalculatorViewModel->AreHistoryShortcutsEnabled = true; m_model->CalculatorViewModel->HistoryVM->AreHistoryShortcutsEnabled = true; m_calculator->AnimateCalculator(NavCategory::IsConverterViewMode(previousMode)); m_model->CalculatorViewModel->HistoryVM->ReloadHistory(newValue); @@ -142,7 +136,6 @@ void MainPage::OnAppPropertyChanged(_In_ Platform::Object ^ sender, _In_ Windows else if (newValue == ViewMode::Scientific) { EnsureCalculator(); - m_model->CalculatorViewModel->AreHistoryShortcutsEnabled = true; m_model->CalculatorViewModel->HistoryVM->AreHistoryShortcutsEnabled = true; if (m_model->PreviousMode != ViewMode::Scientific) { @@ -153,7 +146,6 @@ void MainPage::OnAppPropertyChanged(_In_ Platform::Object ^ sender, _In_ Windows } else if (newValue == ViewMode::Programmer) { - m_model->CalculatorViewModel->AreHistoryShortcutsEnabled = false; m_model->CalculatorViewModel->HistoryVM->AreHistoryShortcutsEnabled = false; EnsureCalculator(); if (m_model->PreviousMode != ViewMode::Programmer) @@ -165,7 +157,6 @@ void MainPage::OnAppPropertyChanged(_In_ Platform::Object ^ sender, _In_ Windows { if (m_model->CalculatorViewModel) { - m_model->CalculatorViewModel->AreHistoryShortcutsEnabled = false; m_model->CalculatorViewModel->HistoryVM->AreHistoryShortcutsEnabled = false; } EnsureDateCalculator(); @@ -179,7 +170,6 @@ void MainPage::OnAppPropertyChanged(_In_ Platform::Object ^ sender, _In_ Windows { if (m_model->CalculatorViewModel) { - m_model->CalculatorViewModel->AreHistoryShortcutsEnabled = false; m_model->CalculatorViewModel->HistoryVM->AreHistoryShortcutsEnabled = false; } EnsureConverter(); diff --git a/src/CalculatorUnitTests/HistoryTests.cpp b/src/CalculatorUnitTests/HistoryTests.cpp index f2eb04b6a..805ba320e 100644 --- a/src/CalculatorUnitTests/HistoryTests.cpp +++ b/src/CalculatorUnitTests/HistoryTests.cpp @@ -29,8 +29,6 @@ namespace CalculatorFunctionalTests TEST_METHOD(TestHistoryClearCommand); TEST_METHOD(TestHistoryClearCommandWithEmptyHistory); TEST_METHOD(TestReLoadHistory); - TEST_METHOD(TestSaveAndReloadHistory); - TEST_METHOD(TestSerializeDeSerializeHistoryItem); TEST_METHOD(TestHistoryItemWithPrettyExpressions); TEST_METHOD(TestHistoryItemWithPrettyExpressionsMixedRadix); TEST_METHOD(TestHistoryItemLoadAndContinueCalculation); @@ -73,12 +71,12 @@ namespace CalculatorFunctionalTests void AddSingleHistoryItem() { Initialize(); - int initialSize = m_historyViewModel->ItemSize; + int initialSize = m_historyViewModel->ItemsCount; m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command1)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandADD)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command8)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); - int sizeAfterItemAdd = m_historyViewModel->ItemSize; + int sizeAfterItemAdd = m_historyViewModel->ItemsCount; auto historyItem = static_cast(m_historyViewModel->Items->GetAt(0)); String ^ expression = L"1 + 8 ="; VERIFY_ARE_EQUAL(initialSize + 1, sizeAfterItemAdd); @@ -101,11 +99,11 @@ namespace CalculatorFunctionalTests m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command2)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); } - VERIFY_ARE_EQUAL((size_t)m_historyViewModel->ItemSize, m_historyViewModel->GetMaxItemSize()); + VERIFY_ARE_EQUAL((size_t)m_historyViewModel->ItemsCount, m_historyViewModel->GetMaxItemSize()); String ^ expression = L"1 + 1 ="; int output = 2; String ^ result = output.ToString(); - auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1)); + auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemsCount - 1)); VERIFY_ARE_EQUAL(historyItem->Expression, expression); VERIFY_ARE_EQUAL(historyItem->Result, result); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command1)); @@ -115,7 +113,7 @@ namespace CalculatorFunctionalTests expression = L"1 + 2 ="; output = 3; result = output.ToString(); - historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1)); + historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemsCount - 1)); VERIFY_ARE_EQUAL(historyItem->Expression, expression); VERIFY_ARE_EQUAL(historyItem->Result, result); Cleanup(); @@ -150,26 +148,26 @@ namespace CalculatorFunctionalTests m_standardViewModel->SendCommandToCalcManager(static_cast(Command::ModeScientific)); m_historyViewModel->ReloadHistory(ViewMode::Scientific); - VERIFY_ARE_EQUAL(scientificItems, m_historyViewModel->ItemSize); + VERIFY_ARE_EQUAL(scientificItems, m_historyViewModel->ItemsCount); for (int i = 0; i < scientificItems; i++) { wstring expr = L"1 + " + wstring(i.ToString()->Data()) + L" ="; int output = 1 + i; String ^ result = output.ToString(); - auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1 - i)); + auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemsCount - 1 - i)); VERIFY_ARE_EQUAL(historyItem->Expression, StringReference(expr.c_str())); VERIFY_ARE_EQUAL(historyItem->Result, result); } m_historyViewModel->ReloadHistory(ViewMode::Standard); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::ModeBasic)); - VERIFY_ARE_EQUAL(standardItems, m_historyViewModel->ItemSize); + VERIFY_ARE_EQUAL(standardItems, m_historyViewModel->ItemsCount); for (int i = 0; i < standardItems; i++) { wstring expr = L"1 + " + wstring(i.ToString()->Data()) + L" ="; int output = 1 + i; String ^ result = output.ToString(); - auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1 - i)); + auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemsCount - 1 - i)); VERIFY_ARE_EQUAL(historyItem->Expression, StringReference(expr.c_str())); VERIFY_ARE_EQUAL(historyItem->Result, result); } @@ -190,63 +188,12 @@ namespace CalculatorFunctionalTests m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command2)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); m_historyViewModel->OnClearCommand(nullptr); - VERIFY_ARE_EQUAL(0, m_historyViewModel->ItemSize); + VERIFY_ARE_EQUAL(0, m_historyViewModel->ItemsCount); VERIFY_IS_TRUE(IsHistoryContainerEmpty(GetHistoryContainerKeyHelper(CalculatorMode::Standard))); VERIFY_IS_TRUE(IsHistoryContainerEmpty(GetHistoryContainerKeyHelper(CalculatorMode::Scientific))); Cleanup(); } - void SerializeDeSerializeHistoryItem() - { - Initialize(); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::ModeScientific)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command1)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandADD)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command2)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); - auto itemBeforeSerializeDeserialize = static_cast(m_historyViewModel->Items->GetAt(0)); - m_historyViewModel->SaveHistory(); - m_historyViewModel->ReloadHistory(ViewMode::Scientific); - auto itemAfterSerializeDeserialize = static_cast(m_historyViewModel->Items->GetAt(0)); - VERIFY_IS_TRUE( - itemBeforeSerializeDeserialize->Expression == itemAfterSerializeDeserialize->Expression - && itemBeforeSerializeDeserialize->Result == itemAfterSerializeDeserialize->Result); - Cleanup(); - } - - void SaveAndReloadHistory() - { - Initialize(); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::ModeScientific)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command1)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandADD)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command8)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command1)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandADD)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command2)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::ModeBasic)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command1)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandADD)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command6)); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); - int itemsBeforeSaveAndReload = m_historyViewModel->ItemSize; - m_historyViewModel->SaveHistory(); - m_historyViewModel->ReloadHistory(ViewMode::Scientific); - m_standardViewModel->SendCommandToCalcManager(static_cast(Command::ModeScientific)); - wstring expr = L"1 + 8 ="; - String ^ result = StringReference(L"9"); - int itemsAfterSaveAndReload = m_historyViewModel->ItemSize; - - auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1)); - VERIFY_ARE_EQUAL(historyItem->Expression, StringReference(expr.c_str())); - VERIFY_ARE_EQUAL(historyItem->Result, result); - VERIFY_ARE_NOT_EQUAL(itemsBeforeSaveAndReload, itemsAfterSaveAndReload); - VERIFY_ARE_EQUAL(itemsBeforeSaveAndReload, itemsAfterSaveAndReload + 1); - Cleanup(); - } - void HistoryItemWithPrettyExpressions() { Initialize(); @@ -267,7 +214,7 @@ namespace CalculatorFunctionalTests m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command1)); m_standardViewModel->SendCommandToCalcManager(static_cast(commands[command])); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); - auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1 - itemIndex)); + auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemsCount - 1 - itemIndex)); String ^ expression = m_uiResourceLoader->GetString(commandResource.ToString()); expression += L"( 1 ) ="; wstring expr = wstring(expression->Data()); @@ -296,7 +243,7 @@ namespace CalculatorFunctionalTests m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command1)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandSIN)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); - auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1)); + auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemsCount - 1)); String ^ expression = m_uiResourceLoader->GetString(L"67"); expression += L"( 1 ) + "; expression += m_uiResourceLoader->GetString(L"73"); @@ -319,7 +266,7 @@ namespace CalculatorFunctionalTests m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandADD)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command3)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); - auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1)); + auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemsCount - 1)); m_standardViewModel->SelectHistoryItem(historyItem); VERIFY_ARE_EQUAL(StringReference(L"9"), m_standardViewModel->DisplayValue); VERIFY_ARE_EQUAL(StringReference(L"1"), m_standardViewModel->ExpressionTokens->GetAt(0)->Token); @@ -344,18 +291,18 @@ namespace CalculatorFunctionalTests m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command3)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); - auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1)); + auto historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemsCount - 1)); m_standardViewModel->SelectHistoryItem(historyItem); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandADD)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::Command5)); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::CommandEQU)); VERIFY_ARE_EQUAL(StringReference(L"14"), m_standardViewModel->DisplayValue); - historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1)); + historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemsCount - 1)); m_standardViewModel->SelectHistoryItem(historyItem); VERIFY_ARE_EQUAL(StringReference(L"9"), m_standardViewModel->DisplayValue); - historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 2)); + historyItem = static_cast(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemsCount - 2)); m_standardViewModel->SelectHistoryItem(historyItem); VERIFY_ARE_EQUAL(StringReference(L"14"), m_standardViewModel->DisplayValue); Cleanup(); @@ -414,19 +361,19 @@ namespace CalculatorFunctionalTests void HistoryEmpty() { Initialize(); - VERIFY_ARE_EQUAL(0, m_historyViewModel->ItemSize); + VERIFY_ARE_EQUAL(0, m_historyViewModel->ItemsCount); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::ModeScientific)); - VERIFY_ARE_EQUAL(0, m_historyViewModel->ItemSize); + VERIFY_ARE_EQUAL(0, m_historyViewModel->ItemsCount); Cleanup(); } void HistoryClearCommandWithEmptyHistory() { Initialize(); - VERIFY_ARE_EQUAL(0, m_historyViewModel->ItemSize); + VERIFY_ARE_EQUAL(0, m_historyViewModel->ItemsCount); m_standardViewModel->SendCommandToCalcManager(static_cast(Command::ModeScientific)); m_historyViewModel->OnClearCommand(nullptr); - VERIFY_ARE_EQUAL(0, m_historyViewModel->ItemSize); + VERIFY_ARE_EQUAL(0, m_historyViewModel->ItemsCount); Cleanup(); } }; @@ -451,16 +398,6 @@ namespace CalculatorFunctionalTests ClearHistory(); } - void HistoryTests::TestSerializeDeSerializeHistoryItem() - { - SerializeDeSerializeHistoryItem(); - } - - void HistoryTests::TestSaveAndReloadHistory() - { - SaveAndReloadHistory(); - } - void HistoryTests::TestHistoryItemWithPrettyExpressions() { HistoryItemWithPrettyExpressions(); diff --git a/src/CalculatorUnitTests/MultiWindowUnitTests.cpp b/src/CalculatorUnitTests/MultiWindowUnitTests.cpp index a9810d9c7..38d29052a 100644 --- a/src/CalculatorUnitTests/MultiWindowUnitTests.cpp +++ b/src/CalculatorUnitTests/MultiWindowUnitTests.cpp @@ -369,7 +369,7 @@ TEST_METHOD(MultipleModesHistoryAddItemTest) ChangeMode(viewModels[i], i); // Validate that the history items list is initially empty - VERIFY_IS_TRUE(0 == viewModels[i]->HistoryVM->ItemSize); + VERIFY_IS_TRUE(0 == viewModels[i]->HistoryVM->ItemsCount); } // Perform Calculations on both the instances and check that the History items work independently @@ -390,7 +390,7 @@ TEST_METHOD(MultipleModesHistoryAddItemTest) ValidateViewModelByCommands(viewModels[1], scientificModeTestItems, true); // Assert for the history list items of 1st instance - VERIFY_IS_TRUE(1 == viewModels[0]->HistoryVM->ItemSize); + VERIFY_IS_TRUE(1 == viewModels[0]->HistoryVM->ItemsCount); auto item1 = static_cast(viewModels[0]->HistoryVM->Items->GetAt(0)); String ^ expression1 = L"1 + 2 ="; @@ -400,7 +400,7 @@ TEST_METHOD(MultipleModesHistoryAddItemTest) VERIFY_ARE_EQUAL(result1, item1->Result); // Assert for the history list items of 2nd instance - VERIFY_IS_TRUE(1 == viewModels[1]->HistoryVM->ItemSize); + VERIFY_IS_TRUE(1 == viewModels[1]->HistoryVM->ItemsCount); auto item2 = static_cast(viewModels[1]->HistoryVM->Items->GetAt(0)); String ^ expression2 = L"1 + 2 " + UtfUtils::MUL + L" 3 ="; @@ -424,7 +424,7 @@ TEST_METHOD(MultipleStandardModesHistoryAddItemTest) ChangeMode(viewModels[i], 0); // Validate that the history items list is initially empty - VERIFY_IS_TRUE(0 == viewModels[i]->HistoryVM->ItemSize); + VERIFY_IS_TRUE(0 == viewModels[i]->HistoryVM->ItemsCount); } // Perform Calculations on both the instances and check that the History items work independently @@ -457,7 +457,7 @@ TEST_METHOD(MultipleStandardModesHistoryAddItemTest) // Assert for the history list items of the instances for (int i = 0; i < 2; i++) { - VERIFY_IS_TRUE(1 == viewModels[i]->HistoryVM->ItemSize); + VERIFY_IS_TRUE(1 == viewModels[i]->HistoryVM->ItemsCount); auto item = static_cast(viewModels[i]->HistoryVM->Items->GetAt(0)); @@ -480,7 +480,7 @@ TEST_METHOD(MultipleScientificModesHistoryAddItemTest) ChangeMode(viewModels[i], 1); // Validate that the history items list is initially empty - VERIFY_IS_TRUE(0 == viewModels[i]->HistoryVM->ItemSize); + VERIFY_IS_TRUE(0 == viewModels[i]->HistoryVM->ItemsCount); } // Perform Calculations on both the instances and check that the History items work independently @@ -513,7 +513,7 @@ TEST_METHOD(MultipleScientificModesHistoryAddItemTest) // Assert for the history list items of the instances for (int i = 0; i < 2; i++) { - VERIFY_IS_TRUE(1 == viewModels[i]->HistoryVM->ItemSize); + VERIFY_IS_TRUE(1 == viewModels[i]->HistoryVM->ItemsCount); auto item = static_cast(viewModels[i]->HistoryVM->Items->GetAt(0));