示例#1
0
        private async Task <QueryResult <BaseItem> > GetChildrenSorted(Folder folder, User user, SortCriteria sort, int?startIndex, int?limit)
        {
            if (folder is UserRootFolder)
            {
                var result = await _userViewManager.GetUserViews(new UserViewQuery
                {
                    UserId = user.Id.ToString("N")
                }, CancellationToken.None).ConfigureAwait(false);

                return(ToResult(result, startIndex, limit));
            }

            var view = folder as UserView;

            if (view != null)
            {
                var result = await GetUserViewChildren(view, user, sort).ConfigureAwait(false);

                return(ToResult(result, startIndex, limit));
            }

            var channel = folder as Channel;

            if (channel != null)
            {
                try
                {
                    // Don't blow up here because it could cause parent screens with other content to fail
                    return(await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
                    {
                        ChannelId = channel.Id.ToString("N"),
                        Limit = limit,
                        StartIndex = startIndex,
                        UserId = user.Id.ToString("N")
                    }, CancellationToken.None));
                }
                catch
                {
                    // Already logged at lower levels
                }
            }

            var channelFolderItem = folder as ChannelFolderItem;

            if (channelFolderItem != null)
            {
                try
                {
                    // Don't blow up here because it could cause parent screens with other content to fail
                    return(await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
                    {
                        ChannelId = channelFolderItem.ChannelId,
                        FolderId = channelFolderItem.Id.ToString("N"),
                        Limit = limit,
                        StartIndex = startIndex,
                        UserId = user.Id.ToString("N")
                    }, CancellationToken.None));
                }
                catch
                {
                    // Already logged at lower levels
                }
            }

            return(ToResult(GetPlainFolderChildrenSorted(folder, user, sort), startIndex, limit));
        }
示例#2
0
        private async Task GetAllItems(string user, string channelId, string folderId, int currentRefreshLevel, int maxRefreshLevel, IProgress <double> progress, CancellationToken cancellationToken)
        {
            var folderItems = new List <string>();

            var innerProgress = new ActionableProgress <double>();

            innerProgress.RegisterAction(p => progress.Report(p / 2));

            var result = await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
            {
                ChannelId = channelId,
                UserId    = user,
                FolderId  = folderId
            }, innerProgress, cancellationToken);

            folderItems.AddRange(result.Items.Where(i => i.IsFolder).Select(i => i.Id.ToString("N")));

            var totalRetrieved = result.Items.Length;
            var totalCount     = result.TotalRecordCount;

            while (totalRetrieved < totalCount)
            {
                result = await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
                {
                    ChannelId  = channelId,
                    UserId     = user,
                    StartIndex = totalRetrieved,
                    FolderId   = folderId
                }, new Progress <double>(), cancellationToken);

                folderItems.AddRange(result.Items.Where(i => i.IsFolder).Select(i => i.Id.ToString("N")));

                totalRetrieved += result.Items.Length;
                totalCount      = result.TotalRecordCount;
            }

            progress.Report(50);

            if (currentRefreshLevel < maxRefreshLevel)
            {
                var numComplete = 0;
                var numItems    = folderItems.Count;

                foreach (var folder in folderItems)
                {
                    try
                    {
                        innerProgress = new ActionableProgress <double>();

                        var startingNumberComplete = numComplete;
                        innerProgress.RegisterAction(p =>
                        {
                            double innerPercent = startingNumberComplete;
                            innerPercent       += (p / 100);
                            innerPercent       /= numItems;
                            progress.Report((innerPercent * 50) + 50);
                        });

                        await GetAllItems(user, channelId, folder, currentRefreshLevel + 1, maxRefreshLevel, innerProgress, cancellationToken).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorException("Error getting channel content", ex);
                    }

                    numComplete++;
                    double percent = numComplete;
                    percent /= numItems;
                    progress.Report((percent * 50) + 50);
                }
            }

            progress.Report(100);
        }