Android Code Snippet Save and load CSBuilders

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):
B4X:
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:
B4X:
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.
 

Jeffrey Cameron

Well-Known Member
Licensed User
If False Then
'to avoid obfuscation issues.
CallSub(Me, "Color")
CallSub(Me, "Append")
CallSub(Me, "Pop")
CallSub(Me, "PopAll")
End If
Could you explain why this is necessary, and why you called those four methods but not the "ToCharSequence" (or the to/from bytes) method?
 

Jeffrey Cameron

Well-Known Member
Licensed User
1. These four methods are called with CallSub from FromBytes.
And because they're being called as variable parameters, since they don't contain underscores, they would get obfuscated by default?
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.
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?
 

Erel

Administrator
Staff member
Licensed User
since they don't contain underscores, they would get obfuscated by default?
Yes.

And because .ToBytes and .ToCharSequence are being called from outside the class we don't need to worry about the name getting obfuscated?
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.
 

jmon

Well-Known Member
Licensed User
Top