Hi,
Communications.openWebPage triggers multiple times (up to about 20ish times) and I would like to know how to stop the re-trigger of it?
Is it because I put Communications.openWebPage in the onUpdate function? because openWebPage opens a new view on the watch prompting the user to check their phone which I feel like could be causing the multiple triggers of it.
import Toybox.Graphics;
import Toybox.Lang;
import Toybox.WatchUi;
import Toybox.Math;
import Toybox.Communications;
using Toybox.Application.Storage;
var pageNumber = 1;
var externalURL = "";
var blockNextPage = false;
class ScreenDelegate extends WatchUi.BehaviorDelegate {
public function initialize() {
BehaviorDelegate.initialize();
}
public function onSelect() as Boolean {
if (pageNumber == 2) {
return true;
}
var modeValue = Storage.getValue("modeKey");
if ((modeValue != null && modeValue.equals("true")) || blockNextPage == true) {
return true;
}
pageNumber = pageNumber + 1;
WatchUi.pushView(new $.ScreenViewDisplay("message"), new $.ScreenDelegate(), WatchUi.SLIDE_UP);
return true;
}
public function onBack() as Boolean {
if (pageNumber <= 1) {
pageNumber = 1;
blockNextPage = false;
return false;
}
pageNumber = pageNumber - 1;
WatchUi.popView(WatchUi.SLIDE_DOWN);
return true;
}
}
class ServiceFetchData {
function onReceive(responseCode as Number, data as Dictionary or String) as Void{
if (responseCode == 200) {
var deviceCode = Storage.getValue("deviceIdKey");
if (deviceCode == null) {
Storage.setValue("deviceIdKey", data["code"]);
}
if (data["status"].equals("true")) {
modeEnabled = true;
Storage.setValue("modeKey", "true");
}
externalURL = data["link"];
WatchUi.pushView(new $.ScreenViewDisplay("message"), new $.ScreenDelegate(), WatchUi.SLIDE_UP);
} else {
WatchUi.pushView(new $.ScreenViewDisplay("error"), new $.ScreenDelegate(), WatchUi.SLIDE_UP);
}
}
function makeRequest() {
var deviceCode = Storage.getValue("deviceIdKey");
if (deviceCode == null) {
deviceCode = "default";
}
var params = {
"action" => "retrieveLink",
"codeKey" => deviceCode
};
var options = {
:method => Communications.HTTP_REQUEST_METHOD_POST,
:headers => {
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON
},
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
};
Communications.makeWebRequest(url, params, options, method(:onReceive));
}
// Other methods related to the service can be added here
}
class ScreenViewDisplay extends WatchUi.View {
var font = fontSizeVar;
var modeValue;
var connectionStatus;
public function initialize(status as String) {
View.initialize();
connectionStatus = status;
modeValue = Storage.getValue("modeKey");
}
function createText(dc as Dc, textArray as String, yPos as Number) {
// Draw the text
dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_TRANSPARENT);
dc.drawText(dc.getWidth() / 2,
yPos,
font,
Graphics.fitTextToArea(textArray, font, dc.getWidth() - 40, dc.getHeight() - yPos, true),
Graphics.TEXT_JUSTIFY_CENTER);
}
public function onUpdate(dc as Dc) as Void {
dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_BLACK); // Set background color
dc.clear();
var titleFont = fontSizeTitle;
var titleFontHeight = Graphics.getFontHeight(titleFont);
var titleText = "Title";
var titleYPosition = 15;
dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_TRANSPARENT);
dc.drawText(dc.getWidth() / 2, titleYPosition, titleFont, titleText, Graphics.TEXT_JUSTIFY_CENTER);
var lineHeight = titleYPosition + titleFontHeight + 5;
dc.setColor(0x201E20, 0x201E20);
dc.setPenWidth(2.5);
dc.drawLine(0, lineHeight, dc.getWidth(), lineHeight);
var descriptionYPosition = lineHeight + 10;
if (modeValue != null && modeValue.equals("true")) {
createText(dc, "modeEnabledMessage", descriptionYPosition);
}
else {
if (pageNumber == 1) {
createText(dc, "pageOneText", descriptionYPosition);
}
else if (pageNumber == 2) {
var savedCode = Storage.getValue("deviceIdKey");
if (savedCode != null && !connectionStatus.equals("error")) {
Communications.openWebPage(
externalURL + savedCode,
null,
null
);
createText(dc, "completePurchaseMsg " + externalURL + savedCode, descriptionYPosition);
} else {
createText(dc, "errorOnSecondPageMsg", descriptionYPosition);
}
}
}
}
}