import Toybox.Graphics;
import Toybox.WatchUi;

class AntPlusTestAppSpeedView extends WatchUi.View {

    hidden var m_speed;
    hidden var m_antPlus;

    function initialize(ant_api) {
        View.initialize();
        m_speed = 0;

        m_antPlus = ant_api;
    }

    // Load your resources here
    function onLayout(dc as Dc) as Void {
        setLayout(Rez.Layouts.Speed_Layout(dc));
    }

    function OnTimerUpdate() {
        m_speed = m_antPlus.getSpeed();
        System.println("Speed: " + m_speed);
        WatchUi.requestUpdate();
    }

    // Called when this View is brought to the foreground. Restore
    // the state of this View and prepare it to be shown. This includes
    // loading resources into memory.
    function onShow() as Void {
        m_antPlus.startSensor(SPEED, self.method(:OnTimerUpdate));
    }

    // Update the view
    function onUpdate(dc as Dc) as Void {
        var spd_label = self.findDrawableById("spd");
        spd_label.setText(m_speed + " m/s");

        // Call the parent onUpdate function to redraw the layout
        View.onUpdate(dc);
    }

    // Called when this View is removed from the screen. Save the
    // state of this View here. This includes freeing resources from
    // memory.
    function onHide() as Void {
        m_antPlus.stopSensor(SPEED);
    }

}
