public static async Task<List<FavoriteStopViewModel>> GetFavoritesViewModel(ITransitRepository repository, ITransitClient client, DateTimeOffset currentTime, IEnumerable<int> stopIds, LatLong? optionalUserLocation) { var staticData = JsonConvert.DeserializeObject<BusStaticData>(await repository.GetSerializedStaticDataAsync()); var favoriteStops = GetFavoriteStops(staticData, stopIds, optionalUserLocation); var scheduleTask = GetSchedule(repository, client, currentTime, favoriteStops.Select(f => f.Id)); var schedule = await scheduleTask; var result = favoriteStops.Select(favorite => ToViewModel(favorite, staticData, schedule, currentTime)) .ToList(); return result; }
private static List<FavoriteStop> GetFavoriteStops(BusStaticData staticData, IEnumerable<int> stopIds, LatLong? optionalUserLocation) { var favoriteStops = stopIds.Where(staticData.Stops.ContainsKey).Select(id => { var stop = staticData.Stops[id]; var distanceFromUser = optionalUserLocation != null ? DistanceTo(optionalUserLocation.Value.Lat, optionalUserLocation.Value.Lon, stop.Lat, stop.Long, 'M') : double.NaN; return new FavoriteStop(stop.ID, stop.Name, stop.RouteNames, distanceFromUser, isNearestStop: false); }) .ToList(); var nearestStop = optionalUserLocation != null ? staticData.Stops.Values .Aggregate((s1, s2) => DistanceTo(optionalUserLocation.Value.Lat, optionalUserLocation.Value.Lon, s1.Lat, s1.Long, 'M') < DistanceTo(optionalUserLocation.Value.Lat, optionalUserLocation.Value.Lon, s2.Lat, s2.Long, 'M') ? s1 : s2) : null; if (nearestStop != null && !favoriteStops.Any(f => f.Id == nearestStop.ID)) { var distanceFromUser = optionalUserLocation != null ? DistanceTo(optionalUserLocation.Value.Lat, optionalUserLocation.Value.Lon, nearestStop.Lat, nearestStop.Long, 'M') : double.NaN; favoriteStops.Add(new FavoriteStop(nearestStop.ID, nearestStop.Name, nearestStop.RouteNames, distanceFromUser, isNearestStop: true)); } favoriteStops.Sort((f1, f2) => f1.DistanceFromUser.CompareTo(f2.DistanceFromUser)); return favoriteStops; }