mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-23 06:25:19 -07:00
Add support of Currency converter on Uno
This commit is contained in:
parent
28c67c6abe
commit
ed477b782b
7 changed files with 92 additions and 46 deletions
|
@ -63,6 +63,9 @@
|
||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Newtonsoft.Json">
|
||||||
|
<Version>12.0.2</Version>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="Uno.UI" Version="1.45.0-dev.1560" />
|
<PackageReference Include="Uno.UI" Version="1.45.0-dev.1560" />
|
||||||
<PackageReference Include="Uno.UniversalImageLoader" Version="1.9.32" />
|
<PackageReference Include="Uno.UniversalImageLoader" Version="1.9.32" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the MIT License.
|
// Licensed under the MIT License.
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
@ -14,33 +15,13 @@ namespace CalculatorApp
|
||||||
{
|
{
|
||||||
public static string GetLocalizedString(string pMessage, params object[] args)
|
public static string GetLocalizedString(string pMessage, params object[] args)
|
||||||
{
|
{
|
||||||
var stringBuilder = new StringBuilder(pMessage);
|
if (pMessage == null)
|
||||||
|
|
||||||
// This will capture all %<number> strings
|
|
||||||
var matches = Regex
|
|
||||||
.Matches(pMessage, @"%(\d+)")
|
|
||||||
.AsEnumerable()
|
|
||||||
.OrderBy(str => str.Value.Replace("%", ""));
|
|
||||||
|
|
||||||
// If our starting index begin at 1, we will decrease all of them in order to use string.Format
|
|
||||||
if (matches.FirstOrDefault()?.Value == "%1")
|
|
||||||
{
|
{
|
||||||
var addedCharacterFromOriginal = 0;
|
return string.Empty;
|
||||||
|
|
||||||
foreach (var match in matches)
|
|
||||||
{
|
|
||||||
if(int.TryParse(match.Value.Replace("%", ""), out var argumentIndex))
|
|
||||||
{
|
|
||||||
stringBuilder.Remove(match.Index + addedCharacterFromOriginal, match.Length);
|
|
||||||
stringBuilder.Insert(match.Index + addedCharacterFromOriginal, "{" + (argumentIndex - 1).ToString() + "}");
|
|
||||||
// Since we are replace %<number> by {<number>}, we will add one character for each iteration
|
|
||||||
// so we need to consider that
|
|
||||||
addedCharacterFromOriginal++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $"{string.Format(stringBuilder.ToString(), args)}";
|
var pattern = Regex.Replace(pMessage, @"%(?<number>\d+)", m => "{" + (int.Parse(m.Groups["number"].Value) - 1) + "}");
|
||||||
|
return string.Format(CultureInfo.CurrentCulture, pattern, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetLocalizedNarratorAnnouncement(string resourceKey, string formatVariable, params object[] args)
|
public static string GetLocalizedNarratorAnnouncement(string resourceKey, string formatVariable, params object[] args)
|
||||||
|
|
|
@ -123,12 +123,12 @@ namespace CalculatorApp.ViewModel
|
||||||
static string FROM_KEY = "from";
|
static string FROM_KEY = "from";
|
||||||
static string TO_KEY = "to";
|
static string TO_KEY = "to";
|
||||||
|
|
||||||
|
private static string DefaultCurrencyCode = LocalizationService.DefaultCurrencyCode;
|
||||||
|
|
||||||
// Fallback default values.
|
// Fallback default values.
|
||||||
static string DEFAULT_FROM_CURRENCY = DefaultCurrencyCode;
|
static string DEFAULT_FROM_CURRENCY = DefaultCurrencyCode;
|
||||||
static string DEFAULT_TO_CURRENCY = "EUR";
|
static string DEFAULT_TO_CURRENCY = "EUR";
|
||||||
|
|
||||||
private static string DefaultCurrencyCode = LocalizationService.DefaultCurrencyCode;
|
|
||||||
|
|
||||||
public CurrencyDataLoader(ICurrencyHttpClient client)
|
public CurrencyDataLoader(ICurrencyHttpClient client)
|
||||||
{
|
{
|
||||||
this.m_client = client;
|
this.m_client = client;
|
||||||
|
@ -507,6 +507,7 @@ namespace CalculatorApp.ViewModel
|
||||||
|
|
||||||
bool TryParseStaticData(String rawJson, CalculatorList<UCM.CurrencyStaticData> staticData)
|
bool TryParseStaticData(String rawJson, CalculatorList<UCM.CurrencyStaticData> staticData)
|
||||||
{
|
{
|
||||||
|
#if NETFX_CORE // TODO UNO
|
||||||
JsonArray data = null;
|
JsonArray data = null;
|
||||||
if (!JsonArray.TryParse(rawJson, out data))
|
if (!JsonArray.TryParse(rawJson, out data))
|
||||||
{
|
{
|
||||||
|
@ -519,7 +520,7 @@ namespace CalculatorApp.ViewModel
|
||||||
string currencyName = "";
|
string currencyName = "";
|
||||||
string currencySymbol = "";
|
string currencySymbol = "";
|
||||||
|
|
||||||
string[] values = {countryCode, countryName, currencyCode, currencyName, currencySymbol};
|
string[] values = { countryCode, countryName, currencyCode, currencyName, currencySymbol };
|
||||||
|
|
||||||
Debug.Assert(values.Length == STATIC_DATA_PROPERTIES.Length);
|
Debug.Assert(values.Length == STATIC_DATA_PROPERTIES.Length);
|
||||||
staticData.Clear();
|
staticData.Clear();
|
||||||
|
@ -540,10 +541,36 @@ namespace CalculatorApp.ViewModel
|
||||||
staticData.Sort((UCM.CurrencyStaticData unit1, UCM.CurrencyStaticData unit2) => { return unit1.countryName.CompareTo(unit2.countryName) < 0; });
|
staticData.Sort((UCM.CurrencyStaticData unit1, UCM.CurrencyStaticData unit2) => { return unit1.countryName.CompareTo(unit2.countryName) < 0; });
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
#else
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var items = Newtonsoft.Json.JsonConvert.DeserializeObject<UCM.CurrencyStaticData[]>(rawJson);
|
||||||
|
staticData.Clear();
|
||||||
|
foreach (var item in items)
|
||||||
|
{
|
||||||
|
staticData.Add(item);
|
||||||
|
}
|
||||||
|
staticData.Sort((UCM.CurrencyStaticData unit1, UCM.CurrencyStaticData unit2) => { return unit1.countryName.CompareTo(unit2.countryName) < 0; });
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
public class JsonCurrencyRatio
|
||||||
|
{
|
||||||
|
public double Rt { get; set; }
|
||||||
|
|
||||||
|
public string An { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryParseAllRatiosData(String rawJson, CurrencyRatioMap allRatios)
|
public bool TryParseAllRatiosData(String rawJson, CurrencyRatioMap allRatios)
|
||||||
{
|
{
|
||||||
|
#if NETFX_CORE // TODO UNO
|
||||||
JsonArray data = null;
|
JsonArray data = null;
|
||||||
if (!JsonArray.TryParse(rawJson, out data))
|
if (!JsonArray.TryParse(rawJson, out data))
|
||||||
{
|
{
|
||||||
|
@ -565,6 +592,26 @@ namespace CalculatorApp.ViewModel
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
#else
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string sourceCurrencyCode = DefaultCurrencyCode;
|
||||||
|
|
||||||
|
var items = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonCurrencyRatio[]>(rawJson);
|
||||||
|
allRatios.Clear();
|
||||||
|
foreach (var item in items)
|
||||||
|
{
|
||||||
|
allRatios.Add(item.An, new UCM.CurrencyRatio(item.Rt, sourceCurrencyCode, item.An));
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// FinalizeUnits
|
// FinalizeUnits
|
||||||
|
@ -712,11 +759,15 @@ namespace CalculatorApp.ViewModel
|
||||||
DateTime epoch = default(DateTime);
|
DateTime epoch = default(DateTime);
|
||||||
if (m_cacheTimestamp.ToUniversalTime() != epoch.ToUniversalTime())
|
if (m_cacheTimestamp.ToUniversalTime() != epoch.ToUniversalTime())
|
||||||
{
|
{
|
||||||
DateTimeFormatter dateFormatter = new DateTimeFormatter("{month.abbreviated} {day.integer}, {year.full}");
|
// TODO UNO
|
||||||
string date = dateFormatter.Format(m_cacheTimestamp);
|
//DateTimeFormatter dateFormatter = new DateTimeFormatter("{month.abbreviated} {day.integer}, {year.full}");
|
||||||
|
//string date = dateFormatter.Format(m_cacheTimestamp);
|
||||||
|
var date = m_cacheTimestamp.ToString("D");
|
||||||
|
|
||||||
DateTimeFormatter timeFormatter = new DateTimeFormatter("shorttime");
|
// TODO UNO
|
||||||
string time = timeFormatter.Format(m_cacheTimestamp);
|
//DateTimeFormatter timeFormatter = new DateTimeFormatter("shorttime");
|
||||||
|
//string time = timeFormatter.Format(m_cacheTimestamp);
|
||||||
|
var time = m_cacheTimestamp.ToString("t");
|
||||||
|
|
||||||
timestamp = LocalizationStringUtil.GetLocalizedString(m_timestampFormat, date, time);
|
timestamp = LocalizationStringUtil.GetLocalizedString(m_timestampFormat, date, time);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Windows.Foundation;
|
using Windows.Foundation;
|
||||||
using Windows.Web.Http;
|
using Windows.Web.Http;
|
||||||
|
|
||||||
|
@ -11,7 +12,8 @@ namespace CalculatorApp.DataLoaders
|
||||||
{
|
{
|
||||||
public class CurrencyHttpClient : ICurrencyHttpClient
|
public class CurrencyHttpClient : ICurrencyHttpClient
|
||||||
{
|
{
|
||||||
Windows.Web.Http.HttpClient m_client;
|
// TODO UNO: Windows.Web.Http.HttpClient
|
||||||
|
System.Net.Http.HttpClient m_client;
|
||||||
string m_responseLanguage;
|
string m_responseLanguage;
|
||||||
string m_sourceCurrencyCode;
|
string m_sourceCurrencyCode;
|
||||||
|
|
||||||
|
@ -20,7 +22,7 @@ namespace CalculatorApp.DataLoaders
|
||||||
|
|
||||||
public CurrencyHttpClient()
|
public CurrencyHttpClient()
|
||||||
{
|
{
|
||||||
m_client = new HttpClient();
|
m_client = new System.Net.Http.HttpClient();
|
||||||
m_responseLanguage = "en-US";
|
m_responseLanguage = "en-US";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +36,7 @@ namespace CalculatorApp.DataLoaders
|
||||||
m_responseLanguage = responseLanguage;
|
m_responseLanguage = responseLanguage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IAsyncOperationWithProgress<String, HttpProgress> GetCurrencyMetadata()
|
public Task<String> GetCurrencyMetadata()
|
||||||
{
|
{
|
||||||
string uri = sc_MetadataUriLocalizeFor + m_responseLanguage;
|
string uri = sc_MetadataUriLocalizeFor + m_responseLanguage;
|
||||||
var metadataUri = new Uri(uri);
|
var metadataUri = new Uri(uri);
|
||||||
|
@ -42,7 +44,7 @@ namespace CalculatorApp.DataLoaders
|
||||||
return m_client.GetStringAsync(metadataUri);
|
return m_client.GetStringAsync(metadataUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IAsyncOperationWithProgress<String, HttpProgress> GetCurrencyRatios()
|
public Task<String> GetCurrencyRatios()
|
||||||
{
|
{
|
||||||
string uri = sc_RatiosUriRelativeTo + m_sourceCurrencyCode;
|
string uri = sc_RatiosUriRelativeTo + m_sourceCurrencyCode;
|
||||||
var ratiosUri = new Uri(uri);
|
var ratiosUri = new Uri(uri);
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace CalculatorApp.DataLoaders
|
namespace CalculatorApp.DataLoaders
|
||||||
{
|
{
|
||||||
|
@ -12,7 +13,7 @@ namespace CalculatorApp.DataLoaders
|
||||||
void SetSourceCurrencyCode(string sourceCurrencyCode);
|
void SetSourceCurrencyCode(string sourceCurrencyCode);
|
||||||
void SetResponseLanguage(string responseLanguage);
|
void SetResponseLanguage(string responseLanguage);
|
||||||
|
|
||||||
Windows.Foundation.IAsyncOperationWithProgress<string, Windows.Web.Http.HttpProgress> GetCurrencyMetadata();
|
Task<string> GetCurrencyMetadata();
|
||||||
Windows.Foundation.IAsyncOperationWithProgress<string, Windows.Web.Http.HttpProgress> GetCurrencyRatios();
|
Task<string> GetCurrencyRatios();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
<NoWarn>NU1701</NoWarn>
|
<NoWarn>NU1701</NoWarn>
|
||||||
<WasmShellGenerateCompressedFiles Condition="'$(Configuration)'=='Debug'">false</WasmShellGenerateCompressedFiles>
|
<WasmShellGenerateCompressedFiles Condition="'$(Configuration)'=='Debug'">false</WasmShellGenerateCompressedFiles>
|
||||||
<MonoRuntimeDebuggerEnabled Condition="'$(Configuration)'=='Debug'">true</MonoRuntimeDebuggerEnabled>
|
<MonoRuntimeDebuggerEnabled Condition="'$(Configuration)'=='Debug'">true</MonoRuntimeDebuggerEnabled>
|
||||||
<WasmShellMonoRuntimeExecutionMode Condition="$([MSBuild]::IsOsPlatform('Linux'))">FullAOT</WasmShellMonoRuntimeExecutionMode>
|
<WasmShellMonoRuntimeExecutionMode Condition="$([MSBuild]::IsOsPlatform('Linux'))">InterpreterAndAOT</WasmShellMonoRuntimeExecutionMode>
|
||||||
<MonoWasmRuntimeConfiguration>release-dynamic</MonoWasmRuntimeConfiguration>
|
<MonoWasmRuntimeConfiguration>release-dynamic</MonoWasmRuntimeConfiguration>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -43,10 +43,15 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
|
||||||
<PackageReference Include="Uno.UI" Version="1.45.0-dev.1560" />
|
<PackageReference Include="Uno.UI" Version="1.45.0-dev.1560" />
|
||||||
<PackageReference Include="Uno.Wasm.Bootstrap" Version="1.0.0-dev.281" />
|
<PackageReference Include="Uno.Wasm.Bootstrap" Version="1.0.0-dev.281" />
|
||||||
<DotNetCliToolReference Include="Uno.Wasm.Bootstrap.Cli" Version="1.0.0-dev.276" />
|
<DotNetCliToolReference Include="Uno.Wasm.Bootstrap.Cli" Version="1.0.0-dev.276" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<MonoRuntimeMixedModeExcludedAssembly Include="Newtonsoft.Json" />
|
||||||
|
<MonoRuntimeMixedModeExcludedAssembly Include="System.Data" />
|
||||||
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="..\CalcManager\CalcManager.wasm" Link="CalcManager.wasm" />
|
<Content Include="..\CalcManager\CalcManager.wasm" Link="CalcManager.wasm" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -205,6 +205,9 @@
|
||||||
<BundleResource Include="Resources\Fonts\winjs-symbols.ttf" />
|
<BundleResource Include="Resources\Fonts\winjs-symbols.ttf" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Newtonsoft.Json">
|
||||||
|
<Version>12.0.2</Version>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="Uno.UI" Version="1.45.0-dev.1560" />
|
<PackageReference Include="Uno.UI" Version="1.45.0-dev.1560" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue