Пример #1
0
 async Task CheckAppConfiguration()
 {
     //try to get configuration
     configuration = appServices.GetConfiguration();
     // init configuration if it's empty ( use default api key & user current location etc ..)
     if (string.IsNullOrEmpty(configuration.APIKey))
     {
         await appServices.InitConfiguration(configuration);
     }
     //run synchronization if exist
     if (configuration.synchronization)
     {
         refreshTimer = new Timer(async(e) =>
         {
             if (canRefresh)
             {
                 await vm.BuildCompleteViewModel();
             }
             canRefresh = true;
         }, null, 0, (int.Parse(configuration.duration)) * 60 * 1000);
     }
     // run animation id exist
     if (configuration.runAnimation)
     {
         animationTimer = new Timer((e) =>
         {
             UIServices.ScaleImage(weatherImage);
         }, null, 0, AppConstants.AnimationDuration);
     }
 }
Пример #2
0
 public ConfigurationPage()
 {
     InitializeComponent();
     appServices        = (Application.Current as App).AppServices;
     configurationModel = appServices.GetConfiguration();
     BindingContext     = configurationModel;
 }
        /// <summary>
        /// get all weather data from the weather service ( current & forecast)
        /// map data with our view models
        /// </summary>
        public async Task BuildCompleteViewModel()
        {
            //check internet connection
            IsConnected = AppHelper.CheckInternet();
            if (IsConnected)
            {
                var configuration = appServices.GetConfiguration();
                if (string.IsNullOrEmpty(configuration.lastUserLocation))
                {
                    AlertChanged(ResourcesValues.LocationUnkonwenMessage);
                    UserDialogs.Instance.HideLoading();
                    return;
                }
                try
                {
                    //pull complete weather data
                    rootModel = await weatherService.GetRootModel(configuration.APIKey, int.Parse(configuration.numberOfDays) + 1, configuration.lastUserLocation);

                    var dailyAstro = rootModel.forecast.forecastday[0].astro;
                    // daily weather mapping
                    _dailyViewModel.textcolor       = configuration.textcolor;
                    _dailyViewModel.regionAndCity   = $"{rootModel.location.region}, {rootModel.location.name}";
                    _dailyViewModel.country         = $"{rootModel.location.country}, Today";
                    _dailyViewModel.temperature     = $"{rootModel.current.temp_c.ToString()}{AppConstants.celsius}";
                    _dailyViewModel.icon            = $"{AppConstants.httpStart}{rootModel.current.condition.icon}";
                    _dailyViewModel.condition       = rootModel.current.condition.text;
                    _dailyViewModel.feelLike        = $"Feel Like {rootModel.current.feelslike_c.ToString()}{AppConstants.celsius}";
                    _dailyViewModel.humidity        = $"{rootModel.current.humidity.ToString()} {AppConstants.persent}";
                    _dailyViewModel.minMaxTemp      = $"{rootModel.current.temp_c.ToString()} {AppConstants.celsius}";
                    _dailyViewModel.wind            = $"{rootModel.current.wind_kph.ToString()} {AppConstants.wind}";
                    _dailyViewModel.wind            = $"{rootModel.current.wind_kph.ToString()} {AppConstants.wind}";
                    _dailyViewModel.backgroundImage = AppHelper.CheckDayState(rootModel.location.localtime, dailyAstro.sunrise, dailyAstro.sunset);
                    // weekly weather mapping
                    _forecastViewModel.Clear();
                    foreach (var item in rootModel.forecast.forecastday.Skip(1))
                    {
                        _forecastViewModel.Add(new ForecastWeatherViewModel(configuration.textcolor,
                                                                            $"{item.day.mintemp_c.ToString()}{AppConstants.celsius}"
                                                                            , $"{item.day.maxtemp_c.ToString()}{AppConstants.celsius}"
                                                                            , AppHelper.GetDayOfWeek(item.date)
                                                                            , $"{AppConstants.httpStart}{item.day.condition.icon}")
                                               );
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine($"Track objecy mapping error: {ex.Message}");
                    AlertChanged(ResourcesValues.tryAgainLaterMessage);
                }
            }
            else
            {
                AlertChanged(ResourcesValues.CheckInternetMessage);
            }
        }