B4J Library [B4X] [XUI] ToastMessage class

Discussion in 'B4J Libraries & Classes' started by udg, Apr 29, 2018.

  1. udg

    udg Expert Licensed User

    Note: uploaded a newer class version (0.30) and its corresponding B4A/B4J demos; this one is much better (read post #6 for details)!

    Hi all,

    some time ago I wrote a class to port a TostMessageShow-alike method to B4J.
    Yesterday I decided it was time to convert it to B4x using XUI. While at it, I added methods here and there and some code to be able to use it with B4A and (hopefully) with B4i.

    Please find attached the class (clXToastMessage.bas) and working example code for B4J (tmtestJ.zip) and B4A (tmtest.zip). Note that both the zip files contain a copy of the class so you don't need to download it separately.

    I couldn't test it with B4i and, to say it all, I'm not even sure it will work with it "as is" because my coding followed snippets found on the Forum not any direct knowledge on my part. So please, amend it for B4i and post here the revised version.

    B4J: class uses JavaObject, JXUI and JFX
    B4A: class uses XUI and StringUtils
    B4i: I don't know; let me know.

    Well, it was a good exercise as a first approach to XUI. Maybe it will be of inspiration for further uses or it will be simply useful to collect hints about wrong code or component misuse..
    Let me know, I'm here to learn!


    Enjoy
     

    Attached Files:

    Last edited: Apr 30, 2018
    Erel, Johan Hormaza and XbNnX_507 like this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    This is not really needed:
    Code:
    Public Sub Initialize(Base As Object)
    #if B4A
       
    If (Base Is ActivityOr (Base Is PanelThen
           Available = 
    True
           mBase = Base 
    'f1
       End If
    #end if
    #if B4J
       If Base Is Form Then
           Available = True
           Dim f1 As Form = Base
           mBase = f1.RootPane
       End If
       If Base Is Pane Then
           Available = True
           mBase = Base
       End If
    #end if
    #if B4i
       If Base Is Page Then
           Available = True
           Dim f1 As Page = Base
           mBase = f1.RootPanel
       End If
       If Base Is Panel Then
           Available = True
           mBase = Base
       End If
    #end if
    The standard way to do it is to expect a B4XView and let the developer pass an Activity, MainForm.RootPane or Page1.RootPanel (or any other panel).
    Code:
    Public Sub Initialize(Base As B4XView)
     mBase = Base
    To make it work in B4i you need to add to Initialize:
    Code:
    mBase.Tag = Me
    And to remove this reference when you remove the view:
    Code:
    Private Sub timer1_tick
       timer1.Enabled = 
    False
       mPanel.RemoveViewFromParent
       mBase.Tag = 
    Null
    End Sub
    Otherwise it will not work with local toasts as the class instance will be released before the timer event.

    Note that the timer is not needed here.

    Code:
    Dim Duration As Int
    If LongDuration Then Duration = LDuration Else Duration = SDuration
    Sleep(Duration)
    mPanel.SetVisibleAnimated(
    500False)
    Sleep(
    500)
    mPanel.RemoveViewFromParent
    mBase.Tag = 
    Null
    This code also fades out the message.

    You can also add a fade in effect in the Initialize method:
    Code:
    mPanel.Visible = False
    mPanel.SetVisibleAnimated(
    500True)
     
    udg likes this.
  3. udg

    udg Expert Licensed User

    I was worried by the fact that, being the parameter an Object, anything could be thrown at it, but using a B4xView helps it since it will be the compiler to check.
    Fade in/out..nice idea. Would you suggest it as a standard behavior or just as an option?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    This is just a suggestion.
     
  5. udg

    udg Expert Licensed User

    Absolutely. It's a much appreciated suggestion and I'll include it in my next revision for sure. I asked because I don't know if I should set the fading as the only way to show a message or just as an option.
    I hope to have some time for programming at lunch time (meanwhile it's better that I show a notice on the first post..)
     
  6. udg

    udg Expert Licensed User

    Hi all,

    please find attached to post #1 a new version (rel 0.30) of the class clXToastMessage.
    It's based on Erel's hints from post #2

    New features:
    - optional fade in / fade out effect
    - queueing of Toast Messages
    - method TMClearAndShow to abruptly stop displaying message and then show a new one

    @Erel: please check the use of mBase.Tag since I feel it's not correct in this context (reusing same instance for several messages). TIA

    udg
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Change:
    Code:
    For i = 0 To m1.Size - 1
           
    If Not(tempSettings.ContainsKey(m1.GetKeyAt(i))) Then _
               tempSettings.Put(m1.GetKeyAt(i), m1.GetValueAt(i))
       
    Next
    To:
    Code:
    For Each key As Object In m1.Keys
           
    If tempSettings.ContainsKey(key) = False Then
               tempSettings.Put(key, m1.Get(key))
           
    End If
       
    Next
    There is no GetKeyAt / GetValueAt in B4i and For Each is preferred over those methods.

    Code:
    Private Sub DuplicateMap(Source As MapAs Map
       
    Dim m1 As Map
       m1.Initialize
       
    For Each key As Object In Source.Keys
           m1.Put(key, Source.Get(key))
       
    Next
       
    Return m1
    End Sub

    Remove this line:
    Code:
    mLbl.Font = DefaultFont
    There is no such variable and it is not needed.

    The usage of the mbase.Tag looks correct.
     
    udg likes this.
  8. LucaMs

    LucaMs Expert Licensed User

    Hi.
    I'm an Erel's cousin so... [kidding :D]

    I can not see where your are really using the tag of your class/instance. You set it in three places of your code but it seems to me that you are not using it to do something.

    I'm sure you're talking about this tag, not the one you use with the buttons.

    I have a (maybe) useful idea, to overload you with work :p:

    Why a Toast Message should not be a Custom View? So you could set some of its properties by Designer.
    I know, it sounds strange, but for example Timers in VB.Net are "UI objects".


    P.S. my cousin :D was writing his answer at the same time as mine.
    [Note that my joke about the cousin is due to the question addressed directly to Erel]
     
    Last edited: May 1, 2018
  9. udg

    udg Expert Licensed User

    Hi @Erel, I couldn't find any reference to "mLbl.Font = DefaultFont".
    Did you refer to the line "lbl.Font = DefaultTextFont" in the B4i section of sub ShowMessage?

    My concern with mbase.Tag was because we set it when we Initialize the class, but once the fade out for the first message is over we set it to Null. Now a a new call to TMShow/ShowMessage will find a null Tag (instead of a reference to "Me") as it happened for the first call.
    In my demo, ButtonX and ButtonP click events make use of a single Toast instance, while Button5 creates a new instance on every click.

    Am I missing something?

    TIA
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Yes.

    The Tag is not needed if the variable that holds the class instance is a global variable. Only if it is a local variable.

    The only case where it will be problematic is if you are using the same local instance multiple times.
     
  11. LucaMs

    LucaMs Expert Licensed User

    I still don't understand of which Tag you (both) are talking about.

    Searched as text (in the @udg's project):

    [​IMG]

    mBase.Tag is not really used, I think.

    b.Tag is just the tag of the buttons.

    :eek:
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice