mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 22:23:29 -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,11 +63,14 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</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.UniversalImageLoader" Version="1.9.32" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
|
||||
</ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\AssemblyVersion.Android.cs">
|
||||
<Link>Properties\AssemblyVersion.Android.cs</Link>
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the MIT License.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
@ -14,33 +15,13 @@ namespace CalculatorApp
|
|||
{
|
||||
public static string GetLocalizedString(string pMessage, params object[] args)
|
||||
{
|
||||
var stringBuilder = new StringBuilder(pMessage);
|
||||
|
||||
// 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")
|
||||
if (pMessage == null)
|
||||
{
|
||||
var addedCharacterFromOriginal = 0;
|
||||
|
||||
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.Empty;
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
|
@ -123,12 +123,12 @@ namespace CalculatorApp.ViewModel
|
|||
static string FROM_KEY = "from";
|
||||
static string TO_KEY = "to";
|
||||
|
||||
private static string DefaultCurrencyCode = LocalizationService.DefaultCurrencyCode;
|
||||
|
||||
// Fallback default values.
|
||||
static string DEFAULT_FROM_CURRENCY = DefaultCurrencyCode;
|
||||
static string DEFAULT_TO_CURRENCY = "EUR";
|
||||
|
||||
private static string DefaultCurrencyCode = LocalizationService.DefaultCurrencyCode;
|
||||
|
||||
public CurrencyDataLoader(ICurrencyHttpClient client)
|
||||
{
|
||||
this.m_client = client;
|
||||
|
@ -507,6 +507,7 @@ namespace CalculatorApp.ViewModel
|
|||
|
||||
bool TryParseStaticData(String rawJson, CalculatorList<UCM.CurrencyStaticData> staticData)
|
||||
{
|
||||
#if NETFX_CORE // TODO UNO
|
||||
JsonArray data = null;
|
||||
if (!JsonArray.TryParse(rawJson, out data))
|
||||
{
|
||||
|
@ -519,7 +520,7 @@ namespace CalculatorApp.ViewModel
|
|||
string currencyName = "";
|
||||
string currencySymbol = "";
|
||||
|
||||
string[] values = {countryCode, countryName, currencyCode, currencyName, currencySymbol};
|
||||
string[] values = { countryCode, countryName, currencyCode, currencyName, currencySymbol };
|
||||
|
||||
Debug.Assert(values.Length == STATIC_DATA_PROPERTIES.Length);
|
||||
staticData.Clear();
|
||||
|
@ -540,10 +541,36 @@ namespace CalculatorApp.ViewModel
|
|||
staticData.Sort((UCM.CurrencyStaticData unit1, UCM.CurrencyStaticData unit2) => { return unit1.countryName.CompareTo(unit2.countryName) < 0; });
|
||||
|
||||
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)
|
||||
{
|
||||
#if NETFX_CORE // TODO UNO
|
||||
JsonArray data = null;
|
||||
if (!JsonArray.TryParse(rawJson, out data))
|
||||
{
|
||||
|
@ -565,6 +592,26 @@ namespace CalculatorApp.ViewModel
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -712,11 +759,15 @@ namespace CalculatorApp.ViewModel
|
|||
DateTime epoch = default(DateTime);
|
||||
if (m_cacheTimestamp.ToUniversalTime() != epoch.ToUniversalTime())
|
||||
{
|
||||
DateTimeFormatter dateFormatter = new DateTimeFormatter("{month.abbreviated} {day.integer}, {year.full}");
|
||||
string date = dateFormatter.Format(m_cacheTimestamp);
|
||||
// TODO UNO
|
||||
//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");
|
||||
string time = timeFormatter.Format(m_cacheTimestamp);
|
||||
// TODO UNO
|
||||
//DateTimeFormatter timeFormatter = new DateTimeFormatter("shorttime");
|
||||
//string time = timeFormatter.Format(m_cacheTimestamp);
|
||||
var time = m_cacheTimestamp.ToString("t");
|
||||
|
||||
timestamp = LocalizationStringUtil.GetLocalizedString(m_timestampFormat, date, time);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Foundation;
|
||||
using Windows.Web.Http;
|
||||
|
||||
|
@ -11,7 +12,8 @@ namespace CalculatorApp.DataLoaders
|
|||
{
|
||||
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_sourceCurrencyCode;
|
||||
|
||||
|
@ -20,7 +22,7 @@ namespace CalculatorApp.DataLoaders
|
|||
|
||||
public CurrencyHttpClient()
|
||||
{
|
||||
m_client = new HttpClient();
|
||||
m_client = new System.Net.Http.HttpClient();
|
||||
m_responseLanguage = "en-US";
|
||||
}
|
||||
|
||||
|
@ -34,7 +36,7 @@ namespace CalculatorApp.DataLoaders
|
|||
m_responseLanguage = responseLanguage;
|
||||
}
|
||||
|
||||
public IAsyncOperationWithProgress<String, HttpProgress> GetCurrencyMetadata()
|
||||
public Task<String> GetCurrencyMetadata()
|
||||
{
|
||||
string uri = sc_MetadataUriLocalizeFor + m_responseLanguage;
|
||||
var metadataUri = new Uri(uri);
|
||||
|
@ -42,7 +44,7 @@ namespace CalculatorApp.DataLoaders
|
|||
return m_client.GetStringAsync(metadataUri);
|
||||
}
|
||||
|
||||
public IAsyncOperationWithProgress<String, HttpProgress> GetCurrencyRatios()
|
||||
public Task<String> GetCurrencyRatios()
|
||||
{
|
||||
string uri = sc_RatiosUriRelativeTo + m_sourceCurrencyCode;
|
||||
var ratiosUri = new Uri(uri);
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CalculatorApp.DataLoaders
|
||||
{
|
||||
|
@ -12,7 +13,7 @@ namespace CalculatorApp.DataLoaders
|
|||
void SetSourceCurrencyCode(string sourceCurrencyCode);
|
||||
void SetResponseLanguage(string responseLanguage);
|
||||
|
||||
Windows.Foundation.IAsyncOperationWithProgress<string, Windows.Web.Http.HttpProgress> GetCurrencyMetadata();
|
||||
Windows.Foundation.IAsyncOperationWithProgress<string, Windows.Web.Http.HttpProgress> GetCurrencyRatios();
|
||||
Task<string> GetCurrencyMetadata();
|
||||
Task<string> GetCurrencyRatios();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<NoWarn>NU1701</NoWarn>
|
||||
<WasmShellGenerateCompressedFiles Condition="'$(Configuration)'=='Debug'">false</WasmShellGenerateCompressedFiles>
|
||||
<MonoRuntimeDebuggerEnabled Condition="'$(Configuration)'=='Debug'">true</MonoRuntimeDebuggerEnabled>
|
||||
<WasmShellMonoRuntimeExecutionMode Condition="$([MSBuild]::IsOsPlatform('Linux'))">FullAOT</WasmShellMonoRuntimeExecutionMode>
|
||||
<WasmShellMonoRuntimeExecutionMode Condition="$([MSBuild]::IsOsPlatform('Linux'))">InterpreterAndAOT</WasmShellMonoRuntimeExecutionMode>
|
||||
<MonoWasmRuntimeConfiguration>release-dynamic</MonoWasmRuntimeConfiguration>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
|
@ -43,10 +43,15 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" 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.Wasm.Bootstrap" Version="1.0.0-dev.281" />
|
||||
<DotNetCliToolReference Include="Uno.Wasm.Bootstrap.Cli" Version="1.0.0-dev.276" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<MonoRuntimeMixedModeExcludedAssembly Include="Newtonsoft.Json" />
|
||||
<MonoRuntimeMixedModeExcludedAssembly Include="System.Data" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="..\CalcManager\CalcManager.wasm" Link="CalcManager.wasm" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -205,10 +205,13 @@
|
|||
<BundleResource Include="Resources\Fonts\winjs-symbols.ttf" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json">
|
||||
<Version>12.0.2</Version>
|
||||
</PackageReference>
|
||||
<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.Filter" Version="1.1.1" />
|
||||
</ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition=" '$(Platform)' == 'iPhoneSimulator' ">
|
||||
<NativeReference Include="NativeReferences/x86_64/libCalcManager.a">
|
||||
<Kind>Static</Kind>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue