コード例 #1
0
ファイル: TripFormViewModel.cs プロジェクト: pingzing/trippit
        private async void PlanTrip()
        {
            if (_cts != null && !_cts.IsCancellationRequested)
            {
                _cts.Cancel();
            }
            _cts = new CancellationTokenSource();
            _cts.Token.ThrowIfCancellationRequested();

            try
            {
                SetBusy(true, AppResources.TripFrom_GettingsAddresses);

                List <IPlace> searchPlaces = new List <IPlace> {
                    FromPlace
                };
                if (IntermediatePlaces.Count > 0)
                {
                    searchPlaces.AddRange(IntermediatePlaces.Select(x => x.IntermediatePlace));
                }
                searchPlaces.Add(ToPlace);
                IEnumerable <PlaceResolutionResult> placeResolutionResults = await ResolvePlaces(searchPlaces, _cts.Token);

                if (placeResolutionResults.Any(x => x.IsFailure))
                {
                    await HandlePlaceResolutionFailure(placeResolutionResults.Where(x => x.IsFailure));

                    SetBusy(false);
                    return;
                }
                IEnumerable <IPlace> places = placeResolutionResults.Select(x => x.AttemptedResolvedPlace);

                FromPlace = places.First();
                if (IntermediatePlaces.Any())
                {
                    int intermediateIndex = 0;
                    foreach (IPlace intermediate in places.Skip(1))
                    {
                        if (intermediate == places.Last())
                        {
                            continue;
                        }
                        IntermediatePlaces[intermediateIndex].IntermediatePlace = intermediate;
                    }
                }
                ToPlace = places.Last();

                ApiCoordinates fromCoords = new ApiCoordinates {
                    Lat = (float)FromPlace.Lat, Lon = (float)FromPlace.Lon
                };
                List <ApiCoordinates> intermediateCoords = IntermediatePlaces
                                                           .Select(x => new ApiCoordinates {
                    Lat = (float)x.IntermediatePlace.Lat, Lon = (float)x.IntermediatePlace.Lon
                })
                                                           .ToList();
                ApiCoordinates toCoords = new ApiCoordinates {
                    Lat = (float)ToPlace.Lat, Lon = (float)ToPlace.Lon
                };
                TripQueryDetails details = new TripQueryDetails(
                    fromCoords,
                    FromPlace.Name,
                    intermediateCoords,
                    toCoords,
                    ToPlace.Name,
                    IsUsingCurrentTime ? DateTime.Now.TimeOfDay : SelectedTime,
                    IsUsingCurrentDate ? DateTime.Today : SelectedDate.DateTime,
                    IsArrivalChecked == true,
                    ConstructTransitModes((bool)IsBusChecked, (bool)IsTramChecked, (bool)IsTrainChecked,
                                          (bool)IsMetroChecked, (bool)IsFerryChecked, (bool)IsBikeChecked),
                    SelectedWalkingAmount,
                    SelectedWalkingSpeed
                    );

                Views.Busy.SetBusyText(AppResources.TripForm_PlanningTrip);

                var result = await _networkService.PlanTripAsync(details, _cts.Token);

                if (result.IsFailure)
                {
                    await HandleTripPlanningFailure(result);

                    SetBusy(false);
                    return;
                }

                if (!BootStrapper.Current.SessionState.ContainsKey(NavParamKeys.PlanResults))
                {
                    BootStrapper.Current.SessionState.Add(NavParamKeys.PlanResults, result.Result);
                }
                else
                {
                    BootStrapper.Current.SessionState.Remove(NavParamKeys.PlanResults);
                    BootStrapper.Current.SessionState.Add(NavParamKeys.PlanResults, result.Result);
                }

                _messengerService.Send(new MessageTypes.PlanFoundMessage(CurrentVisualState));
            }
            catch (OperationCanceledException ex)
            {
                //Log and swallow. Cancellation should only happen on user request here.
                System.Diagnostics.Debug.WriteLine("User cancelled place lookup.");
            }
            finally
            {
                SetBusy(false);
            }
        }
コード例 #2
0
        /// <summary>
        /// Returns a travel plan.
        /// </summary>
        /// <param name="details"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <ApiResult <TripPlan> > PlanTripAsync(TripQueryDetails details, CancellationToken token = default(CancellationToken))
        {
            Uri uri = new Uri(DefaultGqlRequestUrl);

            GqlQuery query = new GqlQuery(ApiGqlMembers.plan)
                             .WithParameters(
                new GqlParameter(ApiGqlMembers.from, new GqlTuple {
                { ApiGqlMembers.lat, details.FromPlaceCoords.Lat }, { ApiGqlMembers.lon, details.FromPlaceCoords.Lon }
            }),
                new GqlParameter(ApiGqlMembers.intermediatePlaces, new GqlParameterArray(details.IntermediateCoords.Select(x => new GqlTuple {
                { ApiGqlMembers.lat, x.Lat }, { ApiGqlMembers.lon, x.Lon }
            }))),
                new GqlParameter(ApiGqlMembers.to, new GqlTuple {
                { ApiGqlMembers.lat, details.ToPlaceCoordinates.Lat }, { ApiGqlMembers.lon, details.ToPlaceCoordinates.Lon }
            }),
                new GqlParameter(ApiGqlMembers.numItineraries, 5),
                new GqlParameter(ApiGqlMembers.time, details.Time),
                new GqlParameter(ApiGqlMembers.date, details.Date),
                new GqlParameter(ApiGqlMembers.arriveBy, details.IsTimeTypeArrival),
                new GqlParameter(ApiGqlMembers.modes, details.TransitModes),
                new GqlParameter(ApiGqlMembers.walkSpeed, details.WalkSpeed.UnderlyingMetersPerSecond),
                new GqlParameter(ApiGqlMembers.walkReluctance, details.WalkAmount.UnderlyingWalkReluctance)
                )
                             .WithReturnValues(
                new GqlReturnValue(ApiGqlMembers.from,
                                   new GqlReturnValue(ApiGqlMembers.name)
                                   ),
                new GqlReturnValue(ApiGqlMembers.to,
                                   new GqlReturnValue(ApiGqlMembers.name)
                                   ),
                new GqlReturnValue(ApiGqlMembers.itineraries,
                                   new GqlReturnValue(ApiGqlMembers.legs,
                                                      new GqlReturnValue(ApiGqlMembers.startTime),
                                                      new GqlReturnValue(ApiGqlMembers.endTime),
                                                      new GqlReturnValue(ApiGqlMembers.mode),
                                                      new GqlReturnValue(ApiGqlMembers.duration),
                                                      new GqlReturnValue(ApiGqlMembers.realTime),
                                                      new GqlReturnValue(ApiGqlMembers.distance),
                                                      new GqlReturnValue(ApiGqlMembers.transitLeg),
                                                      new GqlReturnValue(ApiGqlMembers.legGeometry,
                                                                         new GqlReturnValue(ApiGqlMembers.length),
                                                                         new GqlReturnValue(ApiGqlMembers.points)
                                                                         ),
                                                      new GqlReturnValue(ApiGqlMembers.intermediateStops,
                                                                         new GqlReturnValue(ApiGqlMembers.name),
                                                                         new GqlReturnValue(ApiGqlMembers.lat),
                                                                         new GqlReturnValue(ApiGqlMembers.lon)
                                                                         ),
                                                      new GqlReturnValue(ApiGqlMembers.from,
                                                                         new GqlReturnValue(ApiGqlMembers.name),
                                                                         new GqlReturnValue(ApiGqlMembers.lat),
                                                                         new GqlReturnValue(ApiGqlMembers.lon)
                                                                         ),
                                                      new GqlReturnValue(ApiGqlMembers.to,
                                                                         new GqlReturnValue(ApiGqlMembers.name),
                                                                         new GqlReturnValue(ApiGqlMembers.lat),
                                                                         new GqlReturnValue(ApiGqlMembers.lon)
                                                                         ),
                                                      new GqlReturnValue(ApiGqlMembers.route,
                                                                         new GqlReturnValue(ApiGqlMembers.gtfsId),
                                                                         new GqlReturnValue(ApiGqlMembers.shortName)
                                                                         )
                                                      ),
                                   new GqlReturnValue(ApiGqlMembers.fares,
                                                      new GqlReturnValue(ApiGqlMembers.type),
                                                      new GqlReturnValue(ApiGqlMembers.currency),
                                                      new GqlReturnValue(ApiGqlMembers.cents)
                                                      )
                                   )
                );

            var response = await GetGraphQLAsync <ApiPlan>(query, token);

            if (!response.HasResult)
            {
                return(ApiResult <TripPlan> .FailWithReason(response.Failure.Reason));
            }
            if (response.HasResult && response.Result?.Itineraries.Any() != true)
            {
                LogLogicFailure(FailureReason.NoResults);
                return(ApiResult <TripPlan> .FailWithReason(FailureReason.NoResults));
            }

            return(new ApiResult <TripPlan>(new TripPlan(response.Result, details.FromPlaceString, details.ToPlaceString)));
        }