Class: Toybox::WatchUi::DataField

Inherits:
View show all

Overview

Note:

The system will call the onUpdate() method inherited from View when a Data Field is displayed by the system. Because compute() and onUpdate() are asynchronous, there is no guarantee that compute() will be called before onUpdate(). For this reason, variables should never be initialized in compute().

Create a Data Field.

A DataField is a special View that automatically provides Activity.Info once per second via the compute() method. A DataField requires the implementation of the compute() method to handle the incoming Activity info, and otherwise provides a significant amount of flexibility regarding the way the information is displayed. If less complexity is required, consider a SimpleDataField.

Examples:

A DataField that displays current heart rate

using Toybox.WatchUi;
using Toybox.Graphics;

class MyHRField extends WatchUi.DataField {

    hidden var hrValue;

    function initialize() {
        DataField.initialize();
        hrValue = 0.0f;
    }

    // Get the field layout
    function onLayout(dc) {
        View.setLayout(Rez.Layouts.MainLayout(dc));
    }

    // Calculate the data to display in the field here
    function compute(info) {
        if (info has :currentHeartRate) {
            if (info.currentHeartRate != null) {
                hrValue = info.currentHeartRate;
            } else {
                hrValue = 0.0f;
            }
        }
    }

    // Update the field layout and display the field data
    function onUpdate(dc) {
        View.findDrawableById("Background").setColor(getBackgroundColor());
        var value = View.findDrawableById("value");
        value.setColor(Graphics.COLOR_BLACK);
        value.setText(hrValue.format("%.2f"));
        View.onUpdate(dc);
    }
}

See Also:

Since:

  • 1.0.0

Supported Devices:

  • All devices

Direct Known Subclasses

SimpleDataField

Direct Known Subclasses

SimpleDataField

Constant Summary

OBSCURE_LEFT = 1

Some of the left edge of the device context is obscured

Since:

  • 1.0.0

OBSCURE_TOP = 2

Some of the top edge of the device context is obscured

Since:

  • 1.0.0

OBSCURE_RIGHT = 4

Some of the right edge of the device context is obscured

Since:

  • 1.0.0

OBSCURE_BOTTOM = 8

Some of the bottom edge of the device context is obscured

Since:

  • 1.0.0

Instance Method Summary collapse

Methods inherited from View

addLayer, clearLayers, findDrawableById, getLayerIndex, getLayers, insertLayer, onHide, onLayout, onShow, onUpdate, removeLayer, setKeyToSelectableInteraction, setLayout

Methods inherited from Lang::Object

equals, hashCode, method, toString, weak

Constructor Details

initializeDataField

Returns a new instance of DataField

Since:

  • 1.0.0


13873
13874
# File 'Monkeybrains.rb', line 13873

def initialize()
end

Instance Method Details

compute(info) ⇒ Object

Retrieve Activity.Info in a DataField.

This method is called once per second and automatically provides Activity.Info to the DataField object for display or additional computation. It is necessary to override compute() when implementing a DataField.

Examples:

function compute(info) {
    if (info has :currentHeartRate) {
        if (info.currentHeartRate != null) {
            hrValue = info.currentHeartRate;
        } else {
            hrValue = 0.0f;
        }
    }
}

Parameters:

See Also:

Since:

  • 1.0.0


13895
13896
# File 'Monkeybrains.rb', line 13895

def compute(info)
end

createField(name, fieldId, type, options) ⇒ Toybox::FitContributor::Field

Create a new custom FIT Field.

To record custom activity data to a FIT file, a new Field must first be created to allow Data Fields access to FIT recording without allowing access to the session. Once created, a Field is updated in the FIT file by changing the value of the data within the Field.

Parameters:

Options Hash (options):

  • :count (Toybox::Lang::Number)

    The number of elements to add to the field if it is an Array. This is also the maximum combined size of the Strings plus their null terminators if the Field type is FitContributor.DATA_TYPE_STRING (Default 1).

  • :mesgType (Toybox::Lang::Number)

    Optional. A FitContributor.MESG_TYPE_* value representing the Field message type. This defaults to FitContributor.MESG_TYPE_RECORD if not specified. Additionally, if FitContributor.MESG_TYPE_RECORD is used, the field type cannot be FitContributor.DATA_TYPE_STRING.

  • :units (Toybox::Lang::String)

    Optional. A String representing the display units (e.g. “mph”, “ft”, “Pa”)

  • :nativeNum (Toybox::Lang::Number)

    Optional. A FIT Profile field number when this Field is meant as an equivalent to a native field included in the FIT SDK

Returns:

See Also:

Since:

  • 1.3.0


14028
14029
# File 'Monkeybrains.rb', line 14028

def createField(name, fieldId, type, options)
end

getBackgroundColorToybox::Lang::Number

Get the current Data Field background color.

Some devices provide a global Data Field background color setting. This method will return the current value of that setting as a value of either Graphics.COLOR_WHITE or Graphics.COLOR_BLACK.

Returns:

See Also:

Since:

  • 1.2.0


13990
13991
# File 'Monkeybrains.rb', line 13990

def getBackgroundColor()
end

getObscurityFlagsToybox::Lang::Number

Get the screen regions that are obscured.

Non-rectangular screens have certain portions of the screen obscured. For example, a round screen effectively cuts off the corners of a square screen. This method returns a sum of the enumerated values defined by the WatchUi.DataField.OBSCURE_* constants that match the obscured screen regions on the device. Use of this method is only valid during the call to onUpdate().

Returns:

Since:

  • 1.0.0


13977
13978
# File 'Monkeybrains.rb', line 13977

def getObscurityFlags()
end

onNextMultisportLegObject

The device has transitioned to the next multisport leg.

This method is called when the device transitions to the next multipsport leg.

Since:

  • 3.0.0


13962
13963
# File 'Monkeybrains.rb', line 13962

def onNextMultisportLeg()
end

onTimerLapObject

A lap event has occurred.

This method is called when a lap is added to the current activity. A notification is triggered after the lap record has been written to the FIT file.

Since:

  • 1.3.0


13939
13940
# File 'Monkeybrains.rb', line 13939

def onTimerLap()
end

onTimerPauseObject

The activity timer is paused.

This method is called when the activity timer goes from a running state to a paused state. The paused state occurs when the auto-pause feature pauses the timer. If the activity timer is paused when the app is loaded, this event will run immediately after startup.

Since:

  • 1.3.0


13922
13923
# File 'Monkeybrains.rb', line 13922

def onTimerPause()
end

onTimerResetObject

The current activity has ended.

This method is called when the time has stopped and current activity is ended.

Since:

  • 1.3.0


13947
13948
# File 'Monkeybrains.rb', line 13947

def onTimerReset()
end

onTimerResumeObject

The activity time has resumed.

This method is called when the activity timer goes from a paused state to a running state.

Since:

  • 1.3.0


13930
13931
# File 'Monkeybrains.rb', line 13930

def onTimerResume()
end

onTimerStartObject

The activity timer has started.

This method is called when the activity timer goes from a stopped state to a started state. If the activity timer is running when the app is loaded, this event will run immediately after startup.

Since:

  • 1.3.0


13904
13905
# File 'Monkeybrains.rb', line 13904

def onTimerStart()
end

onTimerStopObject

The activity timer has stopped.

This method is called when the activity timer goes from a running state to a stopped state.

Since:

  • 1.3.0


13912
13913
# File 'Monkeybrains.rb', line 13912

def onTimerStop()
end

onWorkoutStepCompleteObject

The current workout step is complete.

This method is called when the a workout step has been completed.

Since:

  • 3.0.0


13954
13955
# File 'Monkeybrains.rb', line 13954

def onWorkoutStepComplete()
end