Wish A second way of shared classes

Cadenzo

Active Member
Licensed User
Longtime User
It is really a great way to develop cpp (Cross Platform Projects, I take this shortcut from Klaus) with shared classes.
But sometimes I have many small blocks of this #if B4A ... which makes the code not more readable. Also happens, that after developing the B4A version I test it in B4i (sometimes weeks later) and only now I see, that this will not work with B4i. Last case today: I have an InputStream, and .BytesAvailable is not supported in B4i.

So my Idea is, that the compiler could look for a fix named folder like ../../B4Xshared and in this folder (if exists) look for a class with same name as the intern class and than combine it like one class. Or the intern class has an "import" line for this.
Global objects, only used in one platform could be handled in the intern class part. This part could also hold special functions, or even overwrite the shared ones.
 

Cadenzo

Active Member
Licensed User
Longtime User
Why are you using BytesAvailable?
Not necessary, this was only one example. My point is, that everytime when I change code in a shared class, I should check it for every IDE, that uses it. But I am more productive, when working a day only for one platform. May be, in shared classes the codeeditor could include B4A and B4i rules?

The combined classes was just an idea, how to avoid this #if B4A ... blocks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The purpose of XUI, XUI Views and other cross platform libraries is to allow you to mostly write cross platform code.
If you do need to write platform specific code then #if blocks or separate classes are the solutions. There is no way to avoid it.

With the exception of platform specific features, other code should be cross platform.
 

Cadenzo

Active Member
Licensed User
Longtime User
I see, here is just a simple example, how it would look like in my idea:
a shared module now:
Sub Class_Globals
#if B4i
    Private hd As HUD
#end if
End Sub

Sub MyFunction
    '... doing someting
    
    ToastMessage("This is the result...", False)
End Sub

Sub ToastMessage(msg As String, lange As Boolean)
#if B4A
    ToastMessageShow(msg, lange)
#else if B4i
    hd.ToastMessageShow(msg, lange)
#end if
End Sub


when only a part is shared:
'the shared part:
Sub Class_Globals

End Sub

Sub MyFunction
    '... doing someting

    ToastMessage("This is the result...", False)
End Sub


'the B4A part:
Sub Class_Globals

End Sub

Sub ToastMessage(msg As String, lange As Boolean)
    ToastMessageShow(msg, lange)
End Sub


'the B4i part:
Sub Class_Globals
    Private hd As HUD
End Sub

Sub ToastMessage(msg As String, lange As Boolean)
    hd.ToastMessageShow(msg, lange)
End Sub
 

Cadenzo

Active Member
Licensed User
Longtime User
I think, I found a good way for me. I use (as always) a shared class like "GlobalClass" for all the shared code. Than I have another class like "GlobalClassPlus" for this about 5 % differences in every version (B4A, B4i), which I declare and initialize in the GlobalClass. All GlobalClassPlus has the same subs but with small differences in the code (polymorphism). I can access the subs in the GlobalClass and from everywhere else.
 

sorex

Expert
Licensed User
Longtime User
you will make things even more complex than needed by spreading sub over multiple classes.

what's wrong with a few #IFs in your main class?

when I look at one of my projects the difference I see are

(B4A vs B4i)
activity / page for main container (I always use a panel to work in instead of the activity/rootpanel but it needs to be added once on init so the init sub parameter is different)
phoneintents to open a site in an external browser / ios has a direct method
menu array differences as android can exit app it can have a close button extra / ios needs an extra restore purchases menu item if there are purchases
fonts declarations... it's easier with XUI but it's still slightly different
xui.fileuri which has a trailing slash on IOS but not on Android
webview _pageFinished has different paramaters
.enabled / .userInteractionEnabled
.invalidate after drawing is only needed on Android
share app differences

if you use some of the above several times you can just create a small sub for it and put the differences there
and the main calls can be identical for both (add empty parameters if needed to keep the sub the same).

without XUI it was a lot more (text alignment, label border, font size, font typeface to new a few)
 

Cadenzo

Active Member
Licensed User
Longtime User
Good collection, spontaneous I would add differences with Map and with SQL Transactions. I am not such experienced, please correct me, if I am wrong.

As the Cross Platform Projects aspect becomes more and more important, there should be a big place in the forum, to discuss the differences. I agree, that XUI removes many differences as good as it can be done, but we have still two different OS.
 

sorex

Expert
Licensed User
Longtime User
here's a post I wrote years ago about some differences I encountered when doing the first port from B4A to B4i


I'm not sure if some of them are already being solved in some way tho.
 

Cadenzo

Active Member
Licensed User
Longtime User
For SQL Transactions: in B4i no "EndTransaction"
SQL1.BeginTransaction - SQL1.TransactionSuccessful - [SQL1.EndTransaction]

For Maps here
Unlike B4J / B4A Maps, B4i Map does not retain the original order of items
 
Top