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

View file

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