Exemple #1
0
        /// <summary>
        /// Gets triggered if one of the buttons of a cell in the table view was touched.
        /// </summary>
        /// <param name="cell">Cell.</param>
        /// <param name="button">Button.</param>
        /// <param name="action">Action.</param>
        async void HandleActionRequested(DownloaderCell cell, UIButton button, DownloaderCell.TRANSFER_ACTION action)
        {
            // Get the index of the cell in the table view.
            var path = this.TableView.IndexPathForCell(cell);

            if (path == null)
            {
                return;
            }
            int index = path.Row;

            // Get the related download info object.
            var downloadInfo = AppDelegate.AvailableDownloads[index];

            // Via the task identifier we can get back to the enqueued task.
            var pendingTasks = await this.Session.GetTasksAsync();

            NSUrlSessionDownloadTask downloadTask = pendingTasks.DownloadTasks.FirstOrDefault(t => t.TaskIdentifier == downloadInfo.TaskId);

            switch (action)
            {
            case DownloaderCell.TRANSFER_ACTION.Download:
                Console.WriteLine("Creating new download task.");
                // Create a new download task.
                downloadTask = this.Session.CreateDownloadTask(NSUrl.FromString(downloadInfo.Source));

                // The creation can fail.
                if (downloadTask == null)
                {
                    new UIAlertView(string.Empty, "Failed to create download task! Please retry.", null, "OK").Show();
                    return;
                }

                // Update the download info object.
                downloadInfo.TaskId = (int)downloadTask.TaskIdentifier;
                downloadInfo.Status = DownloadInfo.STATUS.Downloading;

                // Resume / start the download.
                downloadTask.Resume();
                Console.WriteLine("Starting download of '{0}'. State of task: '{1}'. ID: '{2}'", downloadInfo.Title, downloadTask.State, downloadTask.TaskIdentifier);

                /*
                 * // If this code is commented in, the app will exit immediately after the downlaod has been initiated.
                 * // When the download completes, iOS will relaunch the app and the UI will reflect the current state.
                 * AppDelegate.SerializeAvailableDownloads ();
                 * Exit (3);
                 */

                break;

            case DownloaderCell.TRANSFER_ACTION.Stop:
                if (downloadTask != null)
                {
                    downloadTask.Cancel();
                }
                downloadInfo.Reset();
                downloadInfo.Status = DownloadInfo.STATUS.Cancelled;
                Console.WriteLine("Stopping download of '{0}'.", downloadInfo.Title);
                break;

            case DownloaderCell.TRANSFER_ACTION.View:
                break;
            }

            // Update UI.
            cell.UpdateFromDownloadInfo(downloadInfo);
        }
		/// <summary>
		/// Gets triggered if one of the buttons of a cell in the table view was touched.
		/// </summary>
		/// <param name="cell">Cell.</param>
		/// <param name="button">Button.</param>
		/// <param name="action">Action.</param>
		async void HandleActionRequested(DownloaderCell cell, UIButton button, DownloaderCell.TRANSFER_ACTION action)
		{
			// Get the index of the cell in the table view.
			var path = this.TableView.IndexPathForCell (cell);
			if (path == null)
			{
				return;
			}
			int index = path.Row;

			// Get the related download info object.
			var downloadInfo = AppDelegate.AvailableDownloads[index];

			// Via the task identifier we can get back to the enqueued task.
			var pendingTasks = await this.Session.GetTasksAsync ();
			NSUrlSessionDownloadTask downloadTask = pendingTasks.DownloadTasks.FirstOrDefault (t => t.TaskIdentifier == downloadInfo.TaskId);

			switch(action)
			{
			case DownloaderCell.TRANSFER_ACTION.Download:
				Console.WriteLine ("Creating new download task.");
				// Create a new download task.
				downloadTask = this.Session.CreateDownloadTask (NSUrl.FromString (downloadInfo.Source));

				// The creation can fail. 
				if (downloadTask == null)
				{
					new UIAlertView (string.Empty, "Failed to create download task! Please retry.", null, "OK").Show ();
					return;
				}

				// Update the download info object.
				downloadInfo.TaskId = (int)downloadTask.TaskIdentifier;
				downloadInfo.Status = DownloadInfo.STATUS.Downloading;

				// Resume / start the download.
				downloadTask.Resume ();
				Console.WriteLine ("Starting download of '{0}'. State of task: '{1}'. ID: '{2}'", downloadInfo.Title, downloadTask.State, downloadTask.TaskIdentifier);

				/*
				// If this code is commented in, the app will exit immediately after the downlaod has been initiated.
				// When the download completes, iOS will relaunch the app and the UI will reflect the current state.
				AppDelegate.SerializeAvailableDownloads ();
				Exit (3);
				*/

				break;

			case DownloaderCell.TRANSFER_ACTION.Stop:
				if (downloadTask != null)
				{
					downloadTask.Cancel ();
				}
				downloadInfo.Reset ();
				downloadInfo.Status = DownloadInfo.STATUS.Cancelled;
				Console.WriteLine ("Stopping download of '{0}'.", downloadInfo.Title);
				break;

			case DownloaderCell.TRANSFER_ACTION.View:
				break;
			}

			// Update UI.
			cell.UpdateFromDownloadInfo (downloadInfo);
		}