From the beginning of Block Calculator design, I chose to rely on a recursive algorithm approach. It was very convenient for drawing the formula and computing their results.
One definition of recursivity, in the realm of informatics is a "method of defining a sequence of objects, such as an expression, function, or set, where some number of initial objects are given and each successive object is defined in terms of the preceding objects."
In my application, I would define recusrsivity as the execution of a subprocess as part of a "greater" process, with both having a similar code basis albeit given different parameters.
An easy way to visualize a recursive algorithm is to draw it as a tree. Recursive "processes" can be observed throughout nature, as plants and trees splits in successive branches with similar or specific functions for the whole organism. Trees are also often used to draw genealogical trees. Thus could we think successive generations of humans as a recursive process, each with the function of procreation inherited from its genitors with the goal of perpetuating the specie? I see one key difference though with recursive algorithms from informatics : subprocesses have to give a feedback to the greater process, while we humans do not have to necessarily give accounts to our parents, and can outlive them. But I digressn :)
Using the Object-Oriented Programming (OOP) approach, I defined an abstract "block" as a base class for all types of blocks such as digits, operations, functions etc. Each of these blocks can contain any number of blocks as child objects. To compute the result of a formula, each block has a function compute() defined, and call the function on all its children.
Figure 3 : representation in a tree of the blocks composing the formula presented in figure 1. The computation process is started from the top, and each block asks for a result value to its children, before applying its own operation. Once the top level block gets a result, it is transfered to the result block for display.
To draw and save the formulas, I took advantage of the Android API class bundle. A bundle is a data class used by android to save and transfer data when putting the application (activity) in background. The bundle can be used as recursive structure, as Bundles can be placed inside a Bundle. Each block has then a function to acquire such a Bundle describing the structure of the formula including all children blocks from this block. When drawing a formula, the associated bundle is explored from top to bottom, starting from a given font size and gradually reducing it the deeper we go. This allow to know the size of the children blocks and correctly position them in a second phase, exploring the tree from bottom to top.
To save, the bundle is converted to a xml file, automatically converting any key/value pair it contains to an argument/value pair inside an xml tag. The name of the block classes is also saved in the file, so that, using reflection, the correct block class can be instantiated when loading the saved file.
Relying on recursivity guided a lot of the design of the app. The extent of the recursivity in Block Calculator is virtually infinite. That is why a zoom function was handy to implement, as the font of children blocks deep in the tree could get very small. The tree structure of Bundle made it also very easy to extract part of the formulas as subtrees, leading to the drag and drop capabilities and the drop-in memory area.