public static async Task StartDownloadAsync(int position, String url, ListView listView) { if (ViewsDownloadInProgressByAudioId.ContainsKey (url)) { return; } // Locate the the child view and update. var firstPostion = listView.FirstVisiblePosition - listView.HeaderViewsCount; var childIndex = position - firstPostion; if (0 <= childIndex && childIndex < listView.ChildCount) { var view = listView.GetChildAt (childIndex); var progressBar = view.FindViewById<ProgressBar> (Resource.Id.DownloadProgress); ViewsDownloadInProgressByAudioId.Add (url, view); var downloader = new AndroidAudioDownloader (); await downloader.CreateDownloadTask (url, progressBar).ContinueWith (task => { if (!task.IsFaulted) { ViewsDownloadInProgressByAudioId.Remove(url); Log.Debug (DEBUG_TAG, "Downloaded {0} bytes.", task.Result); } } ); } }
async void OnAlbumItemClicked(object sender, AdapterView.ItemClickEventArgs e) { var selectedEpisode = _album.Episodes [e.Position]; if (AndroidAudioDownloader.ViewsDownloadInProgressByAudioId.ContainsKey(selectedEpisode.RemoteUrl)) { return; } if (AudioDownloader.HasLocalFile(selectedEpisode.RemoteUrl, selectedEpisode.FileSize)) { var resultIntent = new Intent(); ExtraUtils.PutEpisode(resultIntent, selectedEpisode.Id); ExtraUtils.PutAlbum(resultIntent, _album.Id); ExtraUtils.PutSelectedTab(resultIntent, (int)MainActivity.TabTitle.Player); SetResult(Result.Ok, resultIntent); StartService(PlayerService.CreateIntent( this, PlayerService.ACTION_PLAY, _album.Id, selectedEpisode.Id )); Finish(); } else { await AndroidAudioDownloader.StartDownloadAsync(e.Position, selectedEpisode.RemoteUrl, ListView); } }