From d30849705c16f7fd23963f606a9d47d56ab1d806 Mon Sep 17 00:00:00 2001 From: Hongxu Xu Date: Thu, 19 Nov 2020 12:12:32 +0800 Subject: [PATCH] Remove trailing decimal Disable decimal input if maxFractionDigits is 0 Fix input may be blocked after switched active --- src/CalcManager/UnitConverter.cpp | 12 ++++++++++ src/CalcManager/UnitConverter.h | 4 ++++ src/CalcViewModel/UnitConverterViewModel.cpp | 22 ++++++++++++------- src/CalcViewModel/UnitConverterViewModel.h | 1 + .../UnitConverterViewModelUnitTests.cpp | 14 ++++++++++-- .../UnitConverterViewModelUnitTests.h | 4 +++- 6 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/CalcManager/UnitConverter.cpp b/src/CalcManager/UnitConverter.cpp index d760b611..d42ce033 100644 --- a/src/CalcManager/UnitConverter.cpp +++ b/src/CalcManager/UnitConverter.cpp @@ -191,6 +191,18 @@ void UnitConverter::SwitchActive(const wstring& newValue) } } +bool UnitConversionManager::UnitConverter::IsSwitchedActive() const +{ + return m_switchedActive; +} + +void UnitConversionManager::UnitConverter::SetValue(const std::wstring& newValue) +{ + m_returnDisplay = m_currentDisplay; + m_currentDisplay = newValue; + m_currentHasDecimal = (m_currentDisplay.find(L'.') != wstring::npos); +} + wstring UnitConverter::CategoryToString(const Category& c, wstring_view delimiter) { return Quote(std::to_wstring(c.id)) diff --git a/src/CalcManager/UnitConverter.h b/src/CalcManager/UnitConverter.h index 81a2629f..7f1ba3b1 100644 --- a/src/CalcManager/UnitConverter.h +++ b/src/CalcManager/UnitConverter.h @@ -223,6 +223,8 @@ namespace UnitConversionManager virtual Category GetCurrentCategory() = 0; virtual void SetCurrentUnitTypes(const Unit& fromType, const Unit& toType) = 0; virtual void SwitchActive(const std::wstring& newValue) = 0; + virtual bool IsSwitchedActive() const = 0; + virtual void SetValue(const std::wstring& newValue) = 0; virtual std::wstring SaveUserPreferences() = 0; virtual void RestoreUserPreferences(_In_ std::wstring_view userPreferences) = 0; virtual void SendCommand(Command command) = 0; @@ -246,6 +248,8 @@ namespace UnitConversionManager Category GetCurrentCategory() override; void SetCurrentUnitTypes(const Unit& fromType, const Unit& toType) override; void SwitchActive(const std::wstring& newValue) override; + bool IsSwitchedActive() const override; + void SetValue(const std::wstring& newValue) override; std::wstring SaveUserPreferences() override; void RestoreUserPreferences(std::wstring_view userPreference) override; void SendCommand(Command command) override; diff --git a/src/CalcViewModel/UnitConverterViewModel.cpp b/src/CalcViewModel/UnitConverterViewModel.cpp index c828b90c..dbc9a8f7 100644 --- a/src/CalcViewModel/UnitConverterViewModel.cpp +++ b/src/CalcViewModel/UnitConverterViewModel.cpp @@ -227,9 +227,8 @@ void UnitConverterViewModel::OnUnitChanged(Object ^ parameter) return; } - m_model->SetCurrentUnitTypes(UnitFrom->GetModelUnit(), UnitTo->GetModelUnit()); - UpdateCurrencyFormatter(); + m_model->SetCurrentUnitTypes(UnitFrom->GetModelUnit(), UnitTo->GetModelUnit()); if (m_supplementaryResultsTimer != nullptr) { @@ -271,6 +270,8 @@ void UnitConverterViewModel::OnSwitchActive(Platform::Object ^ unused) m_isInputBlocked = false; m_model->SwitchActive(m_valueFromUnlocalized); + + UpdateIsDecimalEnabled(); } String ^ UnitConverterViewModel::ConvertToLocalizedString(const std::wstring& stringToLocalize, bool allowPartialStrings, CurrencyFormatterParameter cfp) @@ -330,7 +331,7 @@ String ^ UnitConverterViewModel::ConvertToLocalizedString(const std::wstring& st if (hasDecimal) { - if (allowPartialStrings) + if (allowPartialStrings && lastCurrencyFractionDigits > 0) { // allow "in progress" strings, like "3." that occur during the composition of // a final number. Without this, when typing the three characters in "3.2" @@ -407,10 +408,10 @@ String ^ UnitConverterViewModel::ConvertToLocalizedString(const std::wstring& st } result = L"-" + result; } - + // restore the original fraction digits currencyFormatter->FractionDigits = lastCurrencyFractionDigits; - + return result; } @@ -504,9 +505,7 @@ void UnitConverterViewModel::OnButtonPressed(Platform::Object ^ parameter) static constexpr UCM::Command OPERANDS[] = { UCM::Command::Zero, UCM::Command::One, UCM::Command::Two, UCM::Command::Three, UCM::Command::Four, UCM::Command::Five, UCM::Command::Six, UCM::Command::Seven, UCM::Command::Eight, UCM::Command::Nine }; - if (m_isInputBlocked && - command != UCM::Command::Clear && - command != UCM::Command::Backspace) + if (m_isInputBlocked && !m_model->IsSwitchedActive() && command != UCM::Command::Clear && command != UCM::Command::Backspace) { return; } @@ -851,6 +850,13 @@ void UnitConverterViewModel::UpdateCurrencyFormatter() m_currencyFormatter2->IsGrouped = true; m_currencyFormatter2->Mode = CurrencyFormatterMode::UseCurrencyCode; m_currencyFormatter2->ApplyRoundingForCurrency(RoundingAlgorithm::RoundHalfDown); + + UpdateIsDecimalEnabled(); +} + +void UnitConverterViewModel::UpdateIsDecimalEnabled() +{ + IsDecimalEnabled = CurrencyFormatterFrom->FractionDigits > 0; } NumbersAndOperatorsEnum UnitConverterViewModel::MapCharacterToButtonId(const wchar_t ch, bool& canSendNegate) diff --git a/src/CalcViewModel/UnitConverterViewModel.h b/src/CalcViewModel/UnitConverterViewModel.h index e85b56f5..b48c1c3b 100644 --- a/src/CalcViewModel/UnitConverterViewModel.h +++ b/src/CalcViewModel/UnitConverterViewModel.h @@ -288,6 +288,7 @@ namespace CalculatorApp void RefreshSupplementaryResults(); void UpdateInputBlocked(_In_ const std::wstring& currencyInput); void UpdateCurrencyFormatter(); + void UpdateIsDecimalEnabled(); bool UnitsAreValid(); void ResetCategory(); diff --git a/src/CalculatorUnitTests/UnitConverterViewModelUnitTests.cpp b/src/CalculatorUnitTests/UnitConverterViewModelUnitTests.cpp index 83c1e9f2..40c6afe3 100644 --- a/src/CalculatorUnitTests/UnitConverterViewModelUnitTests.cpp +++ b/src/CalculatorUnitTests/UnitConverterViewModelUnitTests.cpp @@ -220,6 +220,16 @@ void UnitConverterMock::SwitchActive(const std::wstring& newValue) m_curValue = newValue; } +bool UnitConverterMock::IsSwitchedActive() const +{ + return false; +} + +void UnitConverterMock::SetValue(const std::wstring& newValue) +{ + m_curValue = newValue; +} + std::wstring UnitConverterMock::SaveUserPreferences() { return L"TEST"; @@ -950,8 +960,8 @@ TEST_METHOD(TestCurrencyFormattingLogic) // Establish base condition vm.CurrentCategory = vm.Categories->GetAt(3); // Currency - vm.Unit1 = vm.Units->GetAt(0); // JPY - vm.Unit2 = vm.Units->GetAt(1); // JOD + vm.Unit1 = vm.Units->GetAt(0); // JPY + vm.Unit2 = vm.Units->GetAt(1); // JOD vm.UnitChanged->Execute(nullptr); const WCHAR *vFrom = L"1.2340", *vTo = L"0.0070"; diff --git a/src/CalculatorUnitTests/UnitConverterViewModelUnitTests.h b/src/CalculatorUnitTests/UnitConverterViewModelUnitTests.h index b054f06c..cc94eade 100644 --- a/src/CalculatorUnitTests/UnitConverterViewModelUnitTests.h +++ b/src/CalculatorUnitTests/UnitConverterViewModelUnitTests.h @@ -38,7 +38,9 @@ namespace CalculatorUnitTests UCM::CategorySelectionInitializer SetCurrentCategory(const UCM::Category& input) override; UCM::Category GetCurrentCategory(); void SetCurrentUnitTypes(const UCM::Unit& fromType, const UCM::Unit& toType) override; - void SwitchActive(const std::wstring& newValue); + void SwitchActive(const std::wstring& newValue) override; + bool IsSwitchedActive() const override; + void SetValue(const std::wstring& newValue) override; std::wstring SaveUserPreferences() override; void RestoreUserPreferences(_In_ std::wstring_view userPreferences) override; void SendCommand(UCM::Command command) override;