Example #1
0
        private void ItemDownloaderOnDownloadProgress(object sender, DownloadProgress itemDownloaderProgress)
        {
            if (cloudItems == null)
            {
                return;
            }

            var itemBeingDownloaded = cloudItems.FirstOrDefault(item =>
                                                                item.ID == itemDownloaderProgress.Id ||
                                                                item.DownloadUrl == itemDownloaderProgress.Url);

            if (itemBeingDownloaded != null)
            {
                if (itemBeingDownloaded.DownloadStatus != DownloadStatus.Downloading)
                {
                    itemBeingDownloaded.DownloadStatus = DownloadStatus.Downloading;
                    try
                    {
                        SuspendOnSelectedItemDetailsChangedEvent = true;
                        Download.ChangeCanExecute();
                        CancelDownload.ChangeCanExecute();
                        Delete.ChangeCanExecute();
                        Play.ChangeCanExecute();
                        SuspendOnSelectedItemDetailsChangedEvent = false;
                    }
                    catch
                    {
                    }
                }

                itemBeingDownloaded.DownloadProgress = itemDownloaderProgress;
            }
        }
Example #2
0
        private async Task doItemDownload(LibraryItem item)
        {
            try
            {
                item.DownloadStatus = DownloadStatus.Downloading;
                var downloadResult =
                    await itemDownloader.Download(new DownloadItem { Id = item.ID, FileName = item.FullTitle + ".mp4" });

                if (!downloadResult.Item1)
                {
                    if (IsInBackground)
                    {
                        await RemoteNotificationsService.Instance.TriggerLocalNotification(
                            "We're sorry. This video cannot be downloaded because there is not enough space on your device. Please clear some space and try again.");
                    }
                    else
                    {
                        await Application.Current.MainPage.DisplayAlert("Error",
                                                                        "We're sorry. This video cannot be downloaded because there is not enough space on your device. Please clear some space and try again.",
                                                                        "OK");
                    }

                    item.DownloadUrl      = downloadResult.Item2;
                    item.DownloadStatus   = DownloadStatus.Failed;
                    item.DownloadProgress = null;
                }

                item.DownloadUrl = downloadResult.Item2;
                if (CloudItems != null && CloudItems.ToList().Contains(item))
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        SuspendOnSelectedItemDetailsChangedEvent = true;
                        Download.ChangeCanExecute();
                        CancelDownload.ChangeCanExecute();
                        Delete.ChangeCanExecute();
                        SuspendOnSelectedItemDetailsChangedEvent = false;

                        if (!downloadResult.Item1)
                        {
                            Play.ChangeCanExecute();
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                LoggerService.Instance.Log("ERROR: Failed to start downloading of item: Exception: " + ex);
            }
        }
Example #3
0
        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
                {
                }
            }
        }