Android Question B4XPages- Eliminate Redundant Subs in Two Pages

Mahares

Expert
Licensed User
Longtime User
I have 2 buttons with same name. One in B4XMain and one in Page1. Both click events are identical except for the reference to the B4XMainPage added in page1. Is there a way to do away with one of the subs. There is a label called lblDate also in each page layout:
B4X:
Public Sub btnDate_Click
    Wait For (Dialog.ShowTemplate(DateTemplate, "", "", "CANCEL")) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Log($"Selected Date is: ${DateTime.Date(DateTemplate.Date}"$ ))
        lblDate.Text=DateTime.Date(DateTemplate.Date)
        lblDate.TextColor =xui.Color_Yellow
    End If    
End Sub

'Below In Page1:
B4X:
Sub btnDate_Click
    Wait For (B4XPages.MainPage.Dialog.ShowTemplate(B4XPages.MainPage.DateTemplate, "", "", "CANCEL")) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Log($"Selected Date is: ${DateTime.Date(B4XPages.MainPage.DateTemplate.Date}"$ ))
        lblDate.Text=DateTime.Date(B4XPages.MainPage.DateTemplate.Date)
        lblDate.TextColor =xui.Color_Black
    End If
End Sub
Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
In main page:
B4X:
Public Sub SelectDate (lbl As B4XView)
   Wait For (Dialog.ShowTemplate(DateTemplate, "", "", "CANCEL")) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Log($"Selected Date is: ${DateTime.Date(DateTemplate.Date}"$ ))
        lbl .Text=DateTime.Date(DateTemplate.Date)
        lbl .TextColor =xui.Color_Yellow
    End If  
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Great.

Just a couple of things:

1 - it would be better if the routine was a Resumable
2 - to be able to call it from other pages, pass it Root and reinitialize the dialog
[do not set the label text color]
[It would be useful to pass the title as well]


B4XMainPage
B4X:
Public Sub SelectDate (Parent As B4XView, lbl As B4XView) As ResumableSub
    Dim SelDate As String = ""
    Dialog.Initialize(Parent)
    Dialog.Title = "Select date"

    Wait For (Dialog.ShowTemplate(DateTemplate, "OK", "", "CANCEL")) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        SelDate = DateTime.Date(DateTemplate.Date)
        Log($"Selected Date is: ${SelDate}"$)
        lbl.Text = SelDate
    End If

    Return SelDate
End Sub


Usage from inside B4XMainPage - with a log just to demonstrate that Resumable is better/needed:
B4X:
Sub btnDate_Click
    Wait For(SelectDate(Root, lblDate)) Complete(SelDate As String)
    Log("After selecting date")
End Sub


Usage from any other page - with a log just to demonstrate that Resumable is better/needed:
B4X:
Sub btnDate_Click
    Wait For(B4XPages.MainPage.SelectDate(Root, lblDate)) Complete(SelDate As String)
    Log("After selecting date")
End Sub
 

Attachments

  • TestDateDialog.zip
    17.5 KB · Views: 127
Last edited:
Upvote 0
Top