private async void OnSearchButtonClicked(string text)
        {
            searchBar.ResignFirstResponder();
            SearchCityIndicator.StartAnimating();

            try
            {
                var geocoder       = new CLGeocoder();
                var placemarkArray = await geocoder.GeocodeAddressAsync(searchBar.Text);

                if (placemarkArray.Length > 0)
                {
                    var firstPosition     = placemarkArray[0];
                    var locationLatitude  = firstPosition.Location.Coordinate.Latitude;
                    var locationLongitude = firstPosition.Location.Coordinate.Longitude;
                    var locationName      = firstPosition.Name;
                    if (!string.IsNullOrEmpty(locationName))
                    {
                        if (UserSettings.IsFavoriteAlreadyInList(locationName))
                        {
                            //Display Alert
                            var okAlertController = UIAlertController.Create("City Name",
                                                                             "is already in List.", UIAlertControllerStyle.Alert);
                            okAlertController.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                            PresentViewController(okAlertController, true, null);
                        }
                        else
                        {
                            var loc = new Location
                            {
                                name      = locationName,
                                latitude  = locationLatitude,
                                longitude = locationLongitude
                            };

                            InvokeOnMainThread(() =>
                            {
                                favorites.Add(loc);
                                UserSettings.SaveFavorites(favorites);

                                // update table view
                                AddLocationTableView.Source = new AddLocationTableViewSource(favorites, this);
                                AddLocationTableView.ReloadData();
                            });
                        }
                    }
                    searchBar.Text        = "";
                    searchBar.Placeholder = "Enter a City";
                }
            }
            catch (Exception ex)
            {
                //Display Alert
                var okAlertController = UIAlertController.Create("Could not find location",
                                                                 "Please try again", UIAlertControllerStyle.Alert);
                okAlertController.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                PresentViewController(okAlertController, true, null);
            }
            SearchCityIndicator.StopAnimating();
        }
Exemplo n.º 2
0
        void ReleaseDesignerOutlets()
        {
            if (AddLocationTableView != null)
            {
                AddLocationTableView.Dispose();
                AddLocationTableView = null;
            }

            if (searchBar != null)
            {
                searchBar.Dispose();
                searchBar = null;
            }

            if (SearchCityIndicator != null)
            {
                SearchCityIndicator.Dispose();
                SearchCityIndicator = null;
            }
        }