read.cash is a platform where you can earn money for your articles and comments. You can get paid upvotes
from other users or just earn points for writing articles and comments, which are converted daily to
Bitcoin Cash (BCH) cryptocurrency, which can be used on the Internet or converted to your local money.
Takes one minute, no documents required
Advanced BCH Monitoring with Tasker (Part 2: How to prepare an interface in tasker)
Our intended structure as mentioned in the previous article needs modularization so we can mutate it easily, adding new features, solving bugs or swapping between different services and all of it, keeping the code maintainable and readable.
Of course, we can create lots of independent tasks and call them as we need it, but that would clutter up our task list on relatively small projects, imagine on the big ones. For those cases, it's better to group tasks inside tasks so we can have a few bigger but well-sorted tasks instead of many unordered small tasks.
Yes, thanks to the Perform Task action we can pass up to two parameters to a task, combined with the Anchor action and the flow control capabilities of Tasker we can create callable functions inside the task.
Let's see this with an example:
figure 1 represents an example interface we are going to prepare, it will have a variable called IValue, and three sub-tasks: setValue, getValue and compute. We'll see about them later.
In order to prepare an interface, we will create for anchor actions, an anchor action is just an action that does nothing but serves both as a label to jump with control flow actions and to sort out code sections :
As for calling a task we use the Perform Task action and it does not give us the chance to set an anchor as the preferred entry point, we'll use this anchor to jump into the other anchors.
How do we prepare it?
The Perform Task action can be set up to two parameters that will be accessible inside the task as the %par1 and %par2 local variables. Our intent is to use the first parameter to point out which anchor we want to access and use the second parameter to pass the rest of the actual parameters.
What we want is to call IExample with, the first parameter set as "setValue" and having our control flow jump to that anchor, this snippet of pseudocode shows how are we going to do that with the Goto action:
We added the return to halt the task execution if we pass something unexpected on the first parameter, without it, that case would keep the flow running until the next action, usually with undesired behaviors.
On the figure 2, we can see how does this looks at the Task's action list and compare it to the upper code section. Keep it in mind, from now on I'll show code with the upper code section because as you can see, it's a more efficient way to show code.
The anchors that we are going to prepare are just getting the structure ready to start implementing.
setValue and compute will receive a single integer parameter so for preparing it we will set %par2 into the local variable %input_value. (note that tasker interprets any lowercase-named variable as local, and just having an uppercase character on a variable name turns it into a global)
getValue and compute will return an integer, so we will prepare a %retval variable and pass it into a Return action.
setValue returns nothing so we will have it return "SUCCESS" instead.
We are going to create two new classes copying the Task IExample in the project list and renaming it as SumExample and DivisionExample.
In both of them setValue will push the passed value to the %SumValue and %DivisionValue variables respectively and getValue will return the last pushed value.
The difference comes on compute that will run the operation that gives name to our examples, adding up its parameter on the SumExample class and dividing the parameter by the %DivisionValue variable on the DivisionExample class.