// The project requires the Google Glass Component from // https://components.xamarin.com/view/googleglass // so make sure you add that in to compile succesfully. protected override async void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Show loading screen SetContentView(Resource.Layout.LoadingScreen); var loadingText = FindViewById <TextView> (Resource.Id.loading_text); loadingText.SetText(Resource.String.getting_departures); // set loading text var progressBar = FindViewById <SliderView> (Resource.Id.indeterm_slider); progressBar.StartIndeterminate(); // start indeterminate progress bar // get station ID from intent int intentStationId = int.Parse(Intent.GetStringExtra("stationId")); // try to call PTV API to get station departures List <Departure> stationDepartures; try{ var ptvApi = new PtvApi(); stationDepartures = await ptvApi.StationDepartures(intentStationId, TransportType.Train, 1); }catch (Exception e) { // show error card var errorCard = new Card(this); errorCard.SetText(e.ToString()); SetContentView(errorCard.View); return; } // if there are no departures, show no departure message if (stationDepartures.Count == 0) { // Show error screen SetContentView(Resource.Layout.ErrorScreen); var errorText = FindViewById <TextView> (Resource.Id.error_text); errorText.SetText(Resource.String.no_upcoming_departures); // set error text return; } // show departures list screen ListView listView; SetContentView(Resource.Layout.DepartureScreen); listView = FindViewById <ListView> (Resource.Id.listview); listView.Adapter = new TrainsScreenAdapter(this, stationDepartures); // bind list of station departures to listView listView.RequestFocus(); // set focus on the listView so scrolling works on the list }
public async void NearbyDepartures(Location location) { var ptvApi = new PtvApi(); // new PTV API service // Show loading screen SetContentView(Resource.Layout.LoadingScreen); var loadingText = FindViewById <TextView>(Resource.Id.loading_text); loadingText.SetText(Resource.String.getting_stopsnearyou); // set loading text var progressBar = FindViewById <SliderView>(Resource.Id.indeterm_slider); progressBar.StartIndeterminate(); // start indeterminate progress bar // try to call PTV API to get nearby stops List <Stop> stopsNearby; try { stopsNearby = await ptvApi.StopsNearby(location.Latitude, location.Longitude); } catch (Exception e) { // show error card var errorCard = new Card(this); errorCard.SetText(e.ToString()); SetContentView(errorCard.View); return; } // depending on our mode of transport, we want different number of stops and error messages int stopLimit = 1; int noStopsNearby = Resource.String.no_stops_nearby; switch (transportType) { case TransportType.Bus: stopLimit = 6; noStopsNearby = Resource.String.no_bus_stops_nearby; break; case TransportType.Tram: stopLimit = 6; noStopsNearby = Resource.String.no_tram_stops_nearby; break; case TransportType.Train: stopLimit = 1; noStopsNearby = Resource.String.no_train_stops_nearby; break; } // Cull our stops nearby to just how many we want stopsNearby = stopsNearby.Where(x => x.TransportType == transportType ).Take(stopLimit).ToList(); // if there are no stops nearby, show no stops message if (stopsNearby.Count == 0) { // Show error screen SetContentView(Resource.Layout.ErrorScreen); var errorText = FindViewById <TextView>(Resource.Id.error_text); errorText.SetText(noStopsNearby); // set error text return; } // Update loading text loadingText.SetText(Resource.String.getting_departures); // Get departures for each stop List <Departure> nearByDepartures = new List <Departure>(); foreach (Stop stop in stopsNearby) { nearByDepartures.AddRange(await ptvApi.StationDepartures(stop.StopID, transportType, 1)); // merge departures together } // if there are no departures, show no departure message if (nearByDepartures.Count == 0) { var noDeparturesCard = new Card(this); noDeparturesCard.SetText(Resource.String.no_upcoming_departures); SetContentView(noDeparturesCard.View); return; } // show departures list screen SetContentView(Resource.Layout.DepartureScreen); headListView = FindViewById <HeadListView>(Resource.Id.listview); // get the right type of screen adapter for the right type of transport if (transportType == TransportType.Train) { // we don't need the train "number" for nearby trains headListView.Adapter = new NearbyTrainScreenAdapter(this, nearByDepartures); // bind list of station departures to listView } else { headListView.Adapter = new NearbyBusTramScreenAdapter(this, nearByDepartures); // bind list of station departures to listView } headListView.activate(); }