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