Invoking Garmin function

hi everyone,

so I tried the invoke() function like the one on the doc for the sensors but for the ActivityMonitor.getInfo()

function getValue(opt){
		var W = [:steps,:calories,:floorsClimbed,:activeMinutesWeek,:activeMinutesDay];
		if(ActM.getInfo() has W[opt]){
			var gM = new Lang.Method(ActM.getInfo(),W[opt]);
			return gM.invoke();
		}
		return null;
	}

but it does not work,

I could do var value = [ActM.getInfo().steps, ... ]

but tell me if i'm wrong when value will be calculated it will calculate the whole array with all values?

the purpose is to avoid calculating value which is not needed.

have a great day.

  • A few things here....

    Let's say you have

    function myFun(a,b,c) {

    }

    that you want to use invoke with

    your code would be

    var fun=Method(:myFun);

    fun.invoke(a1,b1,c1);

    But there is more.  Looks like you're trying to call ActM.getInfo() with a parameter that's a symbol.  You are in effect trying to do

    ActM.getInfo(:steps);

    which will cause an error

    Then there are a couple of things to optimize things I'd suggest.

    you're calling ActM.getInfo() a number of times, where you really don't need to call it more than once per onUpdate() and then pass it, vs calling it each time the function is called, which takes me to the "has"

    When you do a "has", it's a search of the dictionary, where these things don't change while your app is running.

    What I do is have a class variable, say "var hasFloors;" and then I set it in in initialize():

    hasFloors==(ActM.getInfo() has :floorsClimbed);

    and then, just use the boolean.  It costs a bit memory wise for the boolean, but runs more efficiently (checking a boolean is faster than checking a dictionary).

    Also, as long as there is ActM, steps and calories are a given, so no need to even check those.

    On thing I think many miss is checking if Tracking is on:

    System.getDeviceSettings().activityTrackingOn;

    If it's false, no need to even worry about things like steps and calories, as they aren't being tracked.

  • oooooooooooh ok I get it!

    many thanks for those precious advices!

  • I'm going from memory here, but you should be able to do something like this if you want to access a member variable (as opposed to call a function):

    function getValue(opt) {
        var W = [:steps,:calories,:floorsClimbed,:activeMinutesWeek,:activeMinutesDay];
    
        var info = ActM.getInfo();
        if (info has W[opt]) {
            return info[W[opt]];
        }
        return null;
    }

  • That is why excactly I was looking for! many thanks.