Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
0
        private async Task getItems(bool cloudItemsOnly = false)
        {
            if (!cloudItemsOnly)
            {
                await getLocalItems();
            }
            else
            {
                await updateLocalItems();
            }

            var collection = await LibraryClient.Get();

            if (collection?.Entries != null)
            {
                if (CloudItems == null)
                {
                    CloudItems = new ObservableCollection <LibraryItem>();
                }

                var entries = new ObservableCollection <LibraryItem>(collection.Entries);
                switch (librarySort)
                {
                case LibrarySort.Alphabetical:
                    await entries.SortByTitle();

                    break;

                case LibrarySort.Recent:
                    await entries.SortByDate();

                    break;
                }

                var newItems = await CloudItems.UpdateItems(entries);

                if (Account != null && Account.SignedIn && Account.UserInfo != null &&
                    !string.IsNullOrEmpty(Account.UserInfo.Email))
                {
                    var cloud = CloudItems.ToList();
                    var email = Account.UserInfo.Email.ToLower();
                    if (!SharedSettingsService.Instance.GetBoolValue("AutoDownloadDBCreated" + email))
                    {
                        SharedSettingsService.Instance.SetBoolValue("AutoDownloadDBCreated" + email, true);
                        foreach (var cloudItem in cloud)
                        {
                            LocalLibraryService.Instance.CreateCloudItemId(cloudItem.ID, Account.UserInfo.Email);
                        }
                    }
                    else
                    {
                        foreach (var cloudItem in newItems)
                        {
                            if (
                                !LocalLibraryService.Instance.IsItemAddedForDownload(cloudItem.ID,
                                                                                     Account.UserInfo.Email))
                            {
                                LocalLibraryService.Instance.CreateCloudItemId(cloudItem.ID, Account.UserInfo.Email);
                                if (AutoDownload)
                                {
                                    LoggerService.Instance.Log("Library.getItems: Found new item for download: ID: " +
                                                               cloudItem.ID);
                                    await DownloadItem(cloudItem, false);
                                }
                            }
                        }
                    }
                }
            }

            if (LocalItems != null && CloudItems != null)
            {
                var local = LocalItems.ToList();
                var cloud = CloudItems.ToList();
                foreach (var localItem in local)
                {
                    var cloudItemForLocalItem =
                        cloud.FirstOrDefault(
                            item => item.ID == localItem.ID && localItem.Storage == LibraryItemStorage.AppLocal);
                    if (cloudItemForLocalItem != null)
                    {
                        cloudItemForLocalItem.LocalItem = localItem;
                        if (cloudItemForLocalItem.DownloadStatus != DownloadStatus.Downloading)
                        {
                            cloudItemForLocalItem.DownloadStatus = DownloadStatus.Completed;
                        }
                    }
                }
            }

            await sort(librarySort);

            OnPropertyChanged(nameof(CloudItemsCount));
        }
Exemplo n.º 3
0
        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;
        }