コード例 #1
0
        private async Task LoadFlightsList()
        {
            // Stop the timer if it's already running to prevent multiple server calls if the network's slow
            if (timer.IsEnabled)
            {
                timer.Stop();
            }

            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.IfModifiedSince = new DateTimeOffset(DateTime.UtcNow);
            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");

            HttpResponseMessage response = await httpClient.GetAsync(new Uri(url));

            string jsonString = await response.Content.ReadAsStringAsync();

            if (response.StatusCode == HttpStatusCode.NotModified)
            {
                // Nothing changed, wait for 15 seconds this time
                if (!timer.IsEnabled)
                {
                    timer.Tick    += new EventHandler <object>(timerTick);
                    timer.Interval = new TimeSpan(0, 0, 15);
                    timer.Start();
                }
            }
            else
            {
                SkyscannerResponse result = JsonConvert.DeserializeObject <SkyscannerResponse>(jsonString);
                if (result.Status.Equals("UpdatesComplete"))
                {
                    if (result.Itineraries.Length == 0)
                    {
                        DisplayErrorMessage("No flights were found for your search.");
                    }
                    else
                    {
                        agents = result.Agents;
                        DisplayFlightsList(result);
                    }
                }
                else
                {
                    // Updates aren't complete yet, try again in 10 seconds
                    if (!timer.IsEnabled)
                    {
                        timer.Tick    += new EventHandler <object>(timerTick);
                        timer.Interval = new TimeSpan(0, 0, 10);
                        timer.Start();
                    }
                }
            }
        }
コード例 #2
0
        private void DisplayFlightsList(SkyscannerResponse result)
        {
            sessionKey = result.SessionKey;
            query      = result.Query;

            foreach (Itinerary itinerary in result.Itineraries)
            {
                FlightsListItem item                = new FlightsListItem();
                int[]           outboundCarriers    = { };
                int[]           inboundCarriers     = { };
                int             outboundOrigin      = 0;
                int             outboundDestination = 0;
                int             inboundOrigin       = 0;
                int             inboundDestination  = 0;

                item.outboundId = itinerary.OutboundLegId;
                item.inboundId  = itinerary.InboundLegId;

                int remaining = 2;
                foreach (Leg leg in result.Legs)
                {
                    if (remaining == 0)
                    {
                        break;
                    }

                    if (leg.Id.Equals(itinerary.OutboundLegId))
                    {
                        DateTime departure = DateTime.Parse(leg.Departure);
                        DateTime arrival   = DateTime.Parse(leg.Arrival);
                        item.outboundFlightTime = departure.ToString("H:mm") + " - " + arrival.ToString("H:mm");
                        int daysDiff = (arrival - departure).Days;
                        if (daysDiff > 0)
                        {
                            if (daysDiff == 1)
                            {
                                item.outboundAddDays = "(+" + daysDiff + " day)";
                            }
                            else
                            {
                                item.outboundAddDays = "(+" + daysDiff + " days)";
                            }
                        }

                        TimeSpan duration = TimeSpan.FromMinutes(leg.Duration);
                        if (duration.Hours > 0)
                        {
                            item.outboundDuration = ((duration.Days * 24) + duration.Hours) + "h " + duration.Minutes + "m";
                        }
                        else
                        {
                            item.outboundDuration = duration.Minutes + "m";
                        }

                        int stops = leg.SegmentIds.Length - 1;
                        if (stops == 0)
                        {
                            item.outboundStops = "Direct Flight";
                        }
                        else if (stops == 1)
                        {
                            item.outboundStops = stops + " Stop";
                        }
                        else
                        {
                            item.outboundStops = stops + " Stops";
                        }

                        outboundCarriers    = leg.OperatingCarriers;
                        outboundOrigin      = leg.OriginStation;
                        outboundDestination = leg.DestinationStation;
                        remaining--;
                    }
                    else if (leg.Id.Equals(itinerary.InboundLegId))
                    {
                        DateTime departure = DateTime.Parse(leg.Departure);
                        DateTime arrival   = DateTime.Parse(leg.Arrival);
                        item.inboundFlightTime = departure.ToString("H:mm") + " - " + arrival.ToString("H:mm");
                        int daysDiff = (arrival - departure).Days;
                        if (daysDiff > 0)
                        {
                            if (daysDiff == 1)
                            {
                                item.inboundAddDays = "(+" + daysDiff + " day)";
                            }
                            else
                            {
                                item.inboundAddDays = "(+" + daysDiff + " days)";
                            }
                        }

                        TimeSpan duration = TimeSpan.FromMinutes(leg.Duration);
                        if (duration.Hours > 0)
                        {
                            item.inboundDuration = ((duration.Days * 24) + duration.Hours) + "h " + duration.Minutes + "m";
                        }
                        else
                        {
                            item.inboundDuration = duration.Minutes + "m";
                        }

                        int stops = leg.SegmentIds.Length - 1;
                        if (stops == 0)
                        {
                            item.inboundStops = "Direct Flight";
                        }
                        else if (stops == 1)
                        {
                            item.inboundStops = stops + " Stop";
                        }
                        else
                        {
                            item.inboundStops = stops + " Stops";
                        }

                        inboundCarriers    = leg.OperatingCarriers;
                        inboundOrigin      = leg.OriginStation;
                        inboundDestination = leg.DestinationStation;
                        remaining--;
                    }
                }

                remaining = 4;
                string outboundOriginName      = "";
                string outboundDestinationName = "";
                string inboundOriginName       = "";
                string inboundDestinationName  = "";
                foreach (TripPlace place in result.Places)
                {
                    if (remaining == 0)
                    {
                        break;
                    }

                    if (place.Id == outboundOrigin)
                    {
                        outboundOriginName = place.Code;
                        remaining--;
                    }
                    else if (place.Id == outboundDestination)
                    {
                        outboundDestinationName = place.Code;
                        remaining--;
                    }

                    if (place.Id == inboundOrigin)
                    {
                        inboundOriginName = place.Code;
                        remaining--;
                    }
                    else if (place.Id == inboundDestination)
                    {
                        inboundDestinationName = place.Code;
                        remaining--;
                    }
                }
                item.outboundAirports = outboundOriginName + " - " + outboundDestinationName;
                item.inboundAirports  = inboundOriginName + " - " + inboundDestinationName;

                remaining = inboundCarriers.Length + outboundCarriers.Length;
                foreach (Carrier carrier in result.Carriers)
                {
                    if (remaining == 0)
                    {
                        break;
                    }

                    for (int i = 0; i < outboundCarriers.Length; i++)
                    {
                        if (carrier.Id == outboundCarriers[i])
                        {
                            if (item.outboundAirlines.Length > 0)
                            {
                                item.outboundAirlines += ", " + carrier.Name;
                            }
                            else
                            {
                                item.outboundAirlines += "Operated by " + carrier.Name;
                            }
                            remaining--;
                        }
                    }

                    for (int i = 0; i < inboundCarriers.Length; i++)
                    {
                        if (carrier.Id == inboundCarriers[i])
                        {
                            if (item.inboundAirlines.Length > 0)
                            {
                                item.inboundAirlines += ", " + carrier.Name;
                            }
                            else
                            {
                                item.inboundAirlines += "Operated by " + carrier.Name;
                            }
                            remaining--;
                        }
                    }
                }

                item.lowestPrice = "From " + string.Format("{0:C2}", itinerary.PricingOptions[0].Price);

                collection.Add(item);
            }
            ListViewFlights.ItemsSource = collection;

            ProgressRingFlights.IsActive = false;
            StackPanelLoading.Visibility = Visibility.Collapsed;
        }