Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple with a mocked service #2256

Merged
merged 7 commits into from
Nov 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor
tian-lt committed Nov 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 80bbb1ad81df2b1048e2569e5266457f48e53c16
4 changes: 2 additions & 2 deletions src/CalcViewModel/ApplicationViewModel.cpp
Original file line number Diff line number Diff line change
@@ -171,8 +171,8 @@ void ApplicationViewModel::OnModeChanged()
if (!m_ConverterViewModel)
{
auto dataLoader = std::make_shared<UnitConverterDataLoader>(ref new GeographicRegion());
auto currencyDataLoader = std::make_shared<CurrencyDataLoader>(std::make_unique<CurrencyHttpClient>());
m_ConverterViewModel = ref new UnitConverterViewModel(std::make_shared<UnitConversionManager::UnitConverter>(dataLoader, currencyDataLoader));
m_ConverterViewModel =
ref new UnitConverterViewModel(std::make_shared<UnitConversionManager::UnitConverter>(dataLoader, std::make_shared<CurrencyDataLoader>()));
}

m_ConverterViewModel->Mode = m_mode;
18 changes: 0 additions & 18 deletions src/CalcViewModel/CalcViewModel.vcxproj
Original file line number Diff line number Diff line change
@@ -325,7 +325,6 @@
<ClInclude Include="Common\Utils.h" />
<ClInclude Include="DataLoaders\CurrencyDataLoader.h" />
<ClInclude Include="DataLoaders\CurrencyHttpClient.h" />
<ClInclude Include="DataLoaders\ICurrencyHttpClient.h" />
<ClInclude Include="DataLoaders\UnitConverterDataConstants.h" />
<ClInclude Include="DataLoaders\UnitConverterDataLoader.h" />
<ClInclude Include="DateCalculatorViewModel.h" />
@@ -396,23 +395,6 @@
<Project>{fc81ff41-02cd-4cd9-9bc5-45a1e39ac6ed}</Project>
</ProjectReference>
</ItemGroup>
<ItemDefinitionGroup Condition="!Exists('DataLoaders\DataLoaderConstants.h')">
<ClCompile>
<AdditionalOptions>/DUSE_MOCK_DATA %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
</ItemDefinitionGroup>
<Choose>
<When Condition="Exists('DataLoaders\DataLoaderConstants.h')">
<ItemGroup>
<ClInclude Include="DataLoaders\DataLoaderConstants.h" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<ClInclude Include="DataLoaders\DataLoaderMockConstants.h" />
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<None Include="DataLoaders\DefaultFromToCurrency.json" />
</ItemGroup>
6 changes: 0 additions & 6 deletions src/CalcViewModel/CalcViewModel.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -171,12 +171,6 @@
<ClInclude Include="DataLoaders\CurrencyHttpClient.h">
<Filter>DataLoaders</Filter>
</ClInclude>
<ClInclude Include="DataLoaders\DataLoaderMockConstants.h">
<Filter>DataLoaders</Filter>
</ClInclude>
<ClInclude Include="DataLoaders\ICurrencyHttpClient.h">
<Filter>DataLoaders</Filter>
</ClInclude>
<ClInclude Include="DataLoaders\UnitConverterDataConstants.h">
<Filter>DataLoaders</Filter>
</ClInclude>
68 changes: 28 additions & 40 deletions src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp
Original file line number Diff line number Diff line change
@@ -89,9 +89,8 @@ namespace CalculatorApp
}
}

CurrencyDataLoader::CurrencyDataLoader(_In_ unique_ptr<ICurrencyHttpClient> 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<ICurrencyHttpClient> 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<bool> {
vector<function<future<bool>()>> loadFunctions = {
[this]() { return TryLoadDataFromCacheAsync(); },
[this]() { return TryLoadDataFromWebAsync(); },
};

bool didLoad = false;
for (auto& f : loadFunctions)
create_task(
[this]() -> task<bool>
{
didLoad = co_await f();
if (didLoad)
vector<function<future<bool>()>> loadFunctions = {
[this]() { return TryLoadDataFromCacheAsync(); },
[this]() { return TryLoadDataFromWebAsync(); },
};

bool didLoad = false;
for (auto& f : loadFunctions)
{
break;
didLoad = co_await f();
if (didLoad)
{
break;
}
}
}

co_return didLoad;
})
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<wstring, wstring> 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<bool> 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<UCM::CurrencyStaticData>(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)
{
8 changes: 3 additions & 5 deletions src/CalcViewModel/DataLoaders/CurrencyDataLoader.h
Original file line number Diff line number Diff line change
@@ -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<CalculatorApp::ViewModel::DataLoaders::ICurrencyHttpClient> 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<CalculatorApp::ViewModel::DataLoaders::ICurrencyHttpClient> m_client;
CurrencyHttpClient m_client;

bool m_isRtlLanguage;

Loading