}         // end of method LoadKnownPlaces

        /// <summary>
        /// Save the new location to the user's preference file.
        /// </summary>
        private void SaveLocationPreference()
        {
            string currentLocation = null;

            // the location setting is the only one that does not get written
            // to the preferences files automatically so the user has to do this
            // explicitly but clicking OK or Apply.
            if (!cboLocation.Text.Equals(WeatherLionMain.storedPreferences.StoredPreferences.Location) &&
                cboLocation.Text.Trim().Length > 0)
            {
                // combine the city and the region as the current location
                string[] location = cboLocation.Text.ToString().Split(',');

                if (location.Length > 0)
                {
                    // countries who have a region such as a state or municipality
                    if (location.Length > 2)
                    {
                        currentLocation = location[0].Trim() + ", " + location[1].Trim();
                    }// end of if block
                    else
                    {
                        currentLocation = cboLocation.Text.ToString().Trim();
                    }// end of else block

                    // notify the widget of this update (comment out code during form testing)
                    if (!WeatherLionMain.runningWidget.preferenceUpdated.ContainsKey(
                            WeatherLionMain.CURRENT_LOCATION_PREFERENCE))
                    {
                        WeatherLionMain.runningWidget.preferenceUpdated.Add(
                            WeatherLionMain.CURRENT_LOCATION_PREFERENCE, currentLocation);
                        locationSelected = true;
                    }// end of if block

                    if (WeatherLionMain.runningWidget != null)
                    {
                        if (!WeatherLionMain.runningWidget.Visible)
                        {
                            WeatherLionMain.runningWidget.Visible = true;
                        } // end of if block
                    }     // end of if block
                }         // end of if block
            }             // end of if block
            else
            {
                currentLocation = cboLocation.Text;
            }// end of else block

            if (!UtilityMethod.IsFoundInJSONStorage(currentLocation))
            {
                //run an background service
                CityStorageService cs = new CityStorageService(cboLocation.SelectedIndex,
                                                               cboLocation.Text);
                cs.Run();
            } // end of if block
        }     // end of method SaveLocationPreference
예제 #2
0
        }// end of method Run

        /// <summary>
        /// Add new location locally if it does not already exists
        /// </summary>
        private void StoreNewLocationLocally()
        {
            string[] searchCity = city.Split(',');

            foreach (GeoNamesGeoLocation.GeoNames foundCity in GeoNamesGeoLocation.cityGeographicalData
                     .geonames)
            {
                if (foundCity.name.Equals(searchCity[0].Trim().ToLower(), StringComparison.OrdinalIgnoreCase) &&
                    foundCity.adminCodes1.iso.Equals(searchCity[1].Trim(), StringComparison.OrdinalIgnoreCase) &&
                    !UtilityMethod.IsNumeric(foundCity.adminCodes1.iso) ||
                    foundCity.countryName.Equals(searchCity[1].Trim(), StringComparison.OrdinalIgnoreCase))
                {
                    string cityName    = UtilityMethod.ToProperCase(foundCity.name);
                    string countryName = UtilityMethod.ToProperCase(foundCity.countryName);
                    string countryCode = foundCity.countryCode.ToUpper();
                    string regionName  = UtilityMethod
                                         .ToProperCase(foundCity.adminName1);

                    string regionCode = null;
                    regionCode = foundCity.adminCodes1.iso?.ToUpper();

                    float Latitude  = float.Parse(foundCity.lat);
                    float Longitude = float.Parse(foundCity.lng);

                    CityData cityData = new CityData(cityName, countryName, countryCode,
                                                     regionName, regionCode, Latitude, Longitude);

                    string currentCity = regionCode != null ? $"{cityName}, {regionCode}" : $"{cityName}, {countryName}";

                    if (!UtilityMethod.IsFoundInDatabase(currentCity))
                    {
                        UtilityMethod.AddCityToDatabase(cityName, countryName, countryCode,
                                                        regionName, regionCode, Latitude, Longitude);
                    }// end of if block

                    if (!UtilityMethod.IsFoundInJSONStorage(currentCity))
                    {
                        JSONHelper.ExportToJSON(cityData);
                    }// end of if block

                    if (!UtilityMethod.IsFoundInXMLStorage(currentCity))
                    {
                        XMLHelper.ExportToXML(cityData);
                    } // end of if block
                }     // end of if block
            }         // end of for each loop
        }             // end of method StoreNewLocationLocally