From 86cfffc56fee61e4f3ff9a564fd46d5c6d87df9d Mon Sep 17 00:00:00 2001 From: surdeus Date: Sat, 16 Sep 2023 15:40:30 +0300 Subject: [PATCH] Finished the widgetizing. --- tg/command.go | 11 +++++------ tg/page.go | 2 +- tg/private.go | 9 +++++---- tg/widget.go | 17 ++++++++--------- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/tg/command.go b/tg/command.go index a0defec..d18c69c 100644 --- a/tg/command.go +++ b/tg/command.go @@ -166,7 +166,7 @@ func (widget *CommandWidget) Serve( commanders, ) - var cmdUpdates chan *Update + var cmdUpdates *UpdateChan for u := range updates.Chan() { if c.ScreenId() == "" && u.Message != nil { // Skipping and executing the preinit action @@ -189,15 +189,14 @@ func (widget *CommandWidget) Serve( c.Run(cmd.Action, u) if cmd.Widget != nil { - if cmdUpdates != nil { - close(cmdUpdates) - } - cmdUpdates := NewUpdateChan() + cmdUpdates.Close() + cmdUpdates = NewUpdateChan() go func() { cmd.Widget.Serve( &Context{context: c.context, Update: u}, cmdUpdates, ) + cmdUpdates.Close() cmdUpdates = nil }() } @@ -207,7 +206,7 @@ func (widget *CommandWidget) Serve( if cmdUpdates != nil { // Send to the commands channel if we are // executing one. - cmdUpdates <- u + cmdUpdates.Send(u) } else { c.Skip(u) } diff --git a/tg/page.go b/tg/page.go index 99c6aad..b6da7f2 100644 --- a/tg/page.go +++ b/tg/page.go @@ -112,7 +112,7 @@ func (p *Page) Serve( replyUpdates.Send(u ) case p.SubWidget != nil : if subFilterOk { - if subFilter.Filter(u, msgs) { + if !subFilter.Filter(u, msgs) { subUpdates.Send(u) } } else { diff --git a/tg/private.go b/tg/private.go index 4970ac3..a6af20c 100644 --- a/tg/private.go +++ b/tg/private.go @@ -119,19 +119,20 @@ func (c *Context) ChangeScreen(screenId ScreenId, args ...any) error { c.prevScreenId = c.screenId c.screenId = screenId - // Making the new channel for the widget. + // Stopping the current widget. c.skippedUpdates.Close() + // Making channel for the new widget. c.skippedUpdates = NewUpdateChan() if screen.Widget != nil { // Running the widget if the screen has one. go func() { + updates := c.skippedUpdates screen.Widget.Serve(&Context{ context: c.context, Update: c.Update, Args: args, - }, c.skippedUpdates) - - c.skippedUpdates.Close() + }, updates) + updates.Close() }() } else { panic("no widget defined for the screen") diff --git a/tg/widget.go b/tg/widget.go index 0339f94..8dc2c77 100644 --- a/tg/widget.go +++ b/tg/widget.go @@ -2,6 +2,7 @@ package tg import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + //"fmt" ) // Implementing the interface provides @@ -183,19 +184,17 @@ func (widget *InlineKeyboardWidget) Filter( u *Update, msgs MessageMap, ) bool { - if widget == nil { - return true - } - if u.CallbackQuery == nil || len(msgs) < 1 { + if widget == nil || u.CallbackQuery == nil { return true } inlineMsg, inlineOk := msgs[""] - if inlineOk { - if u.CallbackQuery.Message.MessageID != - inlineMsg.MessageID { - return true - } + if !inlineOk { + return true + } + if u.CallbackQuery.Message.MessageID != + inlineMsg.MessageID { + return true } return false