Class: Toybox.WatchUi.DataField

Overview

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.

See Also:

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().

Example:

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);
    }
}

Since:

API Level 1.0.0

Supported Devices:

Direct Known Subclasses

WatchUi.SimpleDataField

Constant Summary

Obscurity

Since:

API Level 1.0.0

Name Value Since Description See Also
OBSCURE_LEFT 1

API Level 1.0.0

Some of the left edge of the device context is obscured

OBSCURE_TOP 2

API Level 1.0.0

Some of the top edge of the device context is obscured

OBSCURE_RIGHT 4

API Level 1.0.0

Some of the right edge of the device context is obscured

OBSCURE_BOTTOM 8

API Level 1.0.0

Some of the bottom edge of the device context is obscured

Instance Method Summary collapse

Instance Method Details

compute(info as Activity.Info)

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.

Parameters:

Example:

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

See Also:

Since:

API Level 1.0.0

createField(name as Lang.String, fieldId as Lang.Number, type as FitContributor.DataType, options as { :count as Lang.Number, :mesgType as FitContributor.MessageType, :units as Lang.String }) as 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:

  • name(Lang.String)

    The name of the Field

  • fieldId(Lang.Number)

    The unique Field identifier of the Field

  • type(FitContributor.DataType)

    A FitContributor.DATA_TYPE_* value representing the type definition of the Field

  • options(Lang.Dictionary)

    Optional parameters for field creation

    • :count(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(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(Lang.String)

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

    • :nativeNum(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:

API Level 1.3.0

getBackgroundColor() as Graphics.ColorType

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:

API Level 1.2.0

getObscurityFlags() as DataField.Obscurity

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:

API Level 1.0.0

initialize()

Constructor

Since:

API Level 1.0.0

onNextMultisportLeg() as Void

The device has transitioned to the next multisport leg.

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

Supported Devices:

Since:

API Level 3.0.0

onTimerLap() as Void

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.

Supported Devices:

Since:

API Level 1.3.0

onTimerPause() as Void

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.

Supported Devices:

Since:

API Level 1.3.0

onTimerReset() as Void

The current activity has ended.

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

Supported Devices:

Since:

API Level 1.3.0

onTimerResume() as Void

The activity time has resumed.

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

Supported Devices:

Since:

API Level 1.3.0

onTimerStart() as Void

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.

Supported Devices:

Since:

API Level 1.3.0

onTimerStop() as Void

The activity timer has stopped.

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

Supported Devices:

Since:

API Level 1.3.0

onWorkoutStarted() as Void

The current workout is started.

This method is called when the a workout is started.

Supported Devices:

Since:

API Level 3.2.0

onWorkoutStepComplete() as Void

The current workout step is complete.

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

Supported Devices:

Since:

API Level 3.0.0

showAlert(alertView as WatchUi.DataFieldAlert) as Void

Show an alert.

Parameters:

Supported Devices:

Since:

API Level 3.2.0

Throws:


Generated Apr 17, 2024 9:40:39 AM