/// <summary> /// Load data about currency exchange rates /// </summary> /// <param name="fiatCurrencyEnum">Fiat currency enum</param> /// <returns>Objects that contains data about currency rates</returns> public FiatCurrencyModel RecieveDataAboutFiatCurreny(FiatCurrencyEnum fiatCurrencyEnum) { dataCache = new DataToFileCache <FiatCurrencyModel>(); cachedFiatCurrencyModel = new FiatCurrencyModel() { Date = new DateTime(1970, 1, 1) }; //To check if something was possible to load string fileName = fiatCurrencyEnum.ToString() + ".txt"; if (dataCache.DataIsCached(fileName)) { cachedFiatCurrencyModel = dataCache.LoadCachedData(fileName); var currentTicks = DateTime.Now.AddDays(-2).Ticks; if (cachedFiatCurrencyModel.Date.Ticks > currentTicks) { return(cachedFiatCurrencyModel); } else { return(TryLoadDataFromApi(fiatCurrencyEnum)); } } else { return(TryLoadDataFromApi(fiatCurrencyEnum)); } }
private FiatCurrencyModel TryLoadDataFromApi(FiatCurrencyEnum fiatCurrencyEnum) { try { cachedFiatCurrencyModel = fiatDataLoader.LoadDataAboutFiatCurrency(fiatCurrencyEnum).Result; dataCache.SaveDataToCache(cachedFiatCurrencyModel, fiatCurrencyEnum.ToString() + ".txt"); return(cachedFiatCurrencyModel); } catch { return(cachedFiatCurrencyModel); } }
private void LoadAndDisplayDataAboutFiatCurrency(FiatCurrencyEnum fromEnum, FiatCurrencyEnum toEnum, string inputText) { if (fromEnum.ToString().Equals(toEnum.ToString())) { Dispatcher.Invoke(() => fiatCurrancyResultTextBlock.Text = $"{double.Parse(inputText):N} {fromEnum} = {double.Parse(inputText):N} {toEnum}"); } else { Dispatcher.Invoke(() => { loadingIndicator.Visibility = Visibility.Visible; cryptoCurrancyConvertButton.Content = ""; }); fiatCurrencyModel = _uiDataProvider.RecieveDataAboutFiatCurreny(fromEnum); if (fiatCurrencyModel.Date == new DateTime(1970, 1, 1)) { Dispatcher.Invoke(() => { addtionalFiatInformationTextBlock.Text = $"Unable to load data\nCheck your internet connection\nor try again later"; fiatCurrancyResultTextBlock.Text = ""; addtionalFiatInformationTextBlock.Foreground = Brushes.Red; }); } else { var userInputAmount = double.Parse(inputText, CultureInfo.InvariantCulture); var convertionResult = ConvertCurrency(fromEnum, userInputAmount, toEnum); Dispatcher.Invoke(() => { fiatCurrancyResultTextBlock.Text = $"{userInputAmount:N} {fromEnum} = {convertionResult:N} {toEnum}"; addtionalFiatInformationTextBlock.Text = "Information for " + fiatCurrencyModel.Date.ToString("dd/MM/yyyy"); addtionalFiatInformationTextBlock.Foreground = Brushes.Orange; }); } Dispatcher.Invoke(() => { loadingIndicator.Visibility = Visibility.Hidden; cryptoCurrancyConvertButton.Content = "Convert"; }); } }
public async Task <FiatCurrencyModel> LoadDataAboutFiatCurrency(FiatCurrencyEnum baseCurrency) { string url = urlBase + "base=" + baseCurrency.ToString().Trim(); using (HttpResponseMessage response = await httpClient.GetAsync(url)) { if (response.IsSuccessStatusCode) { var data = response.Content.ReadAsStringAsync().Result; var convertedData = JsonConvert.DeserializeObject <FiatCurrencyModel>(data); return(convertedData); } else { throw new Exception(response.ReasonPhrase); } } }