コード例 #1
0
        static decimal GetStoredRate(string from, string to)
        {
            var result = webRates[from];

            var obj = result["quotes"];
            var val = (string)obj[from + to];

            return(CurrencyParser.ParseCurrencyString(val));
        }
コード例 #2
0
        public static decimal GetRate(string from, string to)
        {
            if (string.IsNullOrEmpty(urlKey) || from == to)
            {
                return(1.00M);
            }

            if (webRates.ContainsKey(from) &&
                webRates[from].timestamp <= DateTimeOffset.UtcNow.ToUnixTimeSeconds() + 7200)
            {
                return(GetStoredRate(from, to));
            }
            try
            {
                var responce = GetWebCurrencyRate(from);

                var rates = CurrencyParser.ParseCurrencyJSON(responce);

                if (rates.success != "true")
                {
                    return(FallBackRateCalculator());
                }

                webRates[from] = rates;
                return(GetStoredRate(from, to));
            }

            catch
            {
                Toast.MakeText(Application.Context, "Some error occured!",
                               ToastLength.Short).Show();
                return(1.00M);
            }

            decimal FallBackRateCalculator()
            {
                Toast.MakeText(Application.Context, $"Cannot get rate, trying to make a {from} to USD to {to} conversion!",
                               ToastLength.Short).Show();

                decimal from_From_ToUsd = GetRate(urlFromCurrencyUSD, from);
                decimal fromUSDTo_To_   = GetRate(urlFromCurrencyUSD, to);

                return(fromUSDTo_To_ / from_From_ToUsd);
            }
        }