/// <summary>
        /// Returns strong typed page of data.
        /// </summary>
        /// <param name="pageIndex">Page number.</param>
        /// <param name="pageSize">Size of page.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Strong typed page of data.</returns>
        public async Task <IEnumerable <T> > GetPagedItemsAsync(int pageIndex, int pageSize, CancellationToken cancellationToken = default(CancellationToken))
        {
            // First Call
            if (_isFirstCall)
            {
                _nextPage    = _requestBuilder.CreateChildrenRequest(pageSize, _orderBy, _filter);
                _isFirstCall = false;
            }

            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }

            if (_nextPage != null)
            {
                var oneDriveItems = await _nextPage.GetAsync(cancellationToken);

                _nextPage = oneDriveItems.NextPageRequest;
                return(ProcessResult(oneDriveItems));
            }

            // no more data
            return(null);
        }
        /// <summary>
        /// Request a list of DriveItem from oneDrive and create a MicrosoftGraphOneDriveItemCollection Collection
        /// </summary>
        /// <param name="request">Http request to execute</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param>
        /// <returns>When this method completes successfully, it returns MicrosoftGraphOneDriveItemCollection that represents the specified files or folders</returns>
        private async Task <OneDriveStorageItemsCollection> RequestOneDriveItemsAsync(IItemChildrenCollectionRequest request, CancellationToken cancellationToken)
        {
            var oneDriveItems = await request.GetAsync(cancellationToken).ConfigureAwait(false);

            _nextPageItemsRequest = oneDriveItems.NextPageRequest;

            List <IOneDriveStorageItem> items = new List <IOneDriveStorageItem>();

            foreach (var oneDriveItem in oneDriveItems)
            {
                items.Add(InitializeOneDriveStorageItem(oneDriveItem.CopyToDriveItem()));
            }

            return(new OneDriveStorageItemsCollection(items));
        }
        /// <summary>
        /// Request a list of Folder from oneDrive and create a MicrosoftGraphOneDriveItemCollection Collection
        /// </summary>
        /// <param name="request">Http request to execute</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param>
        /// <returns>When this method completes successfully, it returns MicrosoftGraphOneDriveItemCollection that represents the specified files or folders</returns>
        private async Task <List <IOneDriveStorageFolder> > RequestOneDriveFoldersAsync(IItemChildrenCollectionRequest request, CancellationToken cancellationToken)
        {
            var oneDriveItems = await request.GetAsync(cancellationToken);

            _nextPageFoldersRequest = oneDriveItems.NextPageRequest;

            List <Item> oneDriveFolders           = QueryFolders(oneDriveItems);
            List <IOneDriveStorageFolder> folders = new List <IOneDriveStorageFolder>();

            foreach (var oneDriveFolder in oneDriveFolders)
            {
                folders.Add(InitializeOneDriveStorageFolder(oneDriveFolder.CopyToDriveItem()));
            }

            return(folders);
        }
        /// <summary>
        /// Request a list of items
        /// </summary>
        /// <param name="request">Http request to execute</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param>
        /// <returns>When this method completes successfully, it returns a list of OneDriveStorageFile that represents the specified files</returns>
        private async Task <List <IOneDriveStorageFile> > RequestOneDriveFilesAsync(IItemChildrenCollectionRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            var oneDriveItems = await request.GetAsync(cancellationToken);

            _nextPageFilesRequest = oneDriveItems.NextPageRequest;

            // TODO: The first items on the list are never a file
            List <Item> oneDriveFiles = QueryFiles(oneDriveItems);

            // TODO: Algo to get only File
            List <IOneDriveStorageFile> files = new List <IOneDriveStorageFile>();

            foreach (var oneDriveFile in oneDriveFiles)
            {
                files.Add(InitializeOneDriveStorageFile(oneDriveFile.CopyToDriveItem()));
            }

            return(files);
        }
        /// <inheritdoc/>
        public async Task <IReadOnlyList <IOneDriveStorageItem> > GetItemsAsync(uint startIndex, uint maxItemsToRetrieve, CancellationToken cancellationToken = default(CancellationToken))
        {
            IItemChildrenCollectionRequest oneDriveitemsRequest = null;
            var request = ((IItemRequestBuilder)RequestBuilder).Children.Request();

            // skip is not working right now
            // oneDriveitemsRequest = request.Top((int)maxItemsToRetrieve).Skip((int)startIndex);
            int maxToRetrieve = (int)(maxItemsToRetrieve + startIndex);

            oneDriveitemsRequest = request.Top(maxToRetrieve);
            var tempo = await oneDriveitemsRequest.GetAsync(cancellationToken).ConfigureAwait(false);

            List <IOneDriveStorageItem> items = new List <IOneDriveStorageItem>();

            for (int i = (int)startIndex; i < maxToRetrieve && i < tempo.Count; i++)
            {
                items.Add(InitializeOneDriveStorageItem(tempo[i].CopyToDriveItem()));
            }

            return(new OneDriveStorageItemsCollection(items));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets an index-based range of files and folders from the list of all files and subfolders in the current folder.
        /// </summary>
        /// <param name="startIndex">The zero-based index of the first item in the range to get</param>
        /// <param name="maxItemsToRetrieve">The maximum number of items to get</param>
        /// <returns>When this method completes successfully, it returns a list of the subfolders and files in the current folder.</returns>
        public async Task <OneDriveStorageItemsCollection> GetItemsAsync(uint startIndex, uint maxItemsToRetrieve)
        {
            IItemChildrenCollectionRequest oneDriveitemsRequest = null;
            var request = RequestBuilder.Children.Request();

            // skip is not working right now
            // oneDriveitemsRequest = request.Top((int)maxItemsToRetrieve).Skip((int)startIndex);
            int maxToRetrieve = (int)(maxItemsToRetrieve + startIndex);

            oneDriveitemsRequest = request.Top(maxToRetrieve);
            var tempo = await oneDriveitemsRequest.GetAsync().ConfigureAwait(false);

            List <OneDriveStorageItem> items = new List <OneDriveStorageItem>();

            for (int i = (int)startIndex; i < maxToRetrieve && i < tempo.Count; i++)
            {
                items.Add(InitializeOneDriveStorageItem(tempo[i]));
            }

            return(new OneDriveStorageItemsCollection(items));
        }
Exemplo n.º 7
0
        private async Task ListFilesFolders(string path)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                // update the title path
                if (path == "/")
                {
                    pathText.Text = "CloudStreamer";
                }
                else
                {
                    pathText.Text = path;
                }
                fileScrollViewer.Visibility = Visibility.Collapsed;
                progressRing.Visibility     = Visibility.Visible;
                progressRing.IsActive       = true;
            });

            // handle login
            try
            {
                await UpdateOrInitOneDriveAuthIfNecessary();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e, "error");
                try
                {
                    await ExitOrRetryWithMessage("Failed to authenticate with OneDrive. Error: " + e.ToString());
                }
                catch (Exception ex)
                {
                    Crashes.TrackError(ex, new Dictionary <string, string>
                    {
                        { "On", "Call Error Dialog" },
                        { "Where", "MainPage.xaml:InitOneDrive" },
                        { "DueTo", e.Message }
                    });
                }
                return;
            }


            if (oneDriveClient == null)
            {
                return;
            }

            try
            {
                IItemChildrenCollectionRequest request;
                if (path == "/")
                {
                    request = oneDriveClient.Drive.Root.Children.Request();
                }
                else
                {
                    request = oneDriveClient.Drive.Root.ItemWithPath(path).Children.Request();
                }
                IItemChildrenCollectionRequest sortedRequest = request.OrderBy(currentSortBy + " " + currentSortByDir);
                sortedRequest = sortedRequest.Expand("thumbnails");
                files         = (ItemChildrenCollectionPage)await sortedRequest.GetAsync();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e, "error");
                try
                {
                    await ExitOrRetryWithMessage("Failed to load Files from OneDrive. Error: " + e.ToString());
                }
                catch (Exception ex)
                {
                    Crashes.TrackError(ex, new Dictionary <string, string>
                    {
                        { "On", "Call Error Dialog" },
                        { "Where", "MainPage.xaml:ListFilesFolders" },
                        { "DueTo", e.Message }
                    });
                }
            }
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                fileItems = new IncrementalLoadingCollection(files);
                fileScrollViewer.ItemsSource = fileItems;
                loading = false;
                progressRing.IsActive       = false;
                progressRing.Visibility     = Visibility.Collapsed;
                fileScrollViewer.Visibility = Visibility.Visible;
            });

            System.Diagnostics.Debug.WriteLine("Got " + fileItems.Count() + " files");
        }