private async void ItemDownloaderOnDownloadComplete(object sender, AsyncCompletedEventArgs asyncCompletedEventArgs) { var id = ((DownloadProgress)asyncCompletedEventArgs.UserState).Id; var url = ((DownloadProgress)asyncCompletedEventArgs.UserState).Url; var willRetryOnError = ((DownloadProgress)asyncCompletedEventArgs.UserState).WillRetryOnError; var localFilePath = ((DownloadProgress)asyncCompletedEventArgs.UserState).LocalFilePath; var title = ((DownloadProgress)asyncCompletedEventArgs.UserState).Title; var isCustomError = ((DownloadProgress)asyncCompletedEventArgs.UserState).IsCustomError; if (!asyncCompletedEventArgs.Cancelled && asyncCompletedEventArgs.Error != null && willRetryOnError) { return; } LibraryItem itemBeingDownloaded = null; if (CloudItems != null) { itemBeingDownloaded = CloudItems.FirstOrDefault(item => item.ID == id || item.DownloadUrl == url); } if (itemBeingDownloaded != null) { if (asyncCompletedEventArgs.Cancelled) { itemBeingDownloaded.DownloadStatus = DownloadStatus.Canceled; } else if (asyncCompletedEventArgs.Error != null) { itemBeingDownloaded.DownloadStatus = DownloadStatus.Failed; } else { itemBeingDownloaded.DownloadStatus = DownloadStatus.Completed; } itemBeingDownloaded.DownloadProgress = null; } if (!asyncCompletedEventArgs.Cancelled) { if (asyncCompletedEventArgs.Error != null) { var message = "There was a problem downloading your recording of \"" + title + "\". Please try again!"; if (isCustomError) { message = asyncCompletedEventArgs.Error.Message; } await RemoteNotificationsService.Instance.TriggerLocalNotification(new AppNotificationMessage { ID = id, Text = message, Type = AppNotificationType.DownloadFailed }); } else { await RemoteNotificationsService.Instance.TriggerLocalNotification(new AppNotificationMessage { ID = id, Text = "Your recording of \"" + title + "\" was downloaded successfully!", Type = AppNotificationType.DownloadComplete }); var downloadedItem = new LibraryItem { ID = id, Storage = LibraryItemStorage.AppLocal, LocalFilePath = localFilePath }; if (LocalLibraryService.Instance.CreateMediaItem(downloadedItem, true)) { if (itemBeingDownloaded != null) { itemBeingDownloaded.LocalItem = downloadedItem; } if (LocalItems != null) { var itemsToDelete = LocalItems.Where( item => item.LocalFilePath == downloadedItem.LocalFilePath && item.Storage == downloadedItem.Storage).ToArray(); foreach (var itemToDelete in itemsToDelete) { LocalItems.Remove(itemToDelete); } LocalItems.Add(downloadedItem); await sort(librarySort); OnPropertyChanged(nameof(LocalItemsCount)); } } else { LoggerService.Instance.Log( "Library.ItemDownloaderOnDownloadComplete: Unable to create media item"); } } } if (itemBeingDownloaded != null) { try { SuspendOnSelectedItemDetailsChangedEvent = true; Download.ChangeCanExecute(); CancelDownload.ChangeCanExecute(); Delete.ChangeCanExecute(); Play.ChangeCanExecute(); SuspendOnSelectedItemDetailsChangedEvent = false; } catch { } } }
public Library(Account accountViewModel) { this.accountViewModel = accountViewModel; Sort = new Command <LibrarySort>(p => LibrarySort = p); Manage = new Command(() => manage()); Download = new Command <LibraryItem>(async p => await DownloadItem(p, true), p => { return(p != null && !p.IsLocal && p.DownloadStatus != DownloadStatus.Downloading); }); CancelDownload = new Command <LibraryItem>(p => cancelDownload(p), p => { return(p != null && !p.IsLocal && p.DownloadStatus == DownloadStatus.Downloading); }); Play = new Command <LibraryItem>(async p => await play(p), p => { return(p != null && (p.IsLocal || p.LocalItem != null)); }); Delete = new Command <LibraryItem>(async p => { if (p == null) { return; } var message = string.Empty; if (p.IsLocal) { message = "Are you sure you want to delete this recording from your device?" + Environment.NewLine + "It is no longer available to download from the cloud."; LibraryItem cloudRecording = null; if (CloudItems != null) { cloudRecording = CloudItems.ToList().FirstOrDefault(c => c.ID == p.ID); } if (cloudRecording != null) { var timeLeft = cloudRecording.Expires.ToLocalTime().Subtract(DateTime.Now); message = "Are you sure you want to delete this recording from your device?" + Environment.NewLine + "You can download it again from the cloud within the next " + Math.Round(timeLeft.TotalDays) + " days."; } } else { message = "Are you sure you want to delete this recording from the cloud?" + Environment.NewLine + "You will need to record it again to download and watch on any device."; LibraryItem localRecording = null; if (LocalItems != null) { localRecording = LocalItems.ToList().FirstOrDefault(l => l.ID == p.ID); } if (localRecording != null) { message = "Are you sure you want to delete this recording from the cloud?" + Environment.NewLine + "It will no longer be available for download on any device."; } } if (await Application.Current.MainPage.DisplayAlert("Delete Recording", message, "Yes", "No")) { try { CancelDownload?.Execute(p); IsLoading = true; if (!await delete(p)) { await Application.Current.MainPage.DisplayAlert("Error Deleting", "Unable to delete the selected recording.", "OK"); } else { OnItemDeleted?.Invoke(this, null); } IsLoading = false; } catch (Exception ex) { LoggerService.Instance.Log("ERROR: Library.DeleteCommand: " + ex); } } }, p => { return(p != null && p.Storage != LibraryItemStorage.iTunes); }); DownloadChecked = new Command(async() => await downloadChecked(), () => { return(selectedView == LibraryViewMode.Cloud); }); ManageDone = new Command(() => manageDone()); DeleteChecked = new Command(async() => { IEnumerable <LibraryItem> items = null; if (selectedView == LibraryViewMode.Cloud) { items = CloudItems.Where(l => l.Checked); } else { items = LocalItems.Where(l => l.Checked); } if (items == null || !items.Any()) { await Application.Current.MainPage.DisplayAlert("Delete Recordings", "Please select one or more recordings to delete.", "OK"); return; } if (await Application.Current.MainPage.DisplayAlert("Delete Recordings", "Are you sure you want to delete the selected recordings?", "Yes", "No")) { if (!await deleteMultipleItems(items.ToArray())) { await Application.Current.MainPage.DisplayAlert("Error Deleting", "Unable to delete all of the selected recordings.", "OK"); } } Edit = false; }); RefreshCloudItems = new Command(async() => { Edit = false; await getItems(true); try { var clouds = cloudItems.ToList(); foreach (var cloudItem in clouds) { if (!string.IsNullOrEmpty(cloudItem.SmallThumbnailUri)) { await ImageService.Instance.InvalidateCacheEntryAsync(cloudItem.SmallThumbnailUri, CacheType.All); } if (!string.IsNullOrEmpty(cloudItem.LargeThumbnailUri)) { await ImageService.Instance.InvalidateCacheEntryAsync(cloudItem.LargeThumbnailUri, CacheType.All); } cloudItem.RefreshImages(); } } catch (Exception ex) { //XXX : Handle error LoggerService.Instance.Log("ERROR: Library.RefreshCloudItems: " + ex); } IsRefreshing = false; }); RefreshLocalItems = new Command(async() => { Edit = false; await updateLocalItems(); IsRefreshing = false; }); itemDownloader = ItemDownloaderService.Instance; itemDownloader.DownloadProgress += ItemDownloaderOnDownloadProgress; itemDownloader.DownloadComplete += ItemDownloaderOnDownloadComplete; }