Keep implementing the path-like way for the screens. Should think about renaming the ScreenId to the Path or ScreenPath.

This commit is contained in:
Andrey Parhomenko 2023-09-21 00:24:42 +03:00
parent 88d4310db5
commit c96c2a7559
2 changed files with 65 additions and 0 deletions

View file

@ -106,6 +106,21 @@ var (
).Reply()
)
var theNode = tg.NewNode(
"/", tg.WidgetFunc(func(c *tg.Context){
c.Go("/start")
}),
tg.NewNode(
"start", tg.WidgetFunc(func(c *tg.Context){}),
tg.NewNode(
"profile", tg.WidgetFunc(func(c *tg.Context){}),
),
tg.NewNode(
"upper-case", tg.WidgetFunc(func(c *tg.Context){}),
),
),
)
var beh = tg.NewBehaviour().
WithInitFunc(func(c *tg.Context) {
// The session initialization.
@ -224,6 +239,8 @@ var gBeh = tg.NewGroupBehaviour().
)
func main() {
log.Println(theNode.ScreenMap())
return
token := os.Getenv("BOT_TOKEN")
bot, err := tg.NewBot(token)

View file

@ -13,6 +13,54 @@ type Screen struct {
Widget Widget
}
// The node is a simple way to represent
// tree-like structured applications.
type Node struct {
Screen *Screen
Subs []*Node
}
func NewNode(id ScreenId, widget Widget, subs ...*Node) *Node {
ret := &Node{}
ret.Screen = NewScreen(id, widget)
ret.Subs = subs
return ret
}
func (n *Node) ScreenMap() ScreenMap {
m := make(ScreenMap)
id := n.Screen.Id
m[id] = n.Screen
n.Screen.Id = id
var root ScreenId
if id == "/" {
root = ""
} else {
root = id
}
for _, sub := range n.Subs {
buf := sub.screenMap(root + "/")
for k, v := range buf {
m[k] = v
}
}
return m
}
func (n *Node) screenMap(root ScreenId) ScreenMap {
m := make(ScreenMap)
id := root+ n.Screen.Id
m[id] = n.Screen
n.Screen.Id = id
for _, sub := range n.Subs {
buf := sub.screenMap(id + "/")
for k, v := range buf {
m[k] = v
}
}
return m
}
// Map structure for the screens.
type ScreenMap map[ScreenId]*Screen