コード例 #1
0
        /// <summary>
        /// Creates a simple paginated list from a collection of items.
        /// </summary>
        /// <param name="feedbackService">The user feedback service.</param>
        /// <param name="interactivityService">The interactivity service.</param>
        /// <param name="sourceUser">The user who caused the interactive message to be created.</param>
        /// <param name="items">The items.</param>
        /// <param name="titleSelector">A function that selects the title for each field.</param>
        /// <param name="valueSelector">A function that selects the value for each field.</param>
        /// <param name="emptyCollectionDescription">The description to use when the collection is empty.</param>
        /// <param name="appearance">The appearance settings to use for the pager.</param>
        /// <typeparam name="TItem">The type of the items in the collection.</typeparam>
        /// <returns>The paginated embed.</returns>
        public static PaginatedEmbed SimpleFieldsFromCollection <TItem>
        (
            UserFeedbackService feedbackService,
            InteractivityService interactivityService,
            IUser sourceUser,
            IEnumerable <TItem> items,
            Func <TItem, string> titleSelector,
            Func <TItem, string> valueSelector,
            string emptyCollectionDescription     = "There's nothing here.",
            PaginatedAppearanceOptions?appearance = null
        )
        {
            appearance ??= PaginatedAppearanceOptions.Default;

            var enumeratedItems = items.ToList();
            var paginatedEmbed  = new PaginatedEmbed(feedbackService, interactivityService, sourceUser)
            {
                Appearance = appearance
            };

            IEnumerable <EmbedBuilder> pages;

            if (!enumeratedItems.Any())
            {
                var eb = paginatedEmbed.Appearance.CreateEmbedBase().WithDescription(emptyCollectionDescription);
                pages = new[] { eb };
            }
            else
            {
                var fields = enumeratedItems.Select
                             (
                    i =>
                    new EmbedFieldBuilder()
                    .WithName
                    (
                        string.IsNullOrWhiteSpace(titleSelector(i)) ? "Not set" : titleSelector(i)
                    )
                    .WithValue
                    (
                        string.IsNullOrWhiteSpace(valueSelector(i)) ? "Not set" : valueSelector(i)
                    )
                             );

                pages = PageFactory.FromFields(fields);
            }

            paginatedEmbed.WithPages(pages);

            return(paginatedEmbed);
        }
コード例 #2
0
        /// <summary>
        /// Creates a simple paginated list from a collection of items.
        /// </summary>
        /// <param name="feedbackService">The user feedback service.</param>
        /// <param name="interactivityService">The interactivity service.</param>
        /// <param name="sourceUser">The user who caused the interactive message to be created.</param>
        /// <param name="items">The items.</param>
        /// <param name="pageBuilder">A function that builds a page for a single value in the collection.</param>
        /// <param name="emptyCollectionDescription">The description to use when the collection is empty.</param>
        /// <param name="appearance">The appearance settings to use for the pager.</param>
        /// <typeparam name="TItem">The type of the items in the collection.</typeparam>
        /// <returns>The paginated embed.</returns>
        public static async Task <PaginatedEmbed> PagesFromCollectionAsync <TItem>
        (
            UserFeedbackService feedbackService,
            InteractivityService interactivityService,
            IUser sourceUser,
            IEnumerable <TItem> items,
            Func <EmbedBuilder, TItem, Task> pageBuilder,
            string emptyCollectionDescription     = "There's nothing here.",
            PaginatedAppearanceOptions?appearance = null
        )
        {
            appearance ??= PaginatedAppearanceOptions.Default;

            var enumeratedItems = items.ToList();
            var paginatedEmbed  = new PaginatedEmbed(feedbackService, interactivityService, sourceUser)
            {
                Appearance = appearance
            };

            IEnumerable <EmbedBuilder> pages;

            if (!enumeratedItems.Any())
            {
                var eb = paginatedEmbed.Appearance.CreateEmbedBase().WithDescription(emptyCollectionDescription);
                pages = new[] { eb };
            }
            else
            {
                var pageList = new List <EmbedBuilder>();
                foreach (var item in enumeratedItems)
                {
                    var page = paginatedEmbed.Appearance.CreateEmbedBase();
                    await pageBuilder(page, item);

                    pageList.Add(page);
                }

                pages = pageList;
            }

            paginatedEmbed.WithPages(pages);

            return(paginatedEmbed);
        }