Example #1
0
        private async Task ExecuteLoadItemsCommand()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                await Task.Run(() =>
                {
                    _homepageCurrentWeatherData    = LocalDataRepository.GetHomePageWeatherData();
                    _homepageCurrentWeatherDetails = LocalDataRepository.GetHomePageWeatherDetails();


                    OnPropertyChanged("HomepageCurrentWeatherDetails");
                    OnPropertyChanged("HomepageCurrentWeatherData");
                });

                await ValidateListVisibility();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
Example #2
0
        public ItemsViewModel()
        {
            Title = "OpenWeather";

            _homepageCurrentWeatherData    = LocalDataRepository.GetHomePageWeatherData();
            _homepageCurrentWeatherDetails = LocalDataRepository.GetHomePageWeatherDetails();

            Task.Run(async() =>
            {
                if (_homepageCurrentWeatherData == null && _homepageCurrentWeatherDetails == null)
                {
                    await ExecuteSearchCommand("Mabalacat City");
                }
                else
                {
                    await ExecuteLoadItemsCommand();
                }
            });

            MessagingCenter.Subscribe <WeatherApiResonseData>(this, "LoadData", async(obj) =>
            {
                WeatherApiResonseData weatherApiResponse = obj as WeatherApiResonseData;
                await ExecuteSearchCommand(weatherApiResponse.name);
            });

            MessagingCenter.Subscribe <object>(this, "LoadLocation", async(obj) =>
            {
                await ExecuteGetLocationCommand();
            });
        }
Example #3
0
        private async Task <Tuple <WeatherApiResonseData, OneCallWeatherAPIResponseData> > SearchLocation(string searchInput)
        {
            WeatherApiResonseData         weather        = new WeatherApiResonseData();
            OneCallWeatherAPIResponseData weatherDetails = new OneCallWeatherAPIResponseData();

            if (searchInput.Any(char.IsDigit))
            {
                try
                {
                    weather = await OpenWeatherService.GetCurrentWeatherByZipCodeAsync(searchInput);

                    weatherDetails = await OpenWeatherService.GetOneCallAPIRequestAsync(weather.coord.lat, weather.coord.lon);
                }
                catch (Exception ex)
                {
                    Debug.Write(ex.Message);
                    weather = await OpenWeatherService.GetCurrentWeatherByCityNameAsync(searchInput);

                    weatherDetails = await OpenWeatherService.GetOneCallAPIRequestAsync(weather.coord.lat, weather.coord.lon);
                }
            }
            else
            {
                weather = await OpenWeatherService.GetCurrentWeatherByCityNameAsync(searchInput);

                weatherDetails = await OpenWeatherService.GetOneCallAPIRequestAsync(weather.coord.lat, weather.coord.lon);
            }

            return(Tuple.Create(weather, weatherDetails));
        }
        public void InsertHomePageWeatherData(WeatherApiResonseData weatherApiResonseData)
        {
            WeatherApiResonseData _homepageLocalData = App.HomePageLocalWeatherData;

            _homepageLocalData           = weatherApiResonseData;
            App.HomePageLocalWeatherData = _homepageLocalData;
        }
        public void DeleteStoredWeatherData(WeatherApiResonseData weatherApiResonseData)
        {
            ObservableCollection <WeatherApiResonseData> storedWeatherApiResponseData = App.StoredWeatherApiResponseData;

            storedWeatherApiResponseData.Remove(weatherApiResonseData);

            App.HistoryLocalWeatherData = App.StoredWeatherApiResponseData = storedWeatherApiResponseData;
        }
Example #6
0
        private async Task UpdateStoredWeatherData(WeatherApiResonseData weatherApiResonse, OneCallWeatherAPIResponseData weatherAPIResponseDetails)
        {
            await Task.Run(() =>
            {
                weatherApiResonse.search_time = DateTime.Now;

                _homepageCurrentWeatherData    = weatherApiResonse;
                _homepageCurrentWeatherDetails = weatherAPIResponseDetails;
                OnPropertyChanged("HomepageCurrentWeatherData");
                OnPropertyChanged("HomepageCurrentWeatherDetails");

                LocalDataRepository.InsertHomePageWeatherData(weatherApiResonse);
                LocalDataRepository.InsertHomePageWeatherDetails(weatherAPIResponseDetails);
                LocalDataRepository.UpsertStoredWeatherData(weatherApiResonse);
            });
        }
Example #7
0
        private async Task <Tuple <WeatherApiResonseData, OneCallWeatherAPIResponseData> > GetLocation()
        {
            IGeolocator locator = CrossGeolocator.Current;

            locator.DesiredAccuracy = 50;

            Position positions = await locator.GetPositionAsync(timeout : new TimeSpan(10000));

            IEnumerable <Address> address = await locator.GetAddressesForPositionAsync(positions);

            WeatherApiResonseData weather = await OpenWeatherService.GetCurrentWeatherByCityNameAsync(address.FirstOrDefault().Locality);

            OneCallWeatherAPIResponseData weatherDetails = await OpenWeatherService.GetOneCallAPIRequestAsync(weather.coord.lat, weather.coord.lon);

            return(Tuple.Create(weather, weatherDetails));
        }
        async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
        {
            WeatherApiResonseData weatherApiResponse = args.SelectedItem as WeatherApiResonseData;

            if (weatherApiResponse == null)
            {
                return;
            }

            await Navigation.PopModalAsync();

            MessagingCenter.Send(weatherApiResponse, "LoadData");

            // Manually deselect item.
            ItemsListView.SelectedItem = null;
        }
        public void UpsertStoredWeatherData(WeatherApiResonseData weatherApiResonseData)
        {
            ObservableCollection <WeatherApiResonseData> storedWeatherData      = App.StoredWeatherApiResponseData;
            ObservableCollection <WeatherApiResonseData> localstoredWeatherData = App.HistoryLocalWeatherData;

            WeatherApiResonseData existingLocalWeatherData  = localstoredWeatherData.FirstOrDefault(s => s.name == weatherApiResonseData.name);
            WeatherApiResonseData existingStoredWeatherData = storedWeatherData.FirstOrDefault(s => s.name == weatherApiResonseData.name);

            if (existingLocalWeatherData != null && existingStoredWeatherData != null)
            {
                storedWeatherData.Remove(existingStoredWeatherData);
                localstoredWeatherData.Remove(existingLocalWeatherData);
            }

            storedWeatherData.Add(weatherApiResonseData);
            localstoredWeatherData.Add(weatherApiResonseData);

            App.StoredWeatherApiResponseData = storedWeatherData;
            App.HistoryLocalWeatherData      = localstoredWeatherData;
        }
Example #10
0
        public App()
        {
            InitializeComponent();

            DependencyService.Register <OpenWeatherServices>();
            DependencyService.Register <LocalDataRepository>();

            CheckPermissions();

            Task.Run(() =>
            {
                if (StoredWeatherApiResponseData == null)
                {
                    StoredWeatherApiResponseData = new ObservableCollection <WeatherApiResonseData>();
                }

                if (HistoryLocalWeatherData == null)
                {
                    HistoryLocalWeatherData = new ObservableCollection <WeatherApiResonseData>();
                }
                else
                {
                    StoredWeatherApiResponseData = HistoryLocalWeatherData;
                }

                if (HomePageLocalWeatherData == null)
                {
                    HomePageLocalWeatherData = new WeatherApiResonseData();
                }

                if (HomePageLocalWeatherDetails == null)
                {
                    HomePageLocalWeatherDetails = new OneCallWeatherAPIResponseData();
                }
            });

            //MainPage = new AppShell();
            MainPage = new NavigationPage(new ItemsPage());
        }
Example #11
0
 private async Task NotifyUpdateWeatherUI(WeatherApiResonseData weatherApiResonse, OneCallWeatherAPIResponseData weatherAPIResponseDetails)
 {
     await UpdateStoredWeatherData(weatherApiResonse, weatherAPIResponseDetails);
     await ValidateListVisibility();
 }