Ejemplo n.º 1
0
 public static async Task<DownloadManager> Create(RouteListing listing, CancellationToken cancellationToken)
 {
     listing.Progress = 0;
     listing.ShowProgress = true;
     DownloadManager result = new DownloadManager();
     result.Listing = listing;
     result._Route = result.Listing.Route;
     var stopsAndShapes = await ApiLayer.GetStopsAndShapesForRoute(result.Listing.Route.ID, cancellationToken);
     result._Shapes = stopsAndShapes.Item2;
     result._StopsPending = new ObservableCollection<BusStop>(stopsAndShapes.Item1);
     if (cancellationToken.IsCancellationRequested)
         return null;
     result.Initialize();
     result._TotalStops = result.StopsPending.Count;
     DownloadsInProgress.Add(result);
     if (!FileManager.PendingDownloads.Any(item => item.First() == result.Route.ID))
     {
         List<string> pend = new List<string>() { result.Route.ID };
         pend.AddRange(result.StopsPending.Select(stop => stop.ID));
         FileManager.PendingDownloads.Add(pend);
         await listing.RefreshIsDownloaded();
         await FileManager.SavePendingDownloads();
     }
     return result;
 }
Ejemplo n.º 2
0
 public static async Task<DownloadManager> Create(RouteListing listing, CancellationToken cancellationToken)
 {
     listing.Progress = 0;
     listing.ShowProgress = true;
     DownloadManager result = new DownloadManager();
     result.Listing = listing;
     result._Route = result.Listing.Route;
     var stopsAndShapes = await ApiLayer.GetStopsForRoute(result.Listing.Route.ID, cancellationToken);
     result._Shapes = stopsAndShapes.Item2;
     result._StopsPending = new ObservableCollection<BusStop>(stopsAndShapes.Item1);
     if (cancellationToken.IsCancellationRequested)
         return null;
     result.Initialize();
     result._TotalStops = result.StopsPending.Count;
     DownloadsInProgress.Add(result);
     return result;
 }
Ejemplo n.º 3
0
        private async void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            var           cancel = true;
            MessageDialog dialog = new MessageDialog("This will stop downloading and delete what was downloaded of the selected routes. Are you sure?", "Cancel Downloads?");

            dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler((cmd) => cancel = false)));
            dialog.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => cancel  = true)));
            await dialog.ShowAsync();

            if (cancel)
            {
                return;
            }
            LoadingRect.Visibility    = Visibility.Visible;
            MainProgressRing.IsActive = true;
            DownloadsCancellationTokenSource.Cancel();
            DownloadsCancellationTokenSource = new CancellationTokenSource();
            await DownloadsTask;
            var   items = MainList.Items.Where(item => ((item as RouteListing)?.IsChecked).GetValueOrDefault(false)).Select(item => (RouteListing)item).ToArray();
            await DownloadManager.DeleteRoutes(items);

            MainProgressRing.IsActive = false;
            LoadingRect.Visibility    = Visibility.Collapsed;
        }
Ejemplo n.º 4
0
        public static async Task <RouteListing[]> DownloadAll(Action <double, string> statusChangedCallback, CancellationToken cancellationToken, params RouteListing[] routeListings)
        {
            List <RouteListing> errorList = new List <RouteListing>();
            List <BusStop>      allStops  = new List <BusStop>();
            await FileManager.LoadPendingDownloads();

            try
            {
                for (int i = 0; i < routeListings.Length; i++)
                {
                    statusChangedCallback(0.15 * i / routeListings.Length, "Getting stops (" + (i + 1).ToString() + " of " + routeListings.Length.ToString() + ")" + " " + (routeListings[i].Name.All(chr => char.IsDigit(chr)) ? "Route " : "") + routeListings[i].Name);
                    try
                    {
                        var manager = await DownloadManager.Create(routeListings[i], cancellationToken);

                        await FileManager.SaveRoute(manager.Route, manager.StopsPending.Select(item => item.ID).ToArray(), manager.Shapes);

                        foreach (var stop in manager.StopsPending)
                        {
                            if (!allStops.Contains(stop))
                            {
                                allStops.Add(stop);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        errorList.Add(routeListings[i]);
                        ex.ToString();
                    }
                }

                WeekSchedule schedule;
                string[]     routeFilters = DownloadsInProgress.Select(download => download.Listing.Route.ID).ToArray();

                for (int i = 0; i < allStops.Count; i++)
                {
                    statusChangedCallback(0.15 + 0.85 * i / allStops.Count, "Downloading schedules (" + (i + 1).ToString() + " of " + allStops.Count.ToString() + ") " + allStops[i].Name);
                    try
                    {
                        var pend = FileManager.PendingDownloads.Where(item => routeFilters.Contains(item.First())).ToArray();
                        if (pend.Any(item => item.Contains(allStops[i].ID)))
                        {
                            schedule = await ApiLayer.GetScheduleForStop(allStops[i].ID, cancellationToken);

                            schedule.FilterByRoutes(routeFilters);
                            await FileManager.SaveScheduleAsync(schedule, allStops[i]);

                            foreach (var item in pend)
                            {
                                if (item.Contains(allStops[i].ID))
                                {
                                    item.Remove(allStops[i].ID);
                                }
                                if (item.Count == 1)
                                {
                                    FileManager.PendingDownloads.Remove(item);
                                }
                            }
                            await FileManager.SavePendingDownloads();
                        }
                        foreach (var manager in DownloadsInProgress.ToArray())
                        {
                            if (manager.StopsPending.Contains(allStops[i]))
                            {
                                manager.StopsPending.Remove(allStops[i]);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        foreach (var download in DownloadsInProgress.Where(item => item.StopsPending.Contains(allStops[i])))
                        {
                            if (!errorList.Contains(download.Listing))
                            {
                                errorList.Add(download.Listing);
                            }
                        }
                        ex.ToString();
                    }
                }
                statusChangedCallback(1, "Download complete.");
            }
            catch (OperationCanceledException)
            {
                statusChangedCallback(1, "Download cancelled.");
                while (DownloadsInProgress.Count > 0)
                {
                    DownloadsInProgress[0].StopsPending.Clear();
                }
            }
            return(errorList.ToArray());
        }
Ejemplo n.º 5
0
        private async void DownloadButton_Click(object sender, RoutedEventArgs e)
        {
            if (BandwidthManager.EffectiveBandwidthOptions == BandwidthOptions.Low)
            {
                if (SettingsManager.GetSetting("LimitedData.WarnOnDownload", false, true))
                {
                    MessageDialog dialog = new MessageDialog("You are on a limited data plan and not on wifi. Are you sure you want to download these routes over this data?", "Limited Data");
                    dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler((cmd) => DownloadLimitedData = true)));
                    dialog.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => DownloadLimitedData  = false)));
                    await dialog.ShowAsync();

                    if (!DownloadLimitedData)
                    {
                        return;
                    }
                }
                else
                {
                    DownloadLimitedData = true;
                }
            }
            else
            {
                DownloadLimitedData = false;
            }
            var cancellationToken = DownloadsCancellationTokenSource.Token;

            VisualStateManager.GoToState(this, "DownloadingState", true);
            var items = MainList.Items.Where(item => ((item as RouteListing)?.IsChecked).GetValueOrDefault(false)).Select(item => (RouteListing)item).ToArray();

            DownloadsTask = DownloadManager.DownloadAll(async(p, m) => await SetProgress(p, m), cancellationToken, items);
            var errors = await DownloadsTask;

            if (cancellationToken.IsCancellationRequested)
            {
                await SetProgress(1, "Cancelled");
            }
            VisualStateManager.GoToState(this, "NotDownloadingState", true);
            if (errors != null && errors.Length > 0)
            {
                VisualStateManager.GoToState(this, "SelectingState", true);
                foreach (RouteListing listing in MainList.Items)
                {
                    listing.IsChecked = errors.Contains(listing);
                }
                MessageDialog dialog = new MessageDialog("There was an error downloading " + errors.Length.ToString() + " of the routes. The affected routes are selected.", "Error downloading some routes");
                await dialog.ShowAsync();
            }
            else
            {
                bool checkMode = false;
                foreach (RouteListing listing in MainList.Items)
                {
                    checkMode = checkMode || (listing.IsChecked = listing.IsDownloaded == DownloadStatus.Downloading);
                }
                if (checkMode)
                {
                    VisualStateManager.GoToState(this, "SelectingState", true);
                }
                else
                {
                    VisualStateManager.GoToState(this, "NotSelectingState", true);
                }
            }
        }