Android Code Snippet Save and load CSBuilders

Discussion in 'Code Snippets' started by Erel, Oct 15, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Using this class you can build CharSequences (the output of CSBuilder) and save the information required to build those styled strings. This allows to later load the information and create the same CharSequences.

    1. Create a class named PCSBuilder (Persistent CSBuilder):
    Code:
    Sub Class_Globals
       
    Private cs As CSBuilder
       
    Private data As List
       
    Private ser As B4XSerializator
    End Sub

    Public Sub Initialize As PCSBuilder
       cs.Initialize
       data.Initialize
       
    If False Then
           
    'to avoid obfuscation issues.
           CallSub(Me, "Color")
           
    CallSub(Me, "Append")
           
    CallSub(Me, "Pop")
           
    CallSub(Me, "PopAll")
       
    End If
       
    Return Me
    End Sub

    Public Sub FromBytes(b() As Byte) As PCSBuilder
       
    Dim list As List = ser.ConvertBytesToObject(b)
       
    For Each o() As Object In list
           
    If o.Length = 1 Then
               
    CallSub(Me, o(0))
           
    Else
               
    CallSub2(Me, o(0), o(1))
           
    End If
       
    Next
       
    Return Me
    End Sub

    Public Sub ToBytes As Byte()
       
    Return ser.ConvertObjectToBytes(data)
    End Sub

    Public Sub Color (clr As Int) As PCSBuilder
       cs.Color(clr)
       data.Add(
    Array("color", clr))
       
    Return Me
    End Sub

    Public Sub Append (Text As Object) As PCSBuilder
       cs.Append(Text)
       data.Add(
    Array("append", Text))
       
    Return Me
    End Sub

    Public Sub Pop As PCSBuilder
       cs.Pop
       data.Add(
    Array("pop"))
       
    Return Me
    End Sub

    Public Sub PopAll As PCSBuilder
       cs.PopAll
       data.Add(
    Array("popall"))
       
    Return Me
    End Sub

    Public Sub ToCharSequence As Object
       
    Return cs
    End Sub
    Usage example:
    Code:
    Dim p As PCSBuilder
    Activity.Title = p.Initialize.Append("hello ").Color(Colors.Red).Append("world").PopAll.ToCharSequence
    Dim b() As Byte = p.ToBytes
    Label.Text = p.Initialize.FromBytes(b).ToCharSequence
    Save and load bytes: [B4X] Bytes To File
    Or use KVS2.

    The class is not complete. You should add the other methods as well.
     
  2. Jeffrey Cameron

    Jeffrey Cameron Well-Known Member Licensed User

    Could you explain why this is necessary, and why you called those four methods but not the "ToCharSequence" (or the to/from bytes) method?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    1. These four methods are called with CallSub from FromBytes.
    2. We don't want these subs names to be obfuscated. We must help the compiler know that these subs are being called with CallSub. There are two options: add an underscore to the subs names or explicitly call them with CallSub and a string literal.
     
    jimmyF likes this.
  4. Jeffrey Cameron

    Jeffrey Cameron Well-Known Member Licensed User

    And because they're being called as variable parameters, since they don't contain underscores, they would get obfuscated by default?
    And because .ToBytes and .ToCharSequence are being called from outside the class we don't need to worry about the name getting obfuscated? What if we were compiling the class to a library and those routines were not actually called from anywhere in the project, would we need to add those two methods to the "If False" block in that event?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Yes.

    No, they will be obfuscated.
    We don't need to worry about these methods because they are not called with CallSub (with a dynamic string) so the compiler knows about the call and changes the calling code as needed.

    For further discussion please start a new thread.
     
  6. jmon

    jmon Well-Known Member Licensed User

    Maybe that could be a new feature of B4X:
    Code:
    #ignoreobfuscate: Color, Append
     
    alimanam3386 likes this.
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