Many, many, many times when I create a cool new general purpose routine that I would like to share in multiple projects, I am often unable to place it in a code module if the routine has a "Wait For" in it because a code module is a static module that can't handle events.
For example, the below commonly used routine would be very handy to have in a code module so that I did NOT have to copy and paste it in each activity or service module that used it:
But, what if I could pass the calling activity to the routine so that the routine could then use it to handle the events?
For example, it would be cool if I could call the above routine from an activity and pass the activity as a parameter:
And then the routine (in the code module) would be designed to use the passed activity to handle the events (in the code module):
Being able to include routines (that can handle certain events) in code modules would drastically improve code reusability. (The reason why a Class module would not be very useful is that my code modules have many, many general purpose routines in it and I am trying to avoid having a bunch of separate include files)
Think of all the time it will save not having to duplicate or copy and paste a commonly used routine into an activity or service just because the routine uses events so it can't be placed in a shared code module.
Thoughts?
For example, the below commonly used routine would be very handy to have in a code module so that I did NOT have to copy and paste it in each activity or service module that used it:
B4X:
Sub CheckLocationPermission(Reason as String) As ResumableSub
If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
MsgboxAsync(Reason,"Location Permission") 'display meaningful reason to user before displaying cryptic android dialog
Wait For MsgBox_Result (Result As Int)
Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, AskResult As Boolean)
If AskResult = False Then
Return False
Else
Return True
End If
Else
Return True
End If
End Sub
But, what if I could pass the calling activity to the routine so that the routine could then use it to handle the events?
For example, it would be cool if I could call the above routine from an activity and pass the activity as a parameter:
B4X:
Wait for CodeModule.CheckLocationPermission("So this app can record your hiking trail, you need to grant permission on the next screen", Me) (Result as Boolean)
And then the routine (in the code module) would be designed to use the passed activity to handle the events (in the code module):
B4X:
Sub CheckLocationPermission(Reason as String, EventActivity as Activity) As ResumableSub
If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
MsgboxAsync(Reason,"Location Permission") 'display meaningful reason to user before displaying cryptic android dialog
Wait For MsgBox_Result (Result As Int) Using EventActivity
Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, AskResult As Boolean) Using EventActivity
If AskResult = False Then
Return False
Else
Return True
End If
Else
Return True
End If
End Sub
Being able to include routines (that can handle certain events) in code modules would drastically improve code reusability. (The reason why a Class module would not be very useful is that my code modules have many, many general purpose routines in it and I am trying to avoid having a bunch of separate include files)
Think of all the time it will save not having to duplicate or copy and paste a commonly used routine into an activity or service just because the routine uses events so it can't be placed in a shared code module.
Thoughts?
Last edited: