GarminWatchfaces/KittieCats/source/ComplicationSubscriber.mc

120 lines
3.2 KiB
MonkeyC

import Toybox.Complications;
import Toybox.Lang;
class ComplicationSnapshot
{
public var HeartRate as Lang.Numeric;
public var Steps as Lang.Numeric;
public var Stress as Lang.Numeric;
public var BodyBattery as Lang.Numeric;
function initialize(heartRate, steps, stress, bodyBattery)
{
self.HeartRate = heartRate;
self.Steps = steps;
self.Stress = stress;
self.BodyBattery = bodyBattery;
}
}
class ComplicationSubscriber
{
private static var instance = null;
private var heartRateComplicationId;
private var stepsComplicationId;
private var stressComplicationId;
private var bodyBatteryComplicationId;
private var heartRate = null;
private var steps = null;
private var stress = null;
private var bodyBattery = null;
public static function subscribe() as Void
{
ComplicationSubscriber.getInstance().internalSubscribe();
}
public static function getSnapshot() as ComplicationSnapshot
{
return ComplicationSubscriber.getInstance().internalGetSnapshot();
}
function internalSubscribe() as Void
{
self.heartRateComplicationId = new Id(Complications.COMPLICATION_TYPE_HEART_RATE);
self.stepsComplicationId = new Id(Complications.COMPLICATION_TYPE_STEPS);
self.stressComplicationId = new Id(Complications.COMPLICATION_TYPE_STRESS);
self.bodyBatteryComplicationId = new Id(Complications.COMPLICATION_TYPE_BODY_BATTERY);
Complications.registerComplicationChangeCallback(self.method(:onComplicationChanged));
Complications.subscribeToUpdates(self.heartRateComplicationId);
Complications.subscribeToUpdates(self.stepsComplicationId);
Complications.subscribeToUpdates(self.stressComplicationId);
Complications.subscribeToUpdates(self.bodyBatteryComplicationId);
}
function internalGetSnapshot() as ComplicationSnapshot
{
return new ComplicationSnapshot(
self.heartRate,
self.steps,
self.stress,
self.bodyBattery
);
}
function onComplicationChanged(complicationId as Complications.Id) as Void
{
var complication = Complications.getComplication(complicationId);
var complicationValue = complication.value;
switch (complicationId)
{
case self.heartRateComplicationId:
{
self.heartRate = complicationValue;
break;
}
case self.stepsComplicationId:
{
self.steps = complication.unit != null && complication.unit.equals("K") ? Math.floor(complicationValue * 1000) : complicationValue;
break;
}
case self.stressComplicationId:
{
self.stress = complicationValue;
break;
}
case self.bodyBatteryComplicationId:
{
self.bodyBattery = complicationValue;
break;
}
}
}
private static function getInstance()
{
if (instance == null)
{
instance = new ComplicationSubscriber();
}
return instance;
}
}