How to asign same value to different vars

Post your questions and help other users.

Moderator: Martin

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

How to asign same value to different vars

Post by Lucy » 27 Nov 2019 19:12

Ok... ima try to word this right!!
I have a simple little flow/widget with a app task/end trigger, that intercepts "app_name" (and other values depending on what vars i initiate for which task) from all and any apps that start/end and stores it as glovar, (Among doing other stuff). However if i log a particular action from that app using the glovar, it will essentially replace all previously stored values on the widget that was asigned to the last executed app.
Instead of having so many different glovars, flows, etc, how can i make the one glovar do the same task for all apps?
Ive been playing around a bit with the "get" function because i asumed that would be an idea but ultimately i just come up blank!

--EDIT--
Typically my main issue is with my app count.
Lol i probably should had opened with this rather than all that mumbo jumbo haha.
I cannot figure out how to asign my app count var to all apps but with different count attached to the corresponding app name.

--EDIT AGAIN--
This is my script with the counter.

Init script:

Code: Select all


   //APPLYING VALUES
global_app_name = app_name;
global_app_package = package_name;
global_transmitted_app_name = "";
global_received_app_name = "";
global_total_app_name = "";

   //START APP BYTES
global_atransmitted = getByteSize(transmitted_bytes);
global_areceived = getByteSize(received_bytes);
global_atotal = getByteSize(total_bytes);

  // START APP TIME
global_app_start_time = triggertime;

   //STORE DATE FOR 
 date = getDate();
 global_app_start_date =
 "{date,dateformat,EEEE, dd/MMM/yyyy, hh:mm:ss a}";

    //COUNTER
global_app_count = 
if(global_app_count == null) 1 
else global_app_count + 1;

Terminating script:

Code: Select all


   //APPLYING VALUES
global_app_name = app_name;
global_app_package = package_name;

    //END APP BYTES 
global_btransmitted = getByteSize(transmitted_bytes);
global_breceived = getByteSize(received_bytes);
global_btotal = getByteSize(total_bytes);

   //CALCULATING TOTAL BYTES
global_transmitted_runtime =
(global_btransmitted - global_atransmitted);
global_received_runtime =
(global_breceived - global_areceived);
global_total_runtime =
(global_btotal - global_atotal);

   //STORING TOTAL BYTES TO GLOVAR
global_transmitted =
getByteSizeString(global_transmitted_runtime);
global_received =
getByteSizeString(global_received_runtime);
global_total =
getByteSizeString(global_total_runtime);

   //APP END TRIGGERTIME
global_app_end_time = triggertime;

   //CALCULATING APP DURATION
global_app_runtime =
(global_app_end_time - global_app_start_time);

   //CONVERTING DURATION VALUE TO STRING
global_app_runtime_format =
getDurationString(global_app_runtime);

   //STORE DATE
 date = getDate();
 global_app_end_date =
 "{date,dateformat,EEEE, dd/MMM/yyyy, hh:mm:ss a}";


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

Re: How to asign same value to different vars

Post by Desmanto » 28 Nov 2019 17:57

This is still similar to the thread where I mention journal based logging. I just discovered in our excel files why we should use that method, instead of replacing the variable directly. Because journal based logging allow you to backtrack the log, it keeps all previous logging intact.

But maybe you only need something simpler, so replacing variable's value maybe not a problem here. In the past, for something like this, I would recommend creating a dynamic glovar. But it will spam too many unnecessary glovar and make it hard to handle all of them. But since I know we can use glovar map, it is much easier to use map instead. You only need 1 glovar for every apps you have. You create the keys for each app and increment them for every execution.

I don't know how you are going to implement it, since this will change most of your code. I just give you the example idea. Create a new glovar with map type and leave it blank. Then you will add the keys for every execution and the value as the count with this one line script

Code: Select all

global_app_count_test[package_name] = if(global_app_count_test[package_name] == null) 1 else global_app_count_test[package_name] + 1;
example case : at first the glovar map is blank. App com.abc is triggered. The script check for global_app_count_test["com.abc"], it is null. So it assign 1 to the value, while creating the key. Next time app com.xyz is triggered, the same happen, key com.xyz = 1. Then com.abc triggered again, this time, the key already exist. So instead of assigning 1 to it, it add 1, become 2. This will continue and populate every app you open to the glovar map key.

I use package_name, since it is defnitely unique. If you need the App name, you can easily use function getAppName(packageName).
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: How to asign same value to different vars

Post by Lucy » 29 Nov 2019 00:10

Yeah i remember the thread that you replied with that. I am very dumb when it comes to a lot of things unfortunately and i got confused a lil while reading. And more over with your brilliant sounding solution here... i got no idea how to do maps, keys, lists among other things which is why i script the simple stuff. I have tried and read tutorials but still way behind in the dark

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

Re: How to asign same value to different vars

Post by Desmanto » 01 Dec 2019 18:08

For above solution, simply copy that line and paste it to the flow which have the app count lines, which is the init part.

Later you can go to the glovar variable and study how it count all your apps.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: How to asign same value to different vars

Post by Lucy » 19 Dec 2019 04:35

Lol i tried using your example but it says something abour cannot convert null or something! Im just gonna make a hundred glovars🤪 to do the job. Thank you so much for your continued support

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: How to asign same value to different vars

Post by Lucy » 19 Dec 2019 04:42

Sorry i was a bit off on my error... here is a screenshot.
20191219_154104.jpg
20191219_154104.jpg (330.33 KiB) Viewed 15767 times

User avatar
jassing
Posts: 94
Joined: 16 Jul 2017 01:42
Location: SF Bay Area

Re: How to asign same value to different vars

Post by jassing » 19 Dec 2019 18:27

global_app_count_map is null... You're checking if a map element is null...

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: How to asign same value to different vars

Post by Lucy » 19 Dec 2019 21:25

Lol yeah i dont know what im doing obviously 😂🤦‍♀️ despite the explicit details given

User avatar
jassing
Posts: 94
Joined: 16 Jul 2017 01:42
Location: SF Bay Area

Re: How to asign same value to different vars

Post by jassing » 19 Dec 2019 22:42

We all learn at some point...
I prefer, and it is a personal preference, and find it more readable like this:

Code: Select all

if( global_app_count_map == null ) {
   global_app_count_map = newMap() ;
} 
global_app_count_map[ package_name ] = convertNull(global_app_count_map[ package_name ], 0) + 1;
Rather than using a lot of if statements... But everyone's different.

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: How to asign same value to different vars

Post by Lucy » 21 Dec 2019 06:33

Lol yeah im still sitting here with my hand to my forehead 🤦‍♀️ still clueless and cant work out what you guys are writing cause it errors if i use either and still errors if i change parts. I got no idea but did give it a good go over the past week or however long i have been trying. But in all seriousness... im just gonna make a global var for every app installed. That at least i do know how to do. If started a few but lol so many apps.. gonna take a lil bit😂 thank you darlings for all your help and efforts in teaching this poor sod

Post Reply