From 80bbb1ad81df2b1048e2569e5266457f48e53c16 Mon Sep 17 00:00:00 2001 From: Tian Liao Date: Tue, 26 Nov 2024 15:02:29 +0800 Subject: [PATCH] refactor --- src/CalcViewModel/ApplicationViewModel.cpp | 4 +- src/CalcViewModel/CalcViewModel.vcxproj | 18 -- .../CalcViewModel.vcxproj.filters | 6 - .../DataLoaders/CurrencyDataLoader.cpp | 72 +++----- .../DataLoaders/CurrencyDataLoader.h | 8 +- .../DataLoaders/CurrencyHttpClient.cpp | 174 ++++++++++++++---- .../DataLoaders/CurrencyHttpClient.h | 31 ++-- .../DataLoaders/DataLoaderMockConstants.h | 13 -- .../DataLoaders/ICurrencyHttpClient.h | 24 --- .../CalcViewModelCopyForUT.vcxproj | 26 --- .../CalcViewModelCopyForUT.vcxproj.filters | 6 - .../Mocks/CurrencyHttpClient.h | 6 +- 12 files changed, 183 insertions(+), 205 deletions(-) delete mode 100644 src/CalcViewModel/DataLoaders/DataLoaderMockConstants.h delete mode 100644 src/CalcViewModel/DataLoaders/ICurrencyHttpClient.h diff --git a/src/CalcViewModel/ApplicationViewModel.cpp b/src/CalcViewModel/ApplicationViewModel.cpp index f2835fb4..62035c97 100644 --- a/src/CalcViewModel/ApplicationViewModel.cpp +++ b/src/CalcViewModel/ApplicationViewModel.cpp @@ -171,8 +171,8 @@ void ApplicationViewModel::OnModeChanged() if (!m_ConverterViewModel) { auto dataLoader = std::make_shared(ref new GeographicRegion()); - auto currencyDataLoader = std::make_shared(std::make_unique()); - m_ConverterViewModel = ref new UnitConverterViewModel(std::make_shared(dataLoader, currencyDataLoader)); + m_ConverterViewModel = + ref new UnitConverterViewModel(std::make_shared(dataLoader, std::make_shared())); } m_ConverterViewModel->Mode = m_mode; diff --git a/src/CalcViewModel/CalcViewModel.vcxproj b/src/CalcViewModel/CalcViewModel.vcxproj index aedd8d8f..3e17838a 100644 --- a/src/CalcViewModel/CalcViewModel.vcxproj +++ b/src/CalcViewModel/CalcViewModel.vcxproj @@ -325,7 +325,6 @@ - @@ -396,23 +395,6 @@ {fc81ff41-02cd-4cd9-9bc5-45a1e39ac6ed} - - - /DUSE_MOCK_DATA %(AdditionalOptions) - - - - - - - - - - - - - - diff --git a/src/CalcViewModel/CalcViewModel.vcxproj.filters b/src/CalcViewModel/CalcViewModel.vcxproj.filters index 9de55c39..8dd500c0 100644 --- a/src/CalcViewModel/CalcViewModel.vcxproj.filters +++ b/src/CalcViewModel/CalcViewModel.vcxproj.filters @@ -171,12 +171,6 @@ DataLoaders - - DataLoaders - - - DataLoaders - DataLoaders diff --git a/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp b/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp index 9a9787c2..5ff5f581 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp +++ b/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp @@ -89,9 +89,8 @@ namespace CalculatorApp } } -CurrencyDataLoader::CurrencyDataLoader(_In_ unique_ptr client, const wchar_t* forcedResponseLanguage) - : m_client(move(client)) - , m_loadStatus(CurrencyLoadStatus::NotLoaded) +CurrencyDataLoader::CurrencyDataLoader(const wchar_t* forcedResponseLanguage) + : m_loadStatus(CurrencyLoadStatus::NotLoaded) , m_responseLanguage(L"en-US") , m_ratioFormat(L"") , m_timestampFormat(L"") @@ -122,12 +121,7 @@ CurrencyDataLoader::CurrencyDataLoader(_In_ unique_ptr clie } } - if (m_client != nullptr) - { - m_client->SetSourceCurrencyCode(StringReference(DefaultCurrencyCode.data())); - m_client->SetResponseLanguage(m_responseLanguage); - } - + m_client.Initialize(StringReference{ DefaultCurrencyCode.data() }, m_responseLanguage); auto localizationService = LocalizationService::GetInstance(); if (CoreWindow::GetForCurrentThread() != nullptr) { @@ -199,26 +193,29 @@ void CurrencyDataLoader::LoadData() if (!LoadFinished()) { RegisterForNetworkBehaviorChanges(); - create_task([this]() -> task { - vector()>> loadFunctions = { - [this]() { return TryLoadDataFromCacheAsync(); }, - [this]() { return TryLoadDataFromWebAsync(); }, - }; - - bool didLoad = false; - for (auto& f : loadFunctions) + create_task( + [this]() -> task { - didLoad = co_await f(); - if (didLoad) - { - break; - } - } + vector()>> loadFunctions = { + [this]() { return TryLoadDataFromCacheAsync(); }, + [this]() { return TryLoadDataFromWebAsync(); }, + }; - co_return didLoad; - }) + bool didLoad = false; + for (auto& f : loadFunctions) + { + didLoad = co_await f(); + if (didLoad) + { + break; + } + } + + co_return didLoad; + }) .then( - [this](bool didLoad) { + [this](bool didLoad) + { UpdateDisplayedTimestamp(); NotifyDataLoadFinished(didLoad); }, @@ -283,9 +280,7 @@ double CurrencyDataLoader::RoundCurrencyRatio(double ratio) int numberDecimals = FORMATTER_RATE_MIN_DECIMALS; if (ratio < 1) { - numberDecimals = max( - FORMATTER_RATE_MIN_DECIMALS, - (int)(-log10(ratio)) + FORMATTER_RATE_MIN_SIGNIFICANT_DECIMALS); + numberDecimals = max(FORMATTER_RATE_MIN_DECIMALS, (int)(-log10(ratio)) + FORMATTER_RATE_MIN_SIGNIFICANT_DECIMALS); } unsigned long long scale = (unsigned long long)powl(10l, numberDecimals); @@ -314,8 +309,7 @@ pair CurrencyDataLoader::GetCurrencyRatioEquality(_In_ const U auto ratioString = LocalizationStringUtil::GetLocalizedString( m_ratioFormat, digitSymbol, StringReference(unit1.abbreviation.c_str()), roundedFormat, StringReference(unit2.abbreviation.c_str())); - auto accessibleRatioString = - LocalizationStringUtil::GetLocalizedString( + auto accessibleRatioString = LocalizationStringUtil::GetLocalizedString( m_ratioFormat, digitSymbol, StringReference(unit1.accessibleName.c_str()), roundedFormat, StringReference(unit2.accessibleName.c_str())); return make_pair(ratioString->Data(), accessibleRatioString->Data()); @@ -415,18 +409,14 @@ future CurrencyDataLoader::TryLoadDataFromWebAsync() { ResetLoadStatus(); - if (m_client == nullptr) - { - co_return false; - } - if (m_networkAccessBehavior == NetworkAccessBehavior::Offline || (m_networkAccessBehavior == NetworkAccessBehavior::OptIn && !m_meteredOverrideSet)) { co_return false; } - String ^ staticDataResponse = co_await m_client->GetCurrencyMetadata(); - String ^ allRatiosResponse = co_await m_client->GetCurrencyRatios(); + // TODO: determine if below getters are awaitables in production. + String ^ staticDataResponse = m_client.GetCurrencyMetadata(); + String ^ allRatiosResponse = m_client.GetCurrencyRatios(); if (staticDataResponse == nullptr || allRatiosResponse == nullptr) { co_return false; @@ -550,9 +540,7 @@ bool CurrencyDataLoader::TryParseStaticData(_In_ String ^ rawJson, _Inout_ vecto staticData[i] = CurrencyStaticData{ countryCode, countryName, currencyCode, currencyName, currencySymbol }; } - auto sortCountryNames = [](const UCM::CurrencyStaticData & s) { - return ref new String(s.countryName.c_str()); - }; + auto sortCountryNames = [](const UCM::CurrencyStaticData& s) { return ref new String(s.countryName.c_str()); }; LocalizationService::GetInstance()->Sort(staticData, sortCountryNames); @@ -577,7 +565,7 @@ bool CurrencyDataLoader::TryParseAllRatiosData(_In_ String ^ rawJson, _Inout_ Cu { obj = data->GetAt(i)->GetObject(); } - catch (COMException^ e) + catch (COMException ^ e) { if (e->HResult == E_ILLEGAL_METHOD_CALL) { diff --git a/src/CalcViewModel/DataLoaders/CurrencyDataLoader.h b/src/CalcViewModel/DataLoaders/CurrencyDataLoader.h index 6ee878be..99913dd8 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyDataLoader.h +++ b/src/CalcViewModel/DataLoaders/CurrencyDataLoader.h @@ -5,7 +5,7 @@ #include "CalcManager/UnitConverter.h" #include "Common/NetworkManager.h" -#include "ICurrencyHttpClient.h" +#include "CurrencyHttpClient.h" namespace CalculatorApp { @@ -54,9 +54,7 @@ namespace CalculatorApp class CurrencyDataLoader : public UCM::IConverterDataLoader, public UCM::ICurrencyConverterDataLoader { public: - CurrencyDataLoader( - _In_ std::unique_ptr client, - const wchar_t* overrideLanguage = nullptr); + CurrencyDataLoader(const wchar_t* overrideLanguage = nullptr); ~CurrencyDataLoader(); bool LoadFinished(); @@ -114,7 +112,7 @@ namespace CalculatorApp private: Platform::String ^ m_responseLanguage; - std::unique_ptr m_client; + CurrencyHttpClient m_client; bool m_isRtlLanguage; diff --git a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp index 39786f32..59d73fa8 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp +++ b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp @@ -4,47 +4,143 @@ #include "pch.h" #include "CurrencyHttpClient.h" -#ifdef USE_MOCK_DATA -#include "DataLoaderMockConstants.h" -#else -#include "DataLoaderConstants.h" -#endif - -using namespace CalculatorApp::DataLoaders; -using namespace CalculatorApp::ViewModel::DataLoaders; -using namespace Platform; -using namespace std; -using namespace Windows::Foundation; -using namespace Windows::Web::Http; - -CurrencyHttpClient::CurrencyHttpClient() - : m_client(ref new HttpClient()) - , m_responseLanguage(L"en-US") +namespace { -} + constexpr auto MockCurrencyConverterData = LR"( +[ + { + "An": "MAR", + "Rt": 1.00 + }, + { + "An": "MON", + "Rt": 0.50 + }, + { + "An": "NEP", + "Rt": 0.00125 + }, + { + "An": "SAT", + "Rt": 0.25 + }, + { + "An": "URA", + "Rt": 2.75 + }, + { + "An": "VEN", + "Rt": 900.00 + }, + { + "An": "JUP", + "Rt": 1.23456789123456789 + }, + { + "An": "MER", + "Rt": 2.00 + }, + { + "An": "JPY", + "Rt": 0.00125 + }, + { + "An": "JOD", + "Rt": 0.25 + } +])"; + constexpr auto MockCurrencyStaticData = LR"( +[ + { + "CountryCode": "MAR", + "CountryName": "Mars", + "CurrencyCode": "MAR", + "CurrencyName": "The Martian Dollar", + "CurrencySymbol": "¤" + }, + { + "CountryCode": "MON", + "CountryName": "Moon", + "CurrencyCode": "MON", + "CurrencyName": "Moon Bucks", + "CurrencySymbol": "¤" + }, + { + "CountryCode": "NEP", + "CountryName": "Neptune", + "CurrencyCode": "NEP", + "CurrencyName": "Space Coins", + "CurrencySymbol": "¤" + }, + { + "CountryCode": "SAT", + "CountryName": "Saturn", + "CurrencyCode": "SAT", + "CurrencyName": "Rings", + "CurrencySymbol": "¤" + }, + { + "CountryCode": "URA", + "CountryName": "Uranus", + "CurrencyCode": "URA", + "CurrencyName": "Galaxy Credits", + "CurrencySymbol": "¤" + }, + { + "CountryCode": "VEN", + "CountryName": "Venus", + "CurrencyCode": "VEN", + "CurrencyName": "Venusian Seashells", + "CurrencySymbol": "¤" + }, + { + "CountryCode": "JUP", + "CountryName": "Jupiter", + "CurrencyCode": "JUP", + "CurrencyName": "Gas Money", + "CurrencySymbol": "¤" + }, + { + "CountryCode": "MER", + "CountryName": "Mercury", + "CurrencyCode": "MER", + "CurrencyName": "Sun Notes", + "CurrencySymbol": "¤" + }, + { + "CountryCode": "TEST1", + "CountryName": "Test No Fractional Digits", + "CurrencyCode": "JPY", + "CurrencyName": "Test No Fractional Digits", + "CurrencySymbol": "¤" + }, + { + "CountryCode": "TEST2", + "CountryName": "Test Fractional Digits", + "CurrencyCode": "JOD", + "CurrencyName": "Test Fractional Digits", + "CurrencySymbol": "¤" + } +])"; +} // namespace -void CurrencyHttpClient::SetSourceCurrencyCode(String ^ sourceCurrencyCode) +namespace CalculatorApp::ViewModel::DataLoaders { - m_sourceCurrencyCode = sourceCurrencyCode; -} + void CurrencyHttpClient::Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage) + { + m_sourceCurrencyCode = sourceCurrencyCode; + m_responseLanguage = responseLanguage; + } -void CurrencyHttpClient::SetResponseLanguage(String ^ responseLanguage) -{ - m_responseLanguage = responseLanguage; -} + Platform::String ^ CurrencyHttpClient::GetCurrencyMetadata() const + { + (void)m_responseLanguage; // to be used in production. + return ref new Platform::String(MockCurrencyStaticData); + } -IAsyncOperationWithProgress ^ CurrencyHttpClient::GetCurrencyMetadata() -{ - wstring uri = wstring{ sc_MetadataUriLocalizeFor } + m_responseLanguage->Data(); - auto metadataUri = ref new Uri(StringReference(uri.c_str())); - - return m_client->GetStringAsync(metadataUri); -} - -IAsyncOperationWithProgress ^ CurrencyHttpClient::GetCurrencyRatios() -{ - wstring uri = wstring{ sc_RatiosUriRelativeTo } + m_sourceCurrencyCode->Data(); - auto ratiosUri = ref new Uri(StringReference(uri.c_str())); - - return m_client->GetStringAsync(ratiosUri); -} + Platform::String ^ CurrencyHttpClient::GetCurrencyRatios() const + { + (void)m_sourceCurrencyCode; // to be used in production. + return ref new Platform::String(MockCurrencyConverterData); + } +} // namespace CalculatorApp::ViewModel::DataLoaders diff --git a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h index 8f667c7c..05d58a60 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h +++ b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h @@ -1,29 +1,20 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once -#include "ICurrencyHttpClient.h" - -namespace CalculatorApp +namespace CalculatorApp::ViewModel::DataLoaders { - namespace ViewModel::DataLoaders + class CurrencyHttpClient { - class CurrencyHttpClient : public ICurrencyHttpClient - { - public: - CurrencyHttpClient(); + public: + void Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage); - void SetSourceCurrencyCode(Platform::String ^ sourceCurrencyCode) override; - void SetResponseLanguage(Platform::String ^ responseLanguage) override; + Platform::String ^ GetCurrencyMetadata() const; + Platform::String ^ GetCurrencyRatios() const; - Windows::Foundation::IAsyncOperationWithProgress ^ GetCurrencyMetadata() override; - Windows::Foundation::IAsyncOperationWithProgress ^ GetCurrencyRatios() override; - - private: - Windows::Web::Http::HttpClient ^ m_client; - Platform::String ^ m_responseLanguage; - Platform::String ^ m_sourceCurrencyCode; - }; - } + private: + Platform::String ^ m_sourceCurrencyCode; + Platform::String ^ m_responseLanguage; + }; } diff --git a/src/CalcViewModel/DataLoaders/DataLoaderMockConstants.h b/src/CalcViewModel/DataLoaders/DataLoaderMockConstants.h deleted file mode 100644 index 02e7e349..00000000 --- a/src/CalcViewModel/DataLoaders/DataLoaderMockConstants.h +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -#pragma once - -namespace CalculatorApp -{ - namespace DataLoaders - { - static constexpr auto sc_MetadataUriLocalizeFor = L"https://go.microsoft.com/fwlink/?linkid=2091028&localizeFor="; - static constexpr auto sc_RatiosUriRelativeTo = L"https://go.microsoft.com/fwlink/?linkid=2091307&localCurrency="; - } -} diff --git a/src/CalcViewModel/DataLoaders/ICurrencyHttpClient.h b/src/CalcViewModel/DataLoaders/ICurrencyHttpClient.h deleted file mode 100644 index 84104b7b..00000000 --- a/src/CalcViewModel/DataLoaders/ICurrencyHttpClient.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -#pragma once - -namespace CalculatorApp -{ - namespace ViewModel::DataLoaders - { - class ICurrencyHttpClient - { - public: - virtual ~ICurrencyHttpClient() - { - } - - virtual void SetSourceCurrencyCode(Platform::String ^ sourceCurrencyCode) = 0; - virtual void SetResponseLanguage(Platform::String ^ responseLanguage) = 0; - - virtual Windows::Foundation::IAsyncOperationWithProgress ^ GetCurrencyMetadata() = 0; - virtual Windows::Foundation::IAsyncOperationWithProgress ^ GetCurrencyRatios() = 0; - }; - } -} diff --git a/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj b/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj index ed1a9fb1..56532704 100644 --- a/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj +++ b/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj @@ -320,7 +320,6 @@ - @@ -391,31 +390,6 @@ {fc81ff41-02cd-4cd9-9bc5-45a1e39ac6ed} - - - /DUSE_MOCK_DATA %(AdditionalOptions) - stdcpp17 - stdcpp17 - stdcpp17 - stdcpp17 - stdcpp17 - stdcpp17 - stdcpp17 - stdcpp17 - - - - - - - - - - - - - - diff --git a/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj.filters b/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj.filters index 2691d534..d2091270 100644 --- a/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj.filters +++ b/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj.filters @@ -177,12 +177,6 @@ DataLoaders - - DataLoaders - - - DataLoaders - DataLoaders diff --git a/src/CalculatorUnitTests/Mocks/CurrencyHttpClient.h b/src/CalculatorUnitTests/Mocks/CurrencyHttpClient.h index ec88217a..bcb7206c 100644 --- a/src/CalculatorUnitTests/Mocks/CurrencyHttpClient.h +++ b/src/CalculatorUnitTests/Mocks/CurrencyHttpClient.h @@ -1,15 +1,13 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once -#include "CalcViewModel/DataLoaders/ICurrencyHttpClient.h" - namespace CalculatorApp::ViewModel { namespace DataLoaders { - class CurrencyHttpClient : public ICurrencyHttpClient + class CurrencyHttpClient { public: CurrencyHttpClient();