//Main thing public RtStationData[] SearchStations(string SearchQuery, int MaxResults, RtGPS SortDistance, bool NavigableOnly) { //Create empty return variable List <RtStationData> StationResults = new List <RtStationData>(); //Check input parameter is valid if (SearchQuery.Length != ZEROLENGTH) { //Check if input is three capital letters if (SearchQuery.Length == THREELENGTH && SearchQuery.ToUpper() == SearchQuery) { //Search by CRS Code StationResults = RtStations.SearchByCRS(SearchQuery).ToList(); } else { //Search by Name StationResults = RtStations.SearchByName(SearchQuery, MaxResults).ToList(); } } //If Navigable Only, filter if (NavigableOnly) { //Loop through results, if not navigable, remove and minus iterator for (int i = 0; i < StationResults.Count; i++) { if (!NAVIGABLESTATIONS.Contains(StationResults[i].Code)) { StationResults.Remove(StationResults[i]); i--; } } } if (SortDistance != null) { //Create GPS Instance in another thread. for (int z = 0; z < StationResults.Count - 1; z++) { for (int i = 0; i < StationResults.Count - 1; i++) { if (SortDistance.DistanceFromLatLonInKm(StationResults[i + 1].Latitude, StationResults[i + 1].Longitude, SortDistance.Latitude, SortDistance.Longitude) < SortDistance.DistanceFromLatLonInKm(StationResults[i].Latitude, StationResults[i].Longitude, SortDistance.Latitude, SortDistance.Longitude)) { RtStationData tmp = StationResults[i]; StationResults[i] = StationResults[i + 1]; StationResults[i + 1] = tmp; } } } } //SearchQuery Invalid return(StationResults.ToArray()); }
//Creates the view private void GenerateView() { //Dialog Shaded Back _RootView = new LinearLayout(this.Context); _RootView.LayoutParameters = RtGraphicsLayouts.LayoutParameters(RtGraphicsLayouts.EXPAND, RtGraphicsLayouts.EXPAND); _RootView.SetDpPadding(RtGraphicsLayouts, DIALOGPADDING, DIALOGPADDING, DIALOGPADDING, DIALOGPADDING); _RootView.SetBackgroundColor(RtGraphicsColours.SemiTransparentBlack); _RootView.Visibility = ViewStates.Gone; _RootView.SetGravity(GravityFlags.Center); //Dialog Back LinearLayout DialogLayout = new LinearLayout(this.Context); DialogLayout.LayoutParameters = RtGraphicsLayouts.LayoutParameters(RtGraphicsLayouts.EXPAND, RtGraphicsLayouts.EXPAND); DialogLayout.Orientation = Orientation.Vertical; DialogLayout.SetBackgroundResource(Resource.Drawable.StyleCornerBox); _RootView.AddView(DialogLayout); //Dialog Top Bar LinearLayout DialogTopBar = new LinearLayout(this.Context); DialogTopBar.LayoutParameters = RtGraphicsLayouts.LayoutParameters(RtGraphicsLayouts.EXPAND, DIALOGHORIZONTALSHEIGHT); DialogTopBar.SetDpPadding(RtGraphicsLayouts, ZERO, SMALLPADDING, ZERO, SMALLPADDING); DialogLayout.AddView(DialogTopBar); //Location Icon ImageView DialogLocation = new ImageView(this.Context); DialogLocation.LayoutParameters = RtGraphicsLayouts.LayoutParameters(ICONSWIDTH, ICONSHEIGHT); DialogLocation.SetImageResource((Option_SearchLocation) ? Resource.Drawable.Icon_Location : Resource.Drawable.Icon_LocationFaded); DialogTopBar.AddView(DialogLocation); //Navigable Icon ImageView DialogNavigatable = new ImageView(this.Context); DialogNavigatable.LayoutParameters = RtGraphicsLayouts.LayoutParameters(ICONSWIDTH, ICONSHEIGHT); DialogNavigatable.SetImageResource((Option_SearchNavigable) ? Resource.Drawable.Icon_Navigation : Resource.Drawable.Icon_NavigationFaded); DialogTopBar.AddView(DialogNavigatable); //Cancel Icon ImageView DialogCancel = new ImageView(this.Context); DialogCancel.LayoutParameters = RtGraphicsLayouts.LayoutParameters(ICONSWIDTH, ICONSHEIGHT); DialogCancel.SetImageResource(Resource.Drawable.IconCancel); DialogTopBar.AddView(DialogCancel); //Search Input SearchInput = new EditText(this.Context); SearchInput.LayoutParameters = RtGraphicsLayouts.LayoutParameters(RtGraphicsLayouts.EXPAND, DIALOGHORIZONTALSHEIGHT); SearchInput.SetDpPadding(RtGraphicsLayouts, DIALOGPADDING, SMALLPADDING, DIALOGPADDING, SMALLPADDING); SearchInput.SetBackgroundColor(RtGraphicsColours.Orange); SearchInput.Format(RtGraphicsExt.TextFormats.Heading); DialogLayout.AddView(SearchInput); //Results Scroller ScrollView ResultsScroll = new ScrollView(this.Context); DialogLayout.AddView(ResultsScroll); //Results Root LinearLayout ResultsRoot = new LinearLayout(this.Context); ResultsRoot.Orientation = Orientation.Vertical; ResultsScroll.AddView(ResultsRoot); //GPS Instance RtGPS GPS = new RtGPS((LocationManager)ContextWrapper.GetSystemService(ContextWrapper.LocationService)); //Event Handlers _RootView.Click += delegate { CloseDialog(); }; DialogCancel.Click += delegate { CloseDialog(); }; SearchInput.AfterTextChanged += delegate { DrawResults(ResultsRoot, GPS); }; DialogLocation.Click += delegate { ToggleLocationOption(DialogLocation); }; DialogNavigatable.Click += delegate { ToggleNavigableOption(DialogNavigatable); }; }
//Draw results private void DrawResults(LinearLayout ResultsRoot, RtGPS GPS) { //If GPS sending position if ((GPS.Ready && Option_SearchLocation) || !Option_SearchLocation) { //Search stations RtStationData[] data = new RtStations().SearchStations(SearchInput.Text, MAXSEARCHRESULTS, (Option_SearchLocation) ? GPS : null, Option_SearchNavigable); //Clear Results View ResultsRoot.RemoveAllViews(); //Loop through stations data for (int i = 0; i < data.Length; i++) { //Create a local copy of variable for click event RtStationData localdat = data[i]; //Result Back LinearLayout ResultBack = new LinearLayout(this.Context); ResultBack.LayoutParameters = RtGraphicsLayouts.LayoutParameters(RtGraphicsLayouts.EXPAND, DIALOGRESULTHEIGHT); ResultBack.SetDpPadding(RtGraphicsLayouts, SMALLPADDING, SMALLPADDING, SMALLPADDING, SMALLPADDING); ResultBack.SetGravity(GravityFlags.CenterVertical); ResultsRoot.AddView(ResultBack); //Result Station Name TextView ResultStationName = new TextView(this.Context); ResultStationName.Format(RtGraphicsExt.TextFormats.Paragraph); ResultStationName.Text = data[i].StationName; ResultBack.AddView(ResultStationName); //Result Station CRS TextView ResultCRS = new TextView(this.Context); ResultCRS.LayoutParameters = RtGraphicsLayouts.LayoutParameters(RtGraphicsLayouts.EXPAND, RtGraphicsLayouts.CONTAIN); ResultCRS.SetDpPadding(RtGraphicsLayouts, ZERO, ZERO, SMALLPADDING, ZERO); ResultCRS.Format(RtGraphicsExt.TextFormats.Paragraph1); ResultCRS.Text = data[i].Code; ResultCRS.Gravity = GravityFlags.Right; ResultBack.AddView(ResultCRS); //Result Back Event ResultBack.Click += delegate { //If callback isn't null, invoke it, causing sleection to be returned and dialog hidden. StationSelected?.Invoke(DialogID, localdat); CloseDialog(); }; } //If theres no stations if (data.Length == ZERO) { ResultsRoot.RemoveAllViews(); ResultsRoot.AddView(DrawErrorMessage(ERROR_NOSTATIONSFOUND)); GPS = new RtGPS((LocationManager)ContextWrapper.GetSystemService(ContextWrapper.LocationService)); } } else if (GPS.NoGPS) { //If no GPS ResultsRoot.RemoveAllViews(); ResultsRoot.AddView(DrawErrorMessage(ERROR_GPSTURNEDOFF)); GPS = new RtGPS((LocationManager)ContextWrapper.GetSystemService(ContextWrapper.LocationService)); } else { //If GPS not ready ResultsRoot.RemoveAllViews(); ResultsRoot.AddView(DrawErrorMessage(ERROR_GPSLOADING)); } }