- DroidScript : PlusMin

DroidScript : PlusMin

Now suppose you work for a company that used to be state owned and got privatised. The CEO is aware of this and so is the workforce. But all intermediate layers of managers and foremen still thinks they are civil servants and they can control the universe just by issuing commands and rules.
Some egghead has calculated how long a certain task may take. And that time is your allowance. The trade unions however have stipulated that any man should be paid for what he worked. So the intermediate layers came up with an idea: if you needed more time than the allowance, you can plus it up. Or, if you were faster, you can minus it down. Yes, civil servants live on another planet.

The task at hand now is:

1. you record your starting time T0
2. you record your ending time T1
3. subtract T1 - T0 (modulo 60!)
4. subtract dT from allowance
5. if negative claim more time
6. if positive release time to employer
Steps c and d are error prone. 2:01 - 1.59 is not 42 minutes. It is 2 minutes. Now keep in mind that the majority of your colleagues has almost no education. And also keep in mind, that those with an education, returning home after 4 demanding jobs, taking over 8 hours to complete, are not in the mood for a series of Modulo 60 calculi. And then the plusMin app comes in handy.

Droidscript : PlusMin

Below is the source of PlusMin.js In the navigator you can read how to transfer files from the smartphone to a PC.

```function OnStart()
{
var lay = app.CreateLayout ("linear", "VCenter,FillXY");

edt1 = app.CreateTextEdit ("", -1, -1, "Right");
edt1.SetHint ("Starttijd");

edt2 = app.CreateTextEdit ("", -1, -1, "Right");
edt2.SetHint ("Stoptijd");

edt3 = app.CreateTextEdit ("", -1, -1, "Right");
edt3.SetHint ("Scenario");

var btn = app.CreateButton ("Berekenen");
btn.SetOnTouch (btn_OnTouch);

}

function makeMins (str)
{
var mins;
var arr = str.split (':');
mins = parseInt (arr [1]) + 60 * parseInt (arr [0]);
return mins;
}

function btn_OnTouch ()
{
var Tstart, Tstop, Tscen, Tdiff, tijd, mins, hrs;
Tstart = makeMins (edt1.GetText() );
Tstop  = makeMins (edt2.GetText() );
Tscen  = makeMins (edt3.GetText() );

Tdiff = Tstop - Tstart - Tscen;
if (Tdiff > 0)
{
tijd = 'Bijplussen : ';
} else {
tijd = 'Afminnen : ';
Tdiff = -Tdiff;
}
hrs  = Math.floor (Tdiff / 60);
mins = Tdiff % 60;

tijd += hrs + ':';
if (mins < 10) tijd += '0';
tijd += mins;

app.ShowPopup (tijd);
}
```

PlusMin : see the structure

Below is a breakdown of the structure of the 'program'. Every script contains at least one function:

`function OnStart()`
It is the MAIN loop. It's what in Oberon and Modula-2 is in the last BEGIN/END section. And depending on what the script is supposed to do, one or more handler functions (callbacks) are required. In this case there are two functions:
```function makeMins (str)
function btn_OnTouch ()
```
The former is a helper function, the latter is a callback.

function OnStart()

Below is the body of the main() function:

```function OnStart()
{
var lay = app.CreateLayout ("linear", "VCenter,FillXY");

edt1 = app.CreateTextEdit ("", -1, -1, "Right");
edt1.SetHint ("Starttijd");

edt2 = app.CreateTextEdit ("", -1, -1, "Right");
edt2.SetHint ("Stoptijd");

edt3 = app.CreateTextEdit ("", -1, -1, "Right");
edt3.SetHint ("Scenario");

var btn = app.CreateButton ("Berekenen");
btn.SetOnTouch (btn_OnTouch);

}
```
• A layout is defined to be linear and vcenter plus fillxy
• Three text entry fields are defined, each with a "hint"
• A button is defined and a callback handler is defined
• The layout is allocated to the app

The helper function 'makeMins (str)'

This function is here to transform a time string (hh:mm) into an integer (minutes). In Modula-2 this was quite a large function. In javascript it is very short:

```function makeMins (str)
{
var mins;
var arr = str.split (':');
mins = parseInt (arr [1]) + 60 * parseInt (arr [0]);
return mins;
}
```
First I split the time string in two parts and then I convert both number strings to one integer....

The callback function

In fact this is some kind of interrupt handler. It is defined in

`btn.SetOnTouch (btn_OnTouch);`
Never, ever, try
`btn.SetOnTouch (btn_OnTouch (2));`
If a handler has an argument, you get the stragest kinds of errors.

Since there is just one button, the callback processes the input and presents it to the user.

```function btn_OnTouch ()
{
var Tstart, Tstop, Tscen, Tdiff, tijd, mins, hrs;
Tstart = makeMins (edt1.GetText() );
Tstop  = makeMins (edt2.GetText() );
Tscen  = makeMins (edt3.GetText() );

Tdiff = Tstop - Tstart - Tscen;
if (Tdiff > 0)
{
tijd = 'Bijplussen : ';		// Claim
} else {
tijd = 'Afminnen : ';		// Refund
Tdiff = -Tdiff;
}
hrs  = Math.floor (Tdiff / 60);
mins = Tdiff % 60;

tijd += hrs + ':';
if (mins < 10) tijd += '0';
tijd += mins;

app.ShowPopup (tijd);
}
```
It's all quite straightforward:
• I define some local variables
• All text fields are converted to integers
• The difference time is calculated
• The sign of the result defines whether we claim or refund
• Turn the integer back into hh:mm time format
• Show the result in a popup
Is this programming? In this very case: yes. I'm a programmer so I make programs. The functions are programmed, the main routine is scripted. Many scripters who lack programmer's experience also script the functions... You will remember these words when you ever see a few functions created by a scripter...

Page created August 24, 2016 and