Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
        static async Task OldTestCode(IPagedDataSet eventPd)
        {
            while (true)
            {
                Console.Clear();
                var t = await eventPd.GetPageContent();

                Console.WriteLine($"Header: {await t.GetHeader()}");
                Console.WriteLine($"Content: {await t.GetContent()}");

                Console.Write("Command: ");
                var input = Console.ReadKey(true);
                if (input.Key == ConsoleKey.RightArrow)
                {
                    await eventPd.MovePageBy(1);
                }
                else if (input.Key == ConsoleKey.LeftArrow)
                {
                    await eventPd.MovePageBy(-1);
                }
                else if (input.Key == ConsoleKey.DownArrow)
                {
                    await(await eventPd.GetSelector()).MoveCaretBy(eventPd, 1, true);
                }
                else if (input.Key == ConsoleKey.UpArrow)
                {
                    await(await eventPd.GetSelector()).MoveCaretBy(eventPd, -1, true);
                }
                else if (input.Key == ConsoleKey.Spacebar)
                {
                    await(await eventPd.GetSelector()).SetSelectedItem("20");
                }
                else if (input.Key == ConsoleKey.A)
                {
                    await(await eventPd.GetAdder()).AddOffset(eventPd, $"{DateTime.Now}", 0);
                }
                else if (input.Key == ConsoleKey.D)
                {
                    await(await eventPd.GetAdder()).Add($"{DateTime.Now}", 0);
                }
                else if (input.Key == ConsoleKey.X)
                {
                    await(await eventPd.GetRemover()).RemoveAtOffset(eventPd, 0);
                }
                else if (input.Key == ConsoleKey.Z)
                {
                    await(await eventPd.GetRemover()).RemoveAt(eventPd, 0);
                }
                else if (input.Key == ConsoleKey.B)
                {
                    await(await eventPd.GetRemover()).Remove("20");
                }
                else if (input.Key == ConsoleKey.W)
                {
                    await(await eventPd.GetMover()).MoveOffset(eventPd, await(await eventPd.GetSelector()).GetSelectedItem(), -1);
                    await(await eventPd.GetSelector()).MoveCaretBy(eventPd, -1);
                }
                else if (input.Key == ConsoleKey.Q)
                {
                    await(await eventPd.GetMover()).MoveToTop(eventPd, await(await eventPd.GetSelector()).GetSelectedItem());
                    await(await eventPd.GetSelector()).SetCaretPosition(0);
                    await eventPd.SetCurrentPageIndex(0);
                }
                else if (input.Key == ConsoleKey.E)
                {
                    await(await eventPd.GetMover()).MoveToLast(eventPd, await(await eventPd.GetSelector()).GetSelectedItem());
                    await eventPd.SetCurrentPageIndex(await eventPd.GetPageCount() - 1);

                    await(await eventPd.GetSelector()).SetCaretPosition((await eventPd.GetPageItems()).Count() - 1);
                }
                else if (input.Key == ConsoleKey.S)
                {
                    await(await eventPd.GetMover()).MoveOffset(eventPd, await(await eventPd.GetSelector()).GetSelectedItem(), 1);
                    await(await eventPd.GetSelector()).MoveCaretBy(eventPd, 1);
                }
                else if (input.Key == ConsoleKey.Escape)
                {
                    break;
                }
            }
        }