/// <summary>
        /// Remove an item from the <see cref="IPagedDataSet"/> with the specified position
        /// </summary>
        /// <param name="dataSet">The dataset to modify</param>
        /// <param name="offset">The position to remove relative to the dataset's caret</param>
        /// <returns></returns>
        public static async Task RemoveAtOffset(this IRemovable removable, IPagedDataSet dataSet, int offset)
        {
            var pi = await dataSet.GetCurrentPageIndex();

            var ps = await dataSet.GetPageSize();

            var sel = await dataSet.GetSelector();

            if (sel == null)
            {
                throw new InvalidOperationException("The paged data set does not support selecting items that is required for this method to work");
            }

            await removable.RemoveAt(dataSet, pi *ps + offset + await sel.GetCaretPosition());
        }
        /// <summary>
        /// Add an item into the <see cref="IPagedDataSet"/> with the position relative to the <see cref="ISelectable"/>'s caret
        /// </summary>
        /// <param name="addable">The adder to perform</param>
        /// <param name="item">The item to add</param>
        /// <param name="dataSet">the dataset to modify</param>
        /// <param name="offset">The position to add relative to the <see cref="IPagedDataSet{TItem, TResult}"/> caret</param>
        /// <returns></returns>
        public static async Task AddOffset(this IAddable addable, IPagedDataSet dataSet, object item, int offset)
        {
            var selector = await dataSet.GetSelector();

            if (selector == null)
            {
                throw new InvalidOperationException("PagedDataSet must support selector to use this function!");
            }
            var pi = await dataSet.GetCurrentPageIndex();

            var ps = await dataSet.GetPageSize();

            var caret = await selector.GetCaretPosition();

            await addable.Add(item, pi *ps + caret + offset);
        }
Beispiel #3
0
        // TODO: BIGTODO Find a better way to move through pages. This is a very smelly code!
        /// <summary>
        /// Jump the caret to a new position
        /// </summary>
        /// <param name="offset">the number of item to jump. A positive value will jump forward, a negative value will jump backward</param>
        /// <param name="dataSet">The data set to move caret</param>
        /// <param name="throughPage">should overflow offset move to a new page</param>
        /// <returns></returns>
        public static async Task MoveCaretBy(this ISelectable selectable, IPagedDataSet dataSet, int offset, bool throughPage = false)
        {
            if (throughPage)
            {
                var pageCount = await dataSet.GetPageCount();

                if (offset > 0)
                {
                    var tempCaret = await selectable.GetCaretPosition();

                    var tempPageIndex = await dataSet.GetCurrentPageIndex();

                    var currentPageSize = (await dataSet.GetPageItems()).Count();
                    if (currentPageSize == 0)
                    {
                        return;
                    }
                    if (currentPageSize - tempCaret - offset <= 0)
                    {
                        offset       -= currentPageSize - tempCaret;
                        tempPageIndex = (tempPageIndex + 1).NormalizeIndex(pageCount);
                    }
                    else
                    {
                        await selectable.SetCaretPosition(tempCaret + offset);

                        return;
                    }

                    while (true)
                    {
                        await dataSet.SetCurrentPageIndex(tempPageIndex);

                        currentPageSize = (await dataSet.GetPageItems()).Count();
                        if (offset - currentPageSize >= 0)
                        {
                            offset       -= currentPageSize;
                            tempPageIndex = (tempPageIndex + 1).NormalizeIndex(pageCount);
                        }
                        else
                        {
                            break;
                        }
                    }
                    await dataSet.SetCurrentPageIndex(tempPageIndex);

                    await selectable.SetCaretPosition(offset);
                }
                else
                {
                    var tempCaret = await selectable.GetCaretPosition();

                    var tempPageIndex = await dataSet.GetCurrentPageIndex();

                    int currentPageSize;
                    if (tempCaret + offset < 0)
                    {
                        offset        = Math.Abs(offset) - tempCaret - 1;
                        tempPageIndex = (tempPageIndex - 1).NormalizeIndex(pageCount);
                        await dataSet.SetCurrentPageIndex(tempPageIndex);

                        currentPageSize = (await dataSet.GetPageItems()).Count();
                        if (currentPageSize == 0)
                        {
                            return;
                        }
                        tempCaret = currentPageSize - 1;
                    }
                    else
                    {
                        await selectable.SetCaretPosition(tempCaret + offset);

                        return;
                    }

                    while (true)
                    {
                        if (offset - currentPageSize >= 0)
                        {
                            offset       -= currentPageSize;
                            tempPageIndex = (tempPageIndex - 1).NormalizeIndex(pageCount);
                            await dataSet.SetCurrentPageIndex(tempPageIndex);

                            currentPageSize = (await dataSet.GetPageItems()).Count();
                            tempCaret       = currentPageSize - 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    await selectable.SetCaretPosition(tempCaret - offset);
                }
            }
            else
            {
                await selectable.SetCaretPosition(await selectable.GetCaretPosition() + offset);
            }
        }