feat: changed argument for PanelPager from function to interface.

This commit is contained in:
Andrey Parhomenko 2024-07-23 17:05:20 +05:00
parent bae779af8a
commit 94ccb0b724
2 changed files with 22 additions and 13 deletions

View file

@ -6,9 +6,9 @@ import (
var DynamicPanelWidget = tg.RenderFunc(func(c tg.Context) tg.UI {
return tg.UI{
tg.Messagef("Paged panel").PagedPanel(
tg.Messagef("Paged panel").PanelPager(
c, 0, 5,
func(c tg.Context, page, size int) tg.PanelPage {
tg.PanelPagerFunc(func(c tg.Context, page, size int) tg.PanelPage {
rows := []tg.ButtonRow{}
for i := 0; i < size; i++ {
num := 1 + page*size + i
@ -26,7 +26,7 @@ var DynamicPanelWidget = tg.RenderFunc(func(c tg.Context) tg.UI {
Next: page < 3,
Prev: page != 0,
}
},
}),
),
tg.Messagef("").Reply(
BackKeyboard.Reply(),

View file

@ -1,31 +1,40 @@
package tg
type PanelPager interface {
GetPanelPage(c Context, page, size int) PanelPage
}
type PanelPage struct {
Next, Prev bool
Rows []ButtonRow
}
type PanelPageFunc func(
type PanelPagerFunc func(
c Context, page, size int,
) PanelPage
func (fn PanelPagerFunc) GetPanelPage(
c Context, page, size int,
) PanelPage {
return fn(c, page, size)
}
type PagedPanelCompo struct {
type PanelPagerCompo struct {
PanelCompo
page int
size int
nextFormat, prevFormat, delFormat string
fn PanelPageFunc
pager PanelPager
}
func (compo *MessageCompo) PagedPanel(
func (compo *MessageCompo) PanelPager(
c Context,
startPage, size int,
fn PanelPageFunc,
) (*PagedPanelCompo) {
ret := &PagedPanelCompo{}
pager PanelPager,
) (*PanelPagerCompo) {
ret := &PanelPagerCompo{}
ret.page = startPage
ret.size = size
ret.fn = fn
ret.pager = pager
ret.prevFormat = "<<<"
ret.nextFormat = ">>>"
ret.delFormat = "..."
@ -37,10 +46,10 @@ func (compo *MessageCompo) PagedPanel(
return ret
}
func (panel *PagedPanelCompo) MakeRows(
func (panel *PanelPagerCompo) MakeRows(
c Context,
) []ButtonRow {
page := panel.fn(
page := panel.pager.GetPanelPage(
c, panel.page, panel.size,
)