Exemplo n.º 1
0
        public async Task CleanupViewModel()
        {
            await VM.Destroy();

            VM = null;

            geolocationServiceMock.Reset();
            weatherApiMock.Reset();
        }
        public WeatherWidget()
        {
            VM = new WeatherWidgetViewModel();
            VM.Initialize();

            DataContext = VM;

            InitializeComponent();
        }
        public ActionResult Index()
        {
            WeatherWidgetProperties properties = GetProperties();
            var model = new WeatherWidgetViewModel();


            if (String.IsNullOrEmpty(properties.City))
            {
                model.IsCitySelected = false;
                model.Error_msg      = "Please select the City";
            }
            else
            {
                try
                {
                    model.IsCitySelected = true;
                    model.Units          = properties.Units;
                    model.City           = properties.City;
                    string URL = String.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&units={1}&APPID={2}", model.City, model.Units, API_ID);


                    var client = new WebClient();
                    client.Encoding = Encoding.UTF8;
                    string jsonData = client.DownloadString(URL);

                    WeatherResponse response = JsonConvert.DeserializeObject <WeatherResponse>(jsonData);

                    model.City         = response.name;
                    model.Temp         = (int)Math.Round(response.main.temp);
                    model.Pressure     = "Pressure: " + response.main.pressure.ToString() + " kPa";
                    model.Humidity     = "Humidity: " + response.main.humidity.ToString() + " %";
                    model.Temp_max     = (int)Math.Round(response.main.temp_max);
                    model.Temp_min     = (int)Math.Round(response.main.temp_min);
                    model.Weather_Icon = response.weather[0].icon + ".png";

                    // private method -> Gets unitType string and sets model to (°C / °K / °F)
                    model.Unit_Type = Get_Unit_Type(properties.Units);
                }
                catch (Exception e) // in case of wrong input (non-existing city)
                {
                    model.IsCitySelected = false;
                    model.Error_msg      = e.Message;
                }
            }


            return(PartialView("Widgets/_WeatherWidget", model));
        }
Exemplo n.º 4
0
        public async Task SetupViewModel()
        {
            BasicGeoposition mockPosition = new BasicGeoposition();

            mockPosition.Latitude  = 0;
            mockPosition.Longitude = 0;

            WeatherForecast mockForecast = new WeatherForecast();

            #region mockForecastSetup
            ForecastEntry        forecastEntry        = new ForecastEntry();
            ForecastEntryDetails forecastEntryDetails = new ForecastEntryDetails();
            forecastEntryDetails.description = "test";
            forecastEntryDetails.main        = "test";
            List <ForecastEntryDetails> detailsList = new List <ForecastEntryDetails>()
            {
                forecastEntryDetails
            };
            forecastEntry.weather = detailsList;
            forecastEntry.dt_txt  = "10-10-2010 10:10:10";
            ForecastEntryMain forecastEntryMain = new ForecastEntryMain();
            forecastEntryMain.temp = 0;
            forecastEntry.main     = forecastEntryMain;
            mockForecast.list      = new List <ForecastEntry>()
            {
                forecastEntry
            };
            #endregion

            geolocationServiceMock = new Mock <IGeolocationService>();
            geolocationServiceMock.Setup(geolocationService => geolocationService.RequestGeolocationPermission()).Returns(Task.CompletedTask);
            geolocationServiceMock.Setup(geolocationService => geolocationService.GetCurrentCoordinates()).Returns(Task.FromResult(mockPosition));

            weatherApiMock = new Mock <IWeatherApi>();
            weatherApiMock.Setup(weatherApi => weatherApi.LoadWeather(0, 0)).Returns(Task.FromResult(mockForecast));

            VM = new WeatherWidgetViewModel(geolocationServiceMock.Object, weatherApiMock.Object);
            await VM.Initialize();
        }
Exemplo n.º 5
0
        // GET: WeatherWidget
        public ActionResult Index()
        {
            WeatherWidgetProperties properties = GetProperties();

            string location = "Error retrieving location.";
            string zipCode  = "Error retrieving zip code.";
            string format;

            //Queries for the cafe that matches the selected Cafe property value.
            DocumentQuery cafes = DocumentHelper.GetDocuments("DancingGoatMvc.Cafe")
                                  .Path("/Cafes/", PathTypeEnum.Children)
                                  .OnSite("DancingGoatMvc")
                                  .Culture("en-us")
                                  .Where("DocumentName", QueryOperator.Equals, properties.Cafe)
                                  .TopN(1);

            //Pulls city, state, country, and zip code values from query result.
            //Preps Zip Code for weather API call.
            foreach (TreeNode cafe in cafes)
            {
                string city    = cafe.GetValue <string>("CafeCity", "City not available");
                string country = cafe.GetValue <string>("CafeCountry", "Country not available");
                zipCode = cafe.GetValue <string>("CafeZipCode", "Zip code not available");

                //Location value varies if in the USA or not.
                if (country.Contains("USA"))
                {
                    string state = country.Substring(4);
                    location = city + ", " + state + ", USA";
                }
                else
                {
                    location = city + ", " + country;
                }
            }

            //Prepares property DegreeUnit for API call.
            switch (properties.DegreeUnit)
            {
            case "F":
                format = "imperial";
                break;

            case "C":
                format = "metric";
                break;

            case "K":
                format = "default";
                break;

            default:
                format = "imperial";
                break;
            }



            //Constructs part of the View Model
            var model = new WeatherWidgetViewModel
            {
                DegreeUnit = properties.DegreeUnit,
                Cafe       = properties.Cafe,
                Location   = location,
                ZipCode    = zipCode
            };

            try
            {
                string json;

                using (WebClient wc = new WebClient())
                {
                    json = wc.DownloadString(String.Format(API_URL, zipCode, format));
                }

                var data = JsonConvert.DeserializeObject <WeatherResponse>(json);

                model.Temperature = (int)Math.Round(data.main.temp);
            }
            catch (Exception ex)
            {
                model.ErrorMessage = "Could not load weather data! Did you enter a valid city name? Details:<br/>" + ex.Message;
            }

            return(PartialView("Widgets/_WeatherWidget", model));
        }