Journey of a first time developer: Need help implementing excludeAnnotations

Journey of a first time developer: Need help implementing excludeAnnotations 

Hey all,

I'm still building my first WatchFace and I have been building different resolutions and can say the WatchFace is working on all devices using API 3.0 and above.

While trying to run the watchface on my old Vivoactive HR I realized that it was failing to load as it does not have var Calories = ActivityMonitor.getInfo().calories.toString(); available.

Does anyone have a Code Sample that I can use in the Monkey.Jungle to exclude Device and Device Families? 

  • Vivoactive HR Resource Folder: resources-rectangle-148x205

  • Code That needs to be excluded :
  • var Calories = ActivityMonitor.getInfo().calories.toString();
    var CalorieCountDisplay = View.findDrawableById("CalorieCountDisplay");
    CalorieCountDisplay.setText(Calories);

I thought it should look like this but it's clear that it's still calling the Calorie lines:

  • rectangle-148x205.resourcePath = $(rectangle-148x205.resourcePath);resources-rectangle-148x205
    rectangle-148x205.excludeAnnotations = var Calories

Can anyone save me again?

  • Actually, you got something else going on as ActivityMonitor.getInfo(),calories has been around since CIQ 1.0.0.  What you're likely running into is the value is null.  Do a null check.  Null is a possible value per the API doc, and if it is null, your toSting() will crash.

    You may also want to check System.getDeviceSettings().activityTrackingOn, as if it's false, a number of things in ActivityMonitor may not change or be available.

    For some things, you may want to look at using "has".  For example, while the fr245 is 3,x, it doesn't have a baro altimiter and therefore doesn't have floors.  Same with the venu sq.  

  • You're correct as always! I added this and it worked, did I completely butcher this or does it check out?

    		if (ActivityMonitor.getInfo().calories != null) 
    		{
    		var Calories = ActivityMonitor.getInfo().calories.toString(); 
    		var CalorieCountDisplay = View.findDrawableById("CalorieCountDisplay");      
    		CalorieCountDisplay.setText(Calories);
    		}
    		
    		if (ActivityMonitor.getInfo().calories == null) 
    		{
    		var CalorieCountDisplay = View.findDrawableById("CalorieCountDisplay");      
    		CalorieCountDisplay.setText(" ");
    		}

  • Instead of two if statements I probably would have done

    if (ActivityMonitor.getInfo().calories != null) {

    } else {

    }

  • Got it! 

    Added to the list of Jim tips!

    		if (ActivityMonitor.getInfo().calories != null) 
    		{
    		var Calories = ActivityMonitor.getInfo().calories.toString(); 
    		var CalorieCountDisplay = View.findDrawableById("CalorieCountDisplay");      
    		CalorieCountDisplay.setText(Calories);
    		} 
    		else 
    		{
    		var CalorieCountDisplay = View.findDrawableById("CalorieCountDisplay");      
    		CalorieCountDisplay.setText(" ");
    		}

  • FYI, you can simplify this a bit further. In general, this kind of simplification will make your code easier to read and maintain, among other benefits.

    var calories = ActivityMonitor.getInfo().calories;
    if (calories == null) {
        calories = "";
    } else {
        calories = calories.toString();
    }
    
    View.findDrawableById("CalorieCountDisplay").setText(calories);

    If you find yourself checking values for null quite often, before converting them to strings, you could also make a separate helper function to avoid repeating the if-else pattern above.

    function toString(val) {
        if (val == null) {
            val = "";
        } else {
            val = val.toString();
        }
        return val;
    }
    
    //...
    
    var calories = ActivityMonitor.getInfo().calories;
    View.findDrawableById("CalorieCountDisplay").setText(toString(calories));
    

  • Hey Thank You! Your Calorie check is way better than my beginner stuff! 

    For the second example if I understand it correctly it would be checking for Null whenever I call a toString. What does the "val" stand for?

  • it's his own version of toString() where what you want to be a string is passed as a parameter.

    The normal toString is used like this:  var result=val.toString() while his function, var result=toString(val)

    in this case you could just use:

    var result=(val!=null) ? val.toString() : "";