Remove trailing decimal

Disable decimal input if maxFractionDigits is 0
Fix input may be blocked after switched active
This commit is contained in:
Hongxu Xu 2020-11-19 12:12:32 +08:00
commit d30849705c
6 changed files with 46 additions and 11 deletions

View file

@ -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))

View file

@ -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;

View file

@ -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"
@ -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)

View file

@ -288,6 +288,7 @@ namespace CalculatorApp
void RefreshSupplementaryResults();
void UpdateInputBlocked(_In_ const std::wstring& currencyInput);
void UpdateCurrencyFormatter();
void UpdateIsDecimalEnabled();
bool UnitsAreValid();
void ResetCategory();

View file

@ -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";

View file

@ -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;