Accessing Toybox.ActivityMonitor.Info via a simple data field app

Hello,

I'm hoping someone can give me a steer as to how to access Toybox.ActivityMonitor.Info via a simple data field app.

Specific use case: 

Display activityInfo.timeToRecovery for use as a data field viewable during a workout on a Fenix6s.

The current code 
that I have is the following which builds to a blue screen in the simulator:

recoveryfieldview.mc:

import Toybox.Activity;
import Toybox.Lang;
import Toybox.Time;
import Toybox.WatchUi;
import Toybox.ActivityMonitor;

class recoveryfieldView extends WatchUi.SimpleDataField {

// Set the label of the data field here.
function initialize() {
SimpleDataField.initialize();
label = "timeToRecovery";
}

// The given info object contains all the current workout
// information. Calculate a value and return it in this method.
// Note that compute() and onUpdate() are asynchronous, and there is no
// guarantee that compute() will be called before onUpdate().
function compute(info as Activity.Info) as Numeric or Duration or String or Null {
}
onUpdate(){
// See Activity.Info in the documentation for available information.
var activityInfo = info as Toybox.ActivityMonitor.Info;
var timeToRecovery = activityInfo.timeToRecovery;
if (timeToRecovery == null) {
timeToRecovery = 0;
}
return timeToRecovery;
}

}

Any help or pointers would be greatly appreciated.

  • With a simple data field, onUpdate isn't called.  You need to use a complex data field for that.  With a simple one, all you can do is return what you want displayed in compute.

  • Thank you for the nudge. I've now made some changes, and tested on device. 

    Pleased to say it's working!

    If helpful for anyone here's the final code:

    import Toybox.Activity;
    import Toybox.Lang;
    import Toybox.Time;
    import Toybox.WatchUi;

    class rec_data_fieldView extends WatchUi.SimpleDataField {

    // Set the label of the data field here.
    function initialize() {
    SimpleDataField.initialize();
    label = "Rec Time (hrs)";
    }

    // The given info object contains all the current workout
    // information. Calculate a value and return it in this method.
    // Note that compute() and onUpdate() are asynchronous, and there is no
    // guarantee that compute() will be called before onUpdate().

    function compute(info as Activity.Info) as Numeric or Duration or String or Null {
    var timeToRecovery = ActivityMonitor.getInfo().timeToRecovery;
    if (timeToRecovery == null) {
    timeToRecovery = 0;
    }

    return timeToRecovery;

    }

    }