Esempio n. 1
0
        /// <summary>
        /// Updates the weather data asynchronously.
        /// </summary>
        public async void UpdateWeatherDataAsync()
        {
            try
            {
                imgLoading.Visibility = Visibility.Visible;

                // Clear the current data, call and await OpenWeatherMap API to return data
                // and cache the data
                // and update the last update time.
                allWeatherData.Clear();
                await OpenWeatherMapFacade.GetWeatherDataAsync(SelectedCity, allWeatherData, ApiKey);

                OpenWeatherMapFacade.SaveWeatherDataToFileCache(SelectedCity, allWeatherData);
                OpenWeatherMapFacade.SaveLastUpdatedTime();
                SetCurrentWeatherData();
                imgLoading.Visibility = Visibility.Hidden;
            }
            catch (Exception e)
            {
                MessageBox.Show($"Error Occurred:\n{e.Message}\n\nStack:\n{e.StackTrace}",
                                "Error",
                                MessageBoxButton.OK,
                                MessageBoxImage.Error);
            }
            finally
            {
                imgLoading.Visibility = Visibility.Hidden;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Application main window.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            GetApiKey();

            // Initialize timer for weather updates. Weather data is updated automatically
            // every 12m 20sec, otherwise a cache is used. This is to avoid excessive calls
            // OpenWeatherMap API.
            UpdateTimer          = new DispatcherTimer();
            UpdateTimer.Tick    += new EventHandler(UpdateTimer_Tick);
            UpdateTimer.Interval = new TimeSpan(0, 12, 20);
            UpdateTimer.Start();

            // Initialize collections
            allWeatherData     = new ObservableCollection <WeatherCheck.Models.List>();
            currentWeatherData = new ObservableCollection <WeatherCheck.Models.List>();
            cityList           = new List <string>();

            // Keeps track of time elapsed since the last API -call
            LastUpdatedTime = OpenWeatherMapFacade.GetLastUpdatedTime();

            // Load the list of Finland cities/towns
            // and get currently selected city.
            LoadCityList();
            GetSelectedCity();

            if (!String.IsNullOrEmpty(SelectedCity))
            {
                if (IsWeatherUpdateAllowed())
                {
                    UpdateWeatherDataAsync();
                }
                else
                {
                    OpenWeatherMapFacade.LoadWeatherDataFromFileCache(SelectedCity, allWeatherData);
                    SetCurrentWeatherData();
                }
            }

            // Initialize databindings
            icCurrentWeatherData.ItemsSource = currentWeatherData;
            icWeatherData.ItemsSource        = allWeatherData;
            tbSelectedCityName.Text          = SelectedCity;
        }