Consider the following simple code that raises an alert every second time the compute function is called:
import Toybox.Activity; import Toybox.Graphics; import Toybox.Lang; import Toybox.WatchUi; class DataFieldAlertView extends WatchUi.DataFieldAlert { public function initialize() { DataFieldAlert.initialize(); } public function onUpdate(dc as Dc) as Void { dc.clear(); dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_BLACK); dc.drawText( dc.getWidth() / 2, dc.getHeight() / 2 - 30, Graphics.FONT_SMALL, "Alert!", Graphics.TEXT_JUSTIFY_CENTER); } } class DataField extends WatchUi.SimpleDataField { hidden var _counter as Number = 0; function initialize() { SimpleDataField.initialize(); } function compute(info as Info) { if ((WatchUi.DataField has :showAlert) and _counter == 0) { showAlert(new $.DataFieldAlertView()); } _counter++; if (_counter > 1) { _counter = 0; } return 1.0f; } }
The above code works fine.
Now change line 36 from if (_counter > 1) {
to if (_counter > 0) {
, (basically try to show the alert every time the compute function is called), and you get the following exception:
Error: Unhandled Exception Exception: Alert/Settings is active for current datafield Stack: - compute() at ...../SimpleDataFieldView.mc:32 0x10000375 Encountered app crash.
Here are a few questions I have:
- Is this a bug or a feature?
- How to properly mitigate the problem? Can I check if an Alert/Settings dialog is active and only if not, try to raise a new alert?
- If an Alert/Settings dialog is active, can I queue a new alert to show up when the previous one disappears?
- Will this exception only appear when an alert from my datafield is active? What if an alert from a different application that I cannot control triggered an alert?
- What is the type of the Exception raised in this example code? I couldn't find the Alert/Settings is active for current datafield
error message documented somewhere, so the only way to catch exception is to add a generic catch all try/catch statement (not ideal).
- Is there a way to print the type of an object during runtime, similar to how one would print an unknown type in python with print(type(obj))
?
- At https://developer.garmin.com/connect-iq/core-topics/native-controls/ the following is stated:
Since API level 3.2.0 When you want your data field to notify the user of a specific event, you can push a view that extends DataFieldAlert. DataFieldAlert is a special instance of View that can be presented to the user with showAlert. The alert does not accept input and will time out after the standard alert period. The user is required to enable alerts for your app in the workout alert settings.What is the the
standard alert period
that alerts are timed out? Is it configurable? Can it be changed somehow when initializing the DataFieldAlert?