Functions using eval() to format an AutoWear text screen

Share and discuss your flows and ideas with other users.

Moderator: Martin

Post Reply
Dawnshot7
Posts: 2
Joined: 05 Mar 2019 05:48

Functions using eval() to format an AutoWear text screen

Post by Dawnshot7 » 09 Apr 2019 05:56

Here I store two functions in a global variable and call them many times from a flow using eval().
The function parameters are passed by placing them in a list called "params" which is set each function call.
I use the "formatlist" and "fitonwatch" functions to take the contents of maps/lists stored in variables and display them with html formatting and bullets on my watch.

Example: Use voice commands to create lists and reminders using an AutoWear voice screen,
and immediately see items added to lists on an AutoWear text screen.

Call "formatlist" in any flow:

Code: Select all

outputlist=newList();headingcolor="red";keycolor="yellow";valuecolor="white";

//   Function call
params=newList(remindermap["Times"], "--Daily ", ".*daily.*", "null", "ALARMS", formattedkeys);  
eval(global_functions["formatlist"]);


//   FUNCTION "formatlist" defined elsewhere
//   Using the "formatlist" function in a flow:
//   Create a list called "params" that will be passed to the function
//   Your list or map will be params[0]
//   Prefix each bullet with params[1]
//   Only show elements containing params[2]
//   Omit elements containing params[3]
//   Use params[4] as a heading
//   Provide the map keys in params[5] if params[0] is a map
//      The map key will be put after the prefix and
//      before " - " and the value. Sort your map keys
//      before formatting them.
//   Set variables headingcolor, keycolor, and valuecolor as "red", "yellow", etc.

split_function = newList("",
"list=params[0];prefix=params[1];contain=params[2];",
"omit=params[3];heading=params[4];formatkeys=params[5];", 
"len=length(list);count=0;num=0;",
"if(len>0)if(heading!='') addElement(outputlist, '<font color='+headingcolor+'>'+heading+'</font>');", 
"while(count<len){", 
"   count=count+1;", 
"   if(length(formatkeys)>0){", //then list is a map
"      keys=sort(getMapKeys(list));key=keys[count-1];", 
"      formatkey=formatkeys[count-1]; thislistitem=replaceAll(list[key],'  ',' ');}", 
"   else{", 
"      thislistitem=list[count-1]; formatkey='';}", 
"   if(matches(thislistitem,contain) AND not matches(thislistitem, omit) AND not isMap(thislistitem)){", 
"      num=num+1; addElement(outputlist, '<font color='+keycolor+'>'+prefix+formatkey+'</font> - <font color='+valuecolor+'>'+thislistitem+'</font>');}}", 
"if(num==0 AND formatkeys!='') addElement(outputlist, prefix+'none');"); 

function="";
for(line in split_function) function=function+line;
addMapEntry(global_functions,"formatlist", function);


Call "fitonwatch" in any flow:

Code: Select all

//   Function call
params=newList(outputlist); 
eval(global_functions["fitonwatch"]);


//   FUNCTION "fitonwatch" defined elsewhere
//   After formatting your maps and lists using "formatlist", pass outputlist
//      as params[0] to this function

split_function = newList("",
"list=params[0]; outputstring='<blockquote><blockquote>';", 
"len=length(list); count=0;", 
"while(count<len){", 
"   thislistitem=list[count];", 
"   outputstring=outputstring+thislistitem+'<br />';", 
"   count=count+1;}",
"outputstring=outputstring+'</blockquote></blockquote>'"); 

function="";
for(line in split_function) function=function+line;
addMapEntry(global_functions,"fitonwatch", function);

Let me know if you have questions, or have a better way of doing this.
I switched my global variable lists to local variables for sharing, so it wouldn't clog up your global variable list.

http://automagic4android.com/flow.php?i ... bd163f61e1

User avatar
Desmanto
Posts: 1873
Joined: 21 Jul 2017 17:50

Re: Functions using eval() to format an AutoWear text screen

Post by Desmanto » 10 Apr 2019 06:28

My ticwatch E just arrived, I will try later when I setup it up already.

I love your tidy method to define the function. And your method is better, using list. I used to define function using string and have to add + "\n" + in each end (for better debug later). Using list, we can define the whole function in single variable. Then if we need to reuse some of the lines, instead of recreating it again, we can simply combine only the lines needed. Way better. I should use this method from now on.

To combine the lines, if you want to combine all at once, you simply use

Code: Select all

function = "{split_function,listformat}";
This will combine each line and automatically append \n (newline). That's why I thought your list method is better, since I can append all at once, no need extra \n in each line.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), Resurrection Remix Official 7.0.2 20190714, Pie 9.0, Rooted.

Dawnshot7
Posts: 2
Joined: 05 Mar 2019 05:48

Re: Functions using eval() to format an AutoWear text screen

Post by Dawnshot7 » 10 Apr 2019 20:08

Thanks, that's way cleaner! In my case I can just follow the list declaration with:

Code: Select all

addMapEntry(global_functions,"formatlist", "{split_function,listformat}");
If your Ticwatch doesn't let you set watch buttons to AutoWear pseudoapps (to trigger AutoWear command "&APPOPENEDDRIVE&") then go to Configure in the plugin in the flow and check the box for "Show now" to have it immediately show up instead of waiting for a watch button click.

I forgot to define global_functions=newMap(); here which you may need to use the flow. I have flow in which I put all of my function definitions, and run it manually once when I add a new one.

User avatar
Desmanto
Posts: 1873
Joined: 21 Jul 2017 17:50

Re: Functions using eval() to format an AutoWear text screen

Post by Desmanto » 11 Apr 2019 06:55

You can define the map key-value directly, it will be shorter if you don't need loop to add the key-value. So instead of

Code: Select all

userlists=newMap(); 
addMapEntry(userlists, "to do", newList("go to the store")); 
addMapEntry(userlists, "shopping", newList("milk")); 
You can define it using this

Code: Select all

userlists = newMapFromValues(
"to do", newList("go to the store"),
"shopping", newList("milk") );

And I don't quite use addMapEntry() anymore, I simply assign the key to the map, just like this.

Code: Select all

userlist["to do"] = newList("go to the store");
But of course this code has limitation for key that require additional concatenation from other string. But that rarely happen at mine, so I keep using this method until I face that problem and use AddMapEntry() for that specific case.


I don't use AutoWear, prefer to do it all using Automagic alone. I see the power of widget here, as I can map it to a lot of task and show a lot of information on the watch.


I see that you keep your functions in glovar map. I re commend you better move it to a text file stored inside Automagic folder and then init the file before using it. Yeah, it will be extra step, but that is still very fast. The reason is, if you store everything in glovar, it will slow down all of your flow. Check your glovar size (variables.bin at Automagic folder), make sure it is not bigger than 10 KB for best perfomance. As your glovar get bigger, you will notice your flow run much slower and slower. A simple toast message can delayed until can't be read before it timeout. (when my glovar is more than 100 KB).

I am creating a glovar manager flow prior this ticwatch arrived and haven't finished it. It seems I will be distracted again :D. My plan was to manage the glovar size, export/import to text file, detected any big glovar value especially with map/list type, sorting map, and many other function related to glovar.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), Resurrection Remix Official 7.0.2 20190714, Pie 9.0, Rooted.

Post Reply