B4J Question Can not get ABMbutton click event to work

Rob White

Member
Licensed User
Hi all,

This is sort of like my previous problems but this time AMButton

My page file is quit similar but I will include below, note extra log statements in Page_ParseEvent.

My problem is that I can not get the click sub to fire. Page_ParseEvent receives the event but can NOT find the sub. I have checked the event name and sub name but can not see the problem.

B4X:
'Class module
Sub Class_Globals
    Private ws As WebSocket 'ignore
    ' will hold our page information
    Public page As ABMPage
    ' page theme
    Private theme As ABMTheme
    ' to access the constants
    Private ABM As ABMaterial 'ignore   
    ' name of the page, must be the same as the class name (case sensitive!)
    Public Name As String = "Observ"  '<-------------------------------------------------------- IMPORTANT
    ' will hold the unique browsers window id
    Private ABMPageId As String = ""
    ' your own variables
    ' Any controls we need to pickup events from must be declared here.
    Dim cmbMobs As ABMCombo
    Dim cmbBeasts As ABMCombo
    Dim DP As ABMDateTimePicker
    Dim rbgroupB As ABMRadioGroup        ' Beast grouping
    Dim rbgroupO As ABMRadioGroup        ' observation type grouping
    Dim obsNotes As ABMInput
    Dim btnSave As ABMButton
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    ' build the local structure IMPORTANT!
    Main.currAction = uiUtils.getAction("Observation",Main.actions)
    
    BuildPage       
End Sub

#Region ABM
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)   
    Log("Connected")
        
    ws = WebSocket1       
    
    ABMPageId = ABM.GetPageID(page, Name,ws)
    
    Dim session As HttpSession = ABM.GetSession(ws, ABMShared.SessionMaxInactiveIntervalSeconds)
    If session.IsNew Then
        session.Invalidate
        ABMShared.NavigateToPage(ws, "", "./")
        Return
    End If
        
    If ABMShared.NeedsAuthorization Then
        If session.GetAttribute2("IsAuthorized", "") = "" Then
            ABMShared.NavigateToPage(ws, ABMPageId, "../")
            Return
        End If
    End If       
    ABM.UpdateFromCache(Me, ABMShared.CachedPages, ABMPageId, ws)       
    If page.ComesFromPageCache Then
        ' when we have a page that is cached it doesn't matter if it comes or not from a new connection we serve the cached version.
        Log("Comes from cache")       
        page.Refresh       
        page.FinishedLoading       
    Else
        If page.WebsocketReconnected Then
            Log("Websocket reconnected")
            ' when we have a client that doesn't have the page in cache and it's websocket reconnected and also it's session is new - basically when the client had internet problems and it's session (and also cache) expired before he reconnected so the user has content in the browser but we don't have any on the server. So we need to reload the page.
            ' when a client that doesn't have the page in cache and it's websocket reconnected but it's session is not new - when the client had internet problems and when he reconnected it's session was valid but he had no cache for this page we need to reload the page as the user browser has content, reconnected but we have no content in cache
            ABMShared.NavigateToPage (ws, ABMPageId, "./" & page.PageHTMLName)
        Else
            ' when the client did not reconnected it doesn't matter if the session was new or not because this is the websockets first connection so no dynamic content in the browser ... we are going to serve the dynamic content...
            Log("Websocket first connection")
            page.Prepare
            ConnectPage           
        End If
    End If
    Log(ABMPageId)       
End Sub

Private Sub WebSocket_Disconnected
    Log("Disconnected")
End Sub

Sub Page_ParseEvent(Params As Map)
    Dim eventName As String = Params.Get("eventname")
    Dim eventParams() As String = Regex.Split(",",Params.Get("eventparams"))
    
    Log($">>>>>>>>>>>>>>  ${eventName} - Raised"$)
    
    If eventName = "beforeunload" Then
        Log("preparing for url refresh")
        ABM.RemoveMeFromCache(ABMShared.CachedPages, ABMPageId)
        Return
    End If
    Dim caller As Object = page.GetEventHandler(Me, eventName)
    If caller = Me Then
        If SubExists(Me, eventName) Then
            Params.Remove("eventname")
            Params.Remove("eventparams")
            If eventName = "page_dropped" Then
                page.ProcessDroppedEvent(Params)
            End If
            Select Case Params.Size
                Case 0
                    CallSub(Me, eventName)
                Case 1
                    CallSub2(Me, eventName, Params.Get(eventParams(0)))
                Case 2
                    If Params.get(eventParams(0)) = "abmistable" Then
                        Dim PassedTables As List = ABM.ProcessTablesFromTargetName(Params.get(eventParams(1)))
                        CallSub2(Me, eventName, PassedTables)
                    Else
                        CallSub3(Me, eventName, Params.Get(eventParams(0)), Params.Get(eventParams(1)))
                    End If
                Case Else
                    ' cannot be called directly, to many param
                    CallSub2(Me, eventName, Params)
            End Select
        Else
            Log($"Sub ${eventName} NOT FOUND in Page_ParseEvent"$)
        End If
    Else
        CallSubDelayed2(caller, "ParseEvent", Params) 'ignore
    End If
End Sub

public Sub BuildTheme()
    ' start with the base theme defined in ABMShared
    theme.Initialize("pagetheme")
    theme.AddABMTheme(ABMShared.MyTheme)

    ' add your specific page themes
    
End Sub

public Sub BuildPage()
    ' initialize the theme
    Log("At Observ.BuildPage -------------------------------------------------------------------------")
    BuildTheme
    
    ' initialize this page using our theme
    page.InitializeWithTheme(Name, "/ws/" & ABMShared.AppName & "/" & Name, False, ABMShared.SessionMaxInactiveIntervalSeconds, theme)
    page.ShowLoader=True
    page.PageHTMLName = "index.html"
    page.PageTitle = "Observation"
    page.PageDescription = "Record observation on specific beast"
    page.PageKeywords = ""
    page.PageSiteMapPriority = ""
    page.PageSiteMapFrequency = ABM.SITEMAP_FREQ_YEARLY
        
    page.ShowConnectedIndicator = True
                
    ' adding a navigation bar
    'ABMShared.BuildNavigationBar(page, "My Page","../images/logo.png", "", "", "")
            
    ' create the page grid
#Region C:\CattleMan\V1\Cman\Grids\observ.abmgrid
    'PHONE
    '╔═══════════════════════════════════════════════════════════════════════════════════╗
    '║ 1,1                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════╣
    '║ 2,1                                     | 2,2                                     ║
    '╠═══════════════════════════════════════════════════════════════════════════════════╣
    '║ 3,1                              | 3,2                                     |      ║
    '╠═══════════════════════════════════════════════════════════════════════════════════╣
    '║ 4,1                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════╣
    '║ 5,1                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════╣
    '║ 6,1                                                          | 6,2                ║
    '╚═══════════════════════════════════════════════════════════════════════════════════╝

    'TABLET
    '╔═══════════════════════════════════════════════════════════════════════════════════════════════════════════╗
    '║ 1,1                                                                                                       ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 2,1                                                 | 2,2                                                 ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 3,1                                        | 3,2                                                 |        ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 4,1                                                                                                       ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 5,1                                                                                                       ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 6,1                                                                            | 6,2                      ║
    '╚═══════════════════════════════════════════════════════════════════════════════════════════════════════════╝

    'DESKTOP
    '╔═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
    '║ 1,1                                                                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 2,1                                                             | 2,2                                                             ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 3,1                                                  | 3,2                                                             |          ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 4,1                                                                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 5,1                                                                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 6,1                                                                                              | 6,2                            ║
    '╚═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝

    page.AddRows(1,True,"").AddCells12(1,"")
    page.AddRows(1,True,"").AddCellsOS(2,0,0,0,6,6,6,"")
    page.AddRows(1,True,"").AddCellsOS(1,0,0,0,5,5,5,"").AddCellsOS(1,0,0,0,6,6,6,"")
    page.AddRows(2,True,"").AddCells12(1,"")
    page.AddRows(1,True,"").AddCellsOS(1,0,0,0,9,9,9,"").AddCellsOS(1,0,0,0,3,3,3,"")
    page.BuildGrid ' IMPORTANT!
#End Region
    Log("Grid built .......................")
    
End Sub

public Sub ConnectPage()           
    '    connecting the navigation bar
    'ABMShared.ConnectNavigationBar(page)
    
    Dim lblTitle As ABMLabel
    lblTitle.Initialize(page, "lblobservtitle", ABMShared.TitleStr("Observation"), ABM.SIZE_H3, False, "lbltheme1")
    
    cmbMobs.Initialize(page,"cmbmobs","Select mob",500,"")
        
    If Main.allMobs.Size <= 0 Then
        DBUtils.loadMobs(DBUtils.sql1,Main.allMobs)
    End If
    uiUtils.fillMobsCombo(page,cmbMobs,Main.allmobs)
    cmbBeasts.Initialize(page,"cmbbeasts","Select beast",500,"")
    cmbBeasts.Enabled=False
    
    DP.Initialize(page,"dpdate",ABM.DATETIMEPICKER_TYPE_DATE,DateTime.Now,"Observation on","")
    DP.CancelText = "Cancel"
    DP.PickText = "Ok"
    DP.TodayText = "Today"
    DP.ClearText = ""
    DP.FirstDayOfWeek = ABM.FIRSTDAYOFWEEK_MONDAY
    DP.ReturnDateFormat = "DD/MMM/YYYY"
    DP.ReturnTimeFormat=""

    ' Beast groupings
    rbgroupB.Initialize(page, "rbgroupb", "")
    rbgroupB.Title = "Beast group (NOT implemented yet)"
    rbgroupB.AddRadioButtonNoLineBreak("All   ", True)
    rbgroupB.AddRadioButton("Cows   ", True)
    rbgroupB.AddRadioButtonNoLineBreak("Weaners   ", True)
    rbgroupB.AddRadioButton("Bulls", True)
    rbgroupB.SetActive(0)

    ' Observation type groupings
    rbgroupO.Initialize(page, "rbgroupo", "")
    rbgroupO.Title = "Observation type"
    rbgroupO.AddRadioButtonNoLineBreak("Birth", True)
    rbgroupO.AddRadioButton("General   ", True)
    rbgroupO.SetActive(1)

    ' Notes
    obsNotes.Initialize(page,"obsNotes",ABM.INPUT_TEXT,"Notes",True,"")
    obsNotes.Enabled=False
    
    btnSave.InitializeRaised(page, "btnsave", "", "", "Save", "bluegrey")
        
    page.Cell(1,1).AddComponent(lblTitle)
    page.Cell(2,1).AddComponent(cmbMobs)
    page.Cell(2,2).AddComponent(DP)
    page.Cell(3,1).AddComponent(cmbBeasts)
    page.Cell(3,2).AddComponent(rbgroupB)
    page.Cell(4,1).AddComponent(rbgroupO)
    page.Cell(6,1).AddComponent(obsNotes)
    page.Cell(6,2).AddComponent(btnSave)
    Log("End add components")
    ' Tell the browser we finished loading
    ' refresh the page
    page.Refresh
    page.FinishedLoading
    ' restoring the navigation bar position
    'page.RestoreNavigationBarPosition   
End Sub
#end region

Sub cmbMobs_clicked(itemId As String)
    Log($"cmbMobs click event itemID = ${itemId}"$)
    cmbBeasts.Enabled=True
    DBUtils.getBeastGrp(DBUtils.sql1,"MobID = " & itemId,Main.beastGroup)
    uiUtils.fillBeastsCombo(page,cmbBeasts,Main.beastGroup)

End Sub

Sub rbgroupo_Clicked(target As String)
    Log($"++++++++++ Inside rbgroupo_Clicked target = ${target}"$)
    
    setDefNote(rbgroupO.GetActive)
End Sub

Sub rbgroupb_Clicked(target As String)
    Log($"---------- Inside rbgroupb_Clicked target = ${target}"$)
    Log($"Active ID = ${rbgroupB.GetActive}"$)
End Sub

Sub setDefNote(ObsType As Int)
    Log($".......... Inside setDefNote(${ObsType})"$)
    Dim dateStr As String = DateTime.Date(DP.GetDate)
    Select Case ObsType
        Case 0
            obsNotes.Text = $"Birth of calf for ${Main.currBeast.EarTag} on ${dateStr} :- "$
        Case 1
            obsNotes.Text = $"General note for ${Main.currBeast.EarTag} made on ${dateStr} :- "$
    End Select
    obsNotes.Refresh
    
End Sub

Sub cmbBeasts_clicked(itemID As String)
    Log($"cmbBeasts click event itemID = ${itemID}"$)
    Main.currBeast = uiUtils.getBeastFromNLIS(itemID,Main.beastGroup)
    obsNotes.Enabled = True
    setDefNote(rbgroupO.GetActive)
End Sub

Sub butsave_clicked(Target As String)
    ' Observation
    Dim P As procedure
    P.Initialize
    P.ActivityID = Main.currAction.ID
    P.NLIS=Main.currBeast.NLIS
    P.Date=DP.GetDate
    P.Notes=obsNotes.Text
    P.Consumables=""
    P.Value="Observation"
    Log($"Saving procedure for beast NLIS = ${P.NLIS}"$)
    DBUtils.saveNewProc(P)
    Main.currBeast.NLIS = 0
End Sub


#Region ABMPage
' clicked on the navigation bar
Sub Page_NavigationbarClicked(Action As String, Value As String)
    ' saving the navigation bar position
    page.SaveNavigationBarPosition
    If Action = "LogOff" Then
        ABMShared.LogOff(page)
        Return
    End If

    ABMShared.NavigateToPage(ws, ABMPageId, Value)
End Sub

Sub Page_DebugConsole(message As String)
    Log("---> " & message)
End Sub
#end region

B4X:
>>>>>>>>>>>>>>  page_visibilitystate - Raised
Sub page_visibilitystate NOT FOUND in Page_ParseEvent
>>>>>>>>>>>>>>  btnsave_clicked - Raised
Sub btnsave_clicked NOT FOUND in Page_ParseEvent
Just to reinforce:-
Here is the sub call :- Sub butsave_clicked(Target As String)

What am I missing? Seems every control I try to use I have problems with the events!
 

Harris

Expert
Licensed User
I can't see the issue.

However, components are not usually defined in Sub_Globals.
These are declared (inited - with ID's) in the ConnectPage sub.

???
 

Harris

Expert
Licensed User
Seems to me you are over-complicating the design and flow. However - I am certainly no expert...

The button click should be raised - regardless. All seems declared properly.
But I can't understand what you do in the event... (although it should till raise it).
Button click events are simple and i NEVER have an issue unless improperly defined ( usually the ID misspelled ).

I am sure you have - since you are this far in ABM, but possibly review the dummies tut to see where this F'd up...

I take a simpler approach (cause I am simple minded) - whereas you (may) call many other methods within an event... Hard to debug.
 

Harris

Expert
Licensed User
Dim btnSave As ABMButton
My eyes have not yet seen the light... Constantly blurred as they are...
Working on a current project - mostly copy and paste from a previous - I DON'T ENCOUNTER ANY ISSUES. Tis the beauty of ABM.
 

Rob White

Member
Licensed User
OK problem solved! Thanks to Harris's post #4.
Turned out to be a null variable in my code. Looks like the safest thing to do is only have a single call to a sub in there.
I still find it curious that my error generated a sub not found error.

Thanks all I am only a week into ABM and 1 month into B4X so are very green.

ps How do you flag the issue as solved? I see this on some posts.
 
Top