/// <summary> /// Sets the map view and refreshes stops around that view asynchronously. /// </summary> public async Task RefreshStopsForLocationAsync(MapView mapView) { SetProperty(ref this.mapView, mapView); MapView.Current = mapView; this.BusStops = new BusStopList(await ObaDataAccess.Create().GetStopsForLocationAsync(mapView.MapCenter.Latitude, mapView.MapCenter.Longitude)); }
/// <summary> /// Populates a stop solely on the stop ID, which we will look up from OBA. /// </summary> public async Task PopulateStopAsync(string stopId) { var dataAccess = ObaDataAccess.Create(); Stop stop = await dataAccess.GetStopDetailsAsync(stopId); await this.PopulateStopAsync(stop.Name, stopId, stop.Direction); }
/// <summary> /// Private helper method returns all routes. /// </summary> private async Task <List <Route> > LoadAllRoutesAsync() { var obaDataAccess = ObaDataAccess.Create(); var listOfAllRoutes = await obaDataAccess.GetAllRoutesForCurrentRegionAsync(); this.IsLoadingRoutes = false; return(listOfAllRoutes); }
public async Task RefreshStopsForLocationAsync() { this.BusStops = new BusStopList(await ObaDataAccess.Create().GetStopsForLocationAsync( mapView.MapCenter.Latitude, mapView.MapCenter.Longitude, mapView.BoundsHeight, mapView.BoundsWidth)); }
/// <summary> /// Refreshes the currently displayed bus stop. /// </summary> public async Task RefreshStopAsync() { if (!string.IsNullOrEmpty(this.StopId)) { var obaDataAccess = ObaDataAccess.Create(); this.RouteAndMapsViewModels = (from route in await obaDataAccess.GetRoutesForStopAsync(this.StopId) select new RouteMapsAndSchedulesControlViewModel() { StopId = this.stopId, RouteId = route.Id, RouteName = route.ShortName, StopName = this.StopHeaderText, RouteDescription = route.Description }).ToArray(); List <TrackingData> realTimeDataList = new List <TrackingData>(); realTimeDataList.AddRange(await obaDataAccess.GetTrackingDataForStopAsync(this.StopId)); // Add no data items for any routes that aren't coming at the moment: foreach (var viewModel in this.RouteAndMapsViewModels) { if (!realTimeDataList.Any(trackingData => string.Equals(trackingData.RouteId, viewModel.RouteId, StringComparison.OrdinalIgnoreCase))) { realTimeDataList.Add(new TrackingData( viewModel.StopId, viewModel.StopName, viewModel.RouteId, viewModel.RouteName, viewModel.RouteDescription)); } } if (this.isFiltered) { foreach (var data in realTimeDataList) { if (string.Equals(this.filteredRouteId, data.RouteId, StringComparison.OrdinalIgnoreCase)) { data.IsFiltered = true; } } this.RealTimeData = realTimeDataList.ToArray(); } else { this.RealTimeData = realTimeDataList.ToArray(); } foreach (var trackingData in this.RealTimeData) { trackingData.IsFavorite = await OneBusAway.Model.Favorites.IsFavoriteAsync(trackingData.StopAndRoute); } this.LastUpdated = DateTime.Now; this.ShowNoItemsMessage = this.RealTimeData.Length == 0; } }
/// <summary> /// Ask OBA for shape & route data. /// </summary> public async Task GetRouteData(string stopId, string routeId) { var obaDataAccess = ObaDataAccess.Create(); RouteData routeData = await obaDataAccess.GetRouteDataAsync(routeId, stopId); this.MapControlViewModel.BusStops = new BusStopList(routeData.Stops); this.MapControlViewModel.Shapes = routeData.Shapes.ToList(); this.MapControlViewModel.SelectStop(stopId); }
/// <summary> /// Returns a list of suggestions for the user the OBA list of routes. /// </summary> public async Task <IEnumerable <string> > GetSuggestionsAsync(string query, OneBusAway.Model.Point userLocation) { var obaDataAccess = ObaDataAccess.Create(); var listOfAllRoutes = await obaDataAccess.GetAllRoutesForCurrentRegionAsync(); return(from result in listOfAllRoutes where (result.ShortName != null && result.ShortName.IndexOf(query, StringComparison.OrdinalIgnoreCase) > -1) || (result.Description != null && result.Description.IndexOf(query, StringComparison.OrdinalIgnoreCase) > -1) select string.Format(CultureInfo.CurrentCulture, "BUS {0}", result.ShortName)); }
/// <summary> /// Gets the trip details for the tracking data. /// </summary> public async Task GetTripDetailsAsync() { var obaDataAccess = ObaDataAccess.Create(); this.TripDetails = await obaDataAccess.GetTripDetailsAsync(this.trackingData.TripId); if (!string.IsNullOrEmpty(this.selectedStopId)) { this.SelectStop(this.selectedStopId); } this.IsLoadingTripDetails = false; }
/// <summary> /// Called when the user selects a route's search result. /// </summary> private async void OnSearchResultsControlViewModelRouteSelected(object sender, RouteSelectedEventArgs e) { this.searchResultsControlViewModel.SetIsLoadingCurrentRoute(true); try { var obaDataAccess = ObaDataAccess.Create(); var routes = await obaDataAccess.GetRouteDataAsync(e.RouteId); this.MapControlViewModel.BusStops = new BusStopList(from route in routes from stop in route.Stops select stop); this.MapControlViewModel.Shapes = (from route in routes from shape in route.Shapes select shape).ToList(); } finally { this.searchResultsControlViewModel.SetIsLoadingCurrentRoute(false); } this.MapControlViewModel.ZoomToRouteShape(); }
/// <summary> /// Queries Oba for schedule data based on the current stop id. /// </summary> public async Task FindScheduleDataAsync(string stopId, string routeId) { this.stopId = stopId; this.routeId = routeId; // First we need to find the day of the week to use: DateTime date = DateTime.Now; if (this.dayOfTheWeek != (int)date.DayOfWeek) { int daysFromNow = this.dayOfTheWeek - (int)date.DayOfWeek; // If we're talking about a day in the past, roll it forward to next week: if (daysFromNow < 0) { daysFromNow += 7; } date = date.AddDays(daysFromNow); } try { var obaDataAccess = ObaDataAccess.Create(); var scheduleData = await obaDataAccess.GetScheduleForStopAndRouteAsync(stopId, routeId, date); if (scheduleData != null && scheduleData.Length > 0) { this.ScheduleAvailable = true; this.ScheduleData = this.GetDateTimesFromScheduleStopTimes(scheduleData[0].ScheduleStopTimes); this.TripHeadsign = scheduleData[0].TripHeadsign; if (scheduleData.Length == 2) { this.HasSecondTripHeadsign = true; this.SecondScheduleData = this.GetDateTimesFromScheduleStopTimes(scheduleData[1].ScheduleStopTimes); this.SecondTripHeadsign = scheduleData[1].TripHeadsign; } else { this.HasSecondTripHeadsign = false; } } else { this.ScheduleAvailable = false; this.ScheduleData = null; this.HasSecondTripHeadsign = false; } } catch (ArgumentException) { // No schedule available for this stop: this.ScheduleAvailable = false; this.ScheduleData = null; this.HasSecondTripHeadsign = false; } finally { this.IsLoadingSchedule = false; } }
/// <summary> /// Finds the shape of a route by its id and current stopId and then displays it. /// </summary> public async Task FindRouteShapeAsync(string routeId, string stopId) { var routeData = await ObaDataAccess.Create().GetRouteDataAsync(routeId, stopId); this.Shapes = routeData.Shapes.ToList(); }
public async Task PopulateFavoritesAsync() { List <TrackingData> trackingData = new List <TrackingData>(); var favs = await Model.Favorites.GetAsync(); this.ShowNoFavoritesMessage = false; this.ShowNoItemsMessage = false; this.StopHeaderText = Favorites; this.StopSubHeaderText = RealTime; var failedFavorites = new List <StopAndRoutePair>(); var obaDataAccess = ObaDataAccess.Create(); foreach (StopAndRoutePair fav in favs) { try { TrackingData[] tdataArray = (from tdData in await obaDataAccess.GetTrackingDataForStopAsync(fav.Stop) where string.Equals(fav.Route, tdData.RouteId, StringComparison.OrdinalIgnoreCase) select tdData).Take(2).ToArray(); if (tdataArray.Length > 0) { foreach (TrackingData tdata in tdataArray) { tdata.IsFiltered = (this.isFiltered && string.Equals(this.filteredRouteId, tdata.RouteId, StringComparison.OrdinalIgnoreCase)); tdata.Context = TrackingData.Favorites; tdata.IsFavorite = true; trackingData.Add(tdata); } } else { // If we don't have an active trip for this favorite, add a dummy tracking data to the // list that links to the schedule page and shows NO DATA in the mins column. TrackingData tdata = new TrackingData(fav); tdata.IsFiltered = (this.isFiltered && string.Equals(this.filteredRouteId, tdata.RouteId, StringComparison.OrdinalIgnoreCase)); tdata.Context = TrackingData.Favorites; tdata.IsFavorite = true; trackingData.Add(tdata); } } catch { // Favorite likely no longer exists: remove it from the list! // For now, let's just eat the exception failedFavorites.Add(fav); } } // If any favorites failed to load, remove them from the list: if (failedFavorites.Count > 0) { foreach (var failedFav in failedFavorites) { await Model.Favorites.RemoveAsync(failedFav); } await Model.Favorites.PersistAsync(); } this.RealTimeData = trackingData.ToArray(); this.RouteAndMapsViewModels = null; this.LastUpdated = DateTime.Now; if (favs.Count == 0) { this.ShowNoFavoritesMessage = true; } else { this.ShowNoItemsMessage = trackingData.Count == 0; } }
/// <summary> /// Runs the tile updating service. /// </summary> public static async Task UpdateTilesAsync(CancellationToken token) { // Inject the platform services into the PCL: ServiceRepository.FileService = new FileService(); ServiceRepository.GeoLocationService = new GeoLocationService(); ServiceRepository.SettingsService = new SettingsService(); try { // First update the favorites: var favorites = await Model.Favorites.GetAsync(); // Get the tracking data for favorites & filter it out by the routes: List <TrackingData> favoritesRealTimeData = new List <TrackingData>(); foreach (StopAndRoutePair favorite in favorites) { token.ThrowIfCancellationRequested(); // Get tracking data for this stop: var obaDataAccess = ObaDataAccess.Create(); TrackingData[] trackingData = await obaDataAccess.GetTrackingDataForStopAsync(favorite.Stop, token); // Adds the tracking data to the list: favoritesRealTimeData.AddRange(from data in trackingData where string.Equals(favorite.Route, data.RouteId, StringComparison.OrdinalIgnoreCase) select data); } // Now it's time to update the main tile with data: TileXMLBuilder mainTileBuilder = new TileXMLBuilder(); AppendTrackingDataToTile(mainTileBuilder, favoritesRealTimeData); // And now we can update the secondary tiles! var pinnedStopTiles = await SecondaryTile.FindAllAsync(); foreach (var pinnedStopTile in pinnedStopTiles) { token.ThrowIfCancellationRequested(); PageInitializationParameters parameters = null; // Be safe and try this first...should never happen. if (PageInitializationParameters.TryCreate(pinnedStopTile.Arguments, out parameters)) { double lat = parameters.GetParameter <double>("lat"); double lon = parameters.GetParameter <double>("lon"); string stopId = parameters.GetParameter <string>("stopId"); if (!string.IsNullOrEmpty(stopId) && lat != 0 && lon != 0) { // Get the tracking data: var obaDataAccess = ObaDataAccess.Create(lat, lon); TrackingData[] trackingData = await obaDataAccess.GetTrackingDataForStopAsync(stopId, token); TileXMLBuilder secondaryTileBuilder = new TileXMLBuilder(pinnedStopTile.TileId); await secondaryTileBuilder.AppendTileWithLargePictureAndTextAsync( pinnedStopTile.TileId, lat, lon, pinnedStopTile.DisplayName); AppendTrackingDataToTile(secondaryTileBuilder, trackingData); } } } } catch { // Sometimes OBA will fail. What can you do? } }