feat: added the paged panel for easier making pagination stuff.

This commit is contained in:
Andrey Parhomenko 2024-07-22 20:45:53 +05:00
parent 1471d7cdae
commit bae779af8a
2 changed files with 122 additions and 38 deletions

View file

@ -5,21 +5,14 @@ import (
) )
var DynamicPanelWidget = tg.RenderFunc(func(c tg.Context) tg.UI { var DynamicPanelWidget = tg.RenderFunc(func(c tg.Context) tg.UI {
var ( return tg.UI{
n = 0 tg.Messagef("Paged panel").PagedPanel(
ln = 4 c, 0, 5,
panel *tg.PanelCompo func(c tg.Context, page, size int) tg.PanelPage {
) rows := []tg.ButtonRow{}
for i := 0; i < size; i++ {
panel = tg.Messagef( num := 1 + page*size + i
"Some panel", rows = append(rows, tg.ButtonRow{
).Panel(c, tg.RowserFunc(func(c tg.Context) []tg.ButtonRow {
btns := []tg.ButtonRow{
tg.ButtonRow{tg.Buttonf("Page %d", n)},
}
for i := 0; i < ln; i++ {
num := 1 + n*ln + i
btns = append(btns, tg.ButtonRow{
tg.Buttonf("%d", num).Rand().WithAction(tg.Func(func(c tg.Context) { tg.Buttonf("%d", num).Rand().WithAction(tg.Func(func(c tg.Context) {
_, err := c.Sendf("%d", num*num) _, err := c.Sendf("%d", num*num)
if err != nil { if err != nil {
@ -28,22 +21,13 @@ var DynamicPanelWidget = tg.RenderFunc(func(c tg.Context) tg.UI {
tg.Buttonf("%d", num*num), tg.Buttonf("%d", num*num),
}) })
} }
btns = append(btns, tg.ButtonRow{ return tg.PanelPage{
tg.Buttonf("Prev").WithAction(tg.Func(func(c tg.Context) { Rows: rows,
n-- Next: page < 3,
panel.Update(c) Prev: page != 0,
})), }
tg.Buttonf("Next").WithAction(tg.Func(func(c tg.Context) { },
n++ ),
panel.Update(c)
})),
})
return btns
}))
return tg.UI{
panel,
tg.Messagef("").Reply( tg.Messagef("").Reply(
BackKeyboard.Reply(), BackKeyboard.Reply(),
), ),

100
paged-panel.go Normal file
View file

@ -0,0 +1,100 @@
package tg
type PanelPage struct {
Next, Prev bool
Rows []ButtonRow
}
type PanelPageFunc func(
c Context, page, size int,
) PanelPage
type PagedPanelCompo struct {
PanelCompo
page int
size int
nextFormat, prevFormat, delFormat string
fn PanelPageFunc
}
func (compo *MessageCompo) PagedPanel(
c Context,
startPage, size int,
fn PanelPageFunc,
) (*PagedPanelCompo) {
ret := &PagedPanelCompo{}
ret.page = startPage
ret.size = size
ret.fn = fn
ret.prevFormat = "<<<"
ret.nextFormat = ">>>"
ret.delFormat = "..."
ret.PanelCompo = (*compo.Panel(
c, ret,
))
return ret
}
func (panel *PagedPanelCompo) MakeRows(
c Context,
) []ButtonRow {
page := panel.fn(
c, panel.page, panel.size,
)
controlRow := ButtonRow{}
rows := page.Rows
next := func(c Context){
panel.page++
panel.Update(c)
}
prev := func(c Context){
panel.page--
panel.Update(c)
}
if page.Prev {
if panel.delFormat != "" {
rows = append(
[]ButtonRow{
ButtonRow{
Buttonf(panel.delFormat).Rand().
WithAction(Func(prev)),
},
},
rows...,
)
}
controlRow = append(
controlRow,
Buttonf(panel.prevFormat).Rand().
WithAction(Func(prev)),
)
}
if page.Next {
if panel.delFormat != "" {
rows = append(
rows,
ButtonRow{
Buttonf(panel.delFormat).Rand().
WithAction(Func(next)),
},
)
}
controlRow = append(
controlRow,
Buttonf(panel.nextFormat).Rand().
WithAction(Func(next)),
)
}
return append(
rows,
controlRow,
)
}