예제 #1
0
        private void UpdateData()
        {
            Task.Run(async() =>
            {
                CurrencyRatesResponse response;
                var serializer  = new JsonSerializer();
                var pinnedRates = AppPropertiesHelper.GetPinnedRates();

                try
                {
                    using (var stream = await Client.GetStreamAsync(ratesUrl + Date.ToString("dd.MM.yyyy")))
                        using (var streamReader = new StreamReader(stream))
                            using (var json = new JsonTextReader(streamReader))
                            {
                                response = serializer.Deserialize <CurrencyRatesResponse>(json);
                            };

                    response.ExchangeRate.RemoveAll(p => string.IsNullOrEmpty(p.Currency) || p.Currency == "UAH");

                    foreach (var item in response.ExchangeRate)
                    {
                        if (pinnedRates.Contains(item.Currency))
                        {
                            item.Pinned = true;
                        }
                    }

                    var sortedRates = response.ExchangeRate.OrderByDescending(p => p.Pinned).ThenBy(d => pinnedRates.IndexOf(d.Currency)).ToList();

                    Rates = new ObservableCollection <ExchangeRate>(sortedRates);

                    SaveData();
                }
                catch (HttpRequestException)
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Application.Current.MainPage.DisplayAlert("Ошибка", "Нет подключения к Интернет", "Закрыть");

                        Application.Current.Properties.TryGetValue("rates", out object value);
                        if (value is ObservableCollection <ExchangeRate> savedRates)
                        {
                            Rates = new ObservableCollection <ExchangeRate>(savedRates);
                        }
                    });
                }
                catch (Exception e)
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Application.Current.MainPage.DisplayAlert("Ошибка", e.Message, "Закрыть");
                    });
                }
            });
        }
예제 #2
0
        private void PinRate(ExchangeRate obj)
        {
            IsRateIdle = false;
            (PinRateCommand as Command)?.ChangeCanExecute();


            obj.Pinned = !obj.Pinned;
            AppPropertiesHelper.UpdatePinnedRates(obj);
            var pinnedRates = AppPropertiesHelper.GetPinnedRates();

            var sortedRates = Rates.OrderByDescending(p => p.Pinned).ThenBy(d => pinnedRates.IndexOf(d.Currency)).ToList();

            Rates = new ObservableCollection <ExchangeRate>(sortedRates);

            IsRateIdle = true;
            (PinRateCommand as Command)?.ChangeCanExecute();
        }
예제 #3
0
 private void SaveData()
 {
     AppPropertiesHelper.Save("rates", JsonConvert.SerializeObject(Rates));
 }