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 { } } }
private async Task <bool> delete(LibraryItem item) { if (item == null || item.Storage == LibraryItemStorage.iTunes) { return(false); } LoggerService.Instance.Log("INFO: Library.delete: ID: " + item.ID); if (item.IsLocal) { if (item.Storage == LibraryItemStorage.AppLocal) { var deleted = LocalLibraryService.Instance.DeleteMediaItem(item, true); if (deleted) { if (CloudItems != null) { var deletedItem = CloudItems.FirstOrDefault(cloudItem => item.ID == cloudItem.ID); if (deletedItem != null) { deletedItem.DownloadStatus = DownloadStatus.Unknown; deletedItem.LocalItem = null; } } if (LocalItems != null) { LocalItems.Remove(item); OnPropertyChanged(nameof(LocalItemsCount)); } if (item.Equals(SelectedItem)) { SelectedItem = null; } if (item.Equals(SelectedLibraryItem)) { SelectedLibraryItem = null; } return(true); } } } else { var response = await LibraryClient.Delete(item.ID); if (response != null && response.Success) { if (Account != null && Account.SignedIn && Account.UserInfo != null && !string.IsNullOrEmpty(Account.UserInfo.Email)) { LocalLibraryService.Instance.DeleteCloudItemId(item.ID, Account.UserInfo.Email); } CloudItems.Remove(item); OnPropertyChanged(nameof(CloudItemsCount)); if (item.Equals(SelectedItem)) { SelectedItem = null; } if (item.Equals(SelectedCloudItem)) { SelectedCloudItem = null; } return(true); } } return(false); }