[Please note for some reason it would not allow me to post with many (but not all) brackets ( ) so I have had to delete them from the code and the text!!!]
using Toybox.WatchUi as Ui;
using Toybox.Graphics as Gfx;
using Toybox.System as Sys;
using Toybox.Lang as Lang;
class EDGEDataFieldView extends Ui.DataField {
var DistUnits;
function GetUnits {
return sys.getdevicesettings.distanceUnits.toNumber;
}
function compute info {
DistUnits = GetUnits;
if DistUnits == 1 {
AvgSpeed = Math.round info.averageSpeed * 22.3694;
} else {
AvgSpeed = Math.round info.averageSpeed * 36;
}
}
function onUpdate dc {
DistUnits = UserSettingsInfo.distanceUnits.toNumber;
}
I have found that I have to populate the DistUnits variable twice in the code and I have to do so in slightly different ways as you can see. If I call the GetUnits function from onUpdate it returns "method" instead of 0 or 1. And I cannot directly assign DistUnits within compute as both UserSettingsInfo.distanceUnits.toNumber() and sys.getdevicesettings().distanceUnits.toNumber() result in a Symbol Not Found Error or Unexpected Type Error respectively.
I understand (according to the notes in a new Data Field) that compute(info) is not guaranteed to be called prior to onUpdate so this means I would probably have to call twice anyway, or at least define a default value (other than null). What I don't understand is why I cannot call GetUnits from onUpdate (and why when I do or I don't call it at all I get "method"). Why is this so?
Also, it appears that my DistUnits variable is not global variable as if I don't assign a value in onUpdate, but do elsewhere, it fails its value is "method". Is it possible to define a truly global variable that can be assigned in one function and read in another? If so, how do I do this?