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();
            });
        }
        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));
        }
        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;
            }
        }
        public void InsertHomePageWeatherDetails(OneCallWeatherAPIResponseData callWeatherAPIResponseData)
        {
            OneCallWeatherAPIResponseData _homePageLocalWeatherDetails = App.HomePageLocalWeatherDetails;

            _homePageLocalWeatherDetails    = callWeatherAPIResponseData;
            App.HomePageLocalWeatherDetails = _homePageLocalWeatherDetails;
        }
        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);
            });
        }
        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));
        }
Exemple #7
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());
        }
 private async Task NotifyUpdateWeatherUI(WeatherApiResonseData weatherApiResonse, OneCallWeatherAPIResponseData weatherAPIResponseDetails)
 {
     await UpdateStoredWeatherData(weatherApiResonse, weatherAPIResponseDetails);
     await ValidateListVisibility();
 }