コード例 #1
0
        public async Task ShouldDownloadContentFromTwoSitesAsAsyncStream()
        {
            var bd = new BatchDownloader();

            string[] urls = { "http://aliozgur.net", "https://www.microsoft.com" };

            var results = bd.DownloadAllAsAsyncStream(urls);

            var totalLen = 0;

            await foreach (var result in results)
            {
                totalLen += result.Length;
            }

            Assert.IsTrue(totalLen > 0);
        }
コード例 #2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            var messageList = FindViewById <ListView>(Resource.Id.msgList);

            adapter             = new ArrayAdapter <string>(this, Android.Resource.Layout.SimpleListItem1);
            messageList.Adapter = adapter;
            var progressBar = FindViewById <ProgressBar>(Resource.Id.downloadProgressBar);

            progressBar.Max = 100;

            var datePicker = FindViewById <DatePicker>(Resource.Id.downloadDatePicker);
            var button     = FindViewById <Button>(Resource.Id.downloadButton);

            button.Click += async(object sender, EventArgs e) =>
            {
                var cm = GetSystemService(ConnectivityService) as ConnectivityManager;
                if (cm?.ActiveNetworkInfo.Type != ConnectivityType.Wifi)
                {
                    var builder = new AlertDialog.Builder(this);
                    builder.SetMessage("Connect without wifi?");
                    builder.SetNegativeButton("Cancel", (obj, x) => { });
                    builder.SetPositiveButton("Ok", async(obj, which) =>
                    {
                        await StartBatchDownload();
                    });
                    builder.Show();
                }
                else
                {
                    await StartBatchDownload();
                }
            };

            Task StartBatchDownload()
            {
                return(Task.Run(() =>
                {
                    var savePath = GetSavePath();

                    AddressBuilder.Date = datePicker.DateTime;
                    var addrs = AddressBuilder.Load(Assets.Open("PodAddress.xml"));
                    var urls = AddressBuilder.GetEffectiveAddresses(addrs);

                    List <string> failedList = new List <string>();
                    failedList.AddRange(BatchDownloader.DownloadFromUrls(savePath, urls, DownloadProgressHandler));

                    RunOnUiThread(() =>
                    {
                        failedList
                        .Select(msg => "failed url: " + msg)
                        .ToList()
                        .ForEach(msg =>
                        {
                            Log.Info(typeof(MainActivity).ToString(), msg);
                            adapter.Add(msg);
                        });
                        adapter.Add("Download complete");
                        adapter.NotifyDataSetChanged();
                        Toast.MakeText(this, "Download complete", ToastLength.Long).Show();
                    });
                }));
            }

            void DownloadProgressHandler(object sender, DownloadProgressChangedEventArgs e)
            {
                progressBar.SetProgress(e.ProgressPercentage, true);
            }
        }