Added callback function to TemplateMenuItem

This commit is contained in:
Philip Abbey
2024-08-25 18:53:32 +01:00
parent ea32d71a2b
commit 1dc95eeac7
3 changed files with 15 additions and 5 deletions

View File

@ -37,6 +37,8 @@ class HomeAssistantGroupMenuItem extends TemplateMenuItem {
TemplateMenuItem.initialize( TemplateMenuItem.initialize(
definition.get("name") as Lang.String, definition.get("name") as Lang.String,
template, template,
// Now this feels very "closely coupled" to the application, but it is the most reliable method instead of using a timer.
getApp().method(:updateNextMenuItem),
icon, icon,
options options
); );

View File

@ -47,6 +47,8 @@ class HomeAssistantTemplateMenuItem extends TemplateMenuItem {
TemplateMenuItem.initialize( TemplateMenuItem.initialize(
label, label,
template, template,
// Now this feels very "closely coupled" to the application, but it is the most reliable method instead of using a timer.
getApp().method(:updateNextMenuItem),
icon, icon,
options options
); );

View File

@ -9,7 +9,7 @@
// tested on a Venu 2 device. The source code is provided at: // tested on a Venu 2 device. The source code is provided at:
// https://github.com/house-of-abbey/GarminHomeAssistant. // https://github.com/house-of-abbey/GarminHomeAssistant.
// //
// P A Abbey & J D Abbey, 12 January 2024 // P A Abbey & J D Abbey, 24 August 2024
// //
// //
// Description: // Description:
@ -28,10 +28,13 @@ using Toybox.Graphics;
class TemplateMenuItem extends WatchUi.IconMenuItem { class TemplateMenuItem extends WatchUi.IconMenuItem {
private var mTemplate as Lang.String; private var mTemplate as Lang.String;
private var mCallback as Method() as Void;
function initialize( function initialize(
label as Lang.String or Lang.Symbol, label as Lang.String or Lang.Symbol,
template as Lang.String, template as Lang.String,
// Do not use Lang.Method as it does not compile!
callback as Method() as Void,
icon as Graphics.BitmapType or WatchUi.Drawable, icon as Graphics.BitmapType or WatchUi.Drawable,
options as { options as {
:alignment as WatchUi.MenuItem.Alignment :alignment as WatchUi.MenuItem.Alignment
@ -46,6 +49,7 @@ class TemplateMenuItem extends WatchUi.IconMenuItem {
); );
mTemplate = template; mTemplate = template;
mCallback = callback;
} }
// Callback function after completing the GET request to fetch the status. // Callback function after completing the GET request to fetch the status.
@ -117,8 +121,9 @@ class TemplateMenuItem extends WatchUi.IconMenuItem {
setSubLabel(WatchUi.loadResource($.Rez.Strings.TemplateError) as Lang.String); setSubLabel(WatchUi.loadResource($.Rez.Strings.TemplateError) as Lang.String);
} }
requestUpdate(); requestUpdate();
// Now this feels very "closely coupled" to the application, but it is the most reliable method instead of using a timer. if (mCallback != null) {
getApp().updateNextMenuItem(); mCallback.invoke();
}
break; break;
default: default:
@ -131,8 +136,9 @@ class TemplateMenuItem extends WatchUi.IconMenuItem {
function getState() as Void { function getState() as Void {
if (mTemplate == null) { if (mTemplate == null) {
// Nothing to do here. // Nothing to do here.
// Now this feels very "closely coupled" to the application, but it is the most reliable method instead of using a timer. if (mCallback != null) {
getApp().updateNextMenuItem(); mCallback.invoke();
}
} else { } else {
if (! System.getDeviceSettings().phoneConnected) { if (! System.getDeviceSettings().phoneConnected) {
// System.println("HomeAssistantTemplateMenuItem getState(): No Phone connection, skipping API call."); // System.println("HomeAssistantTemplateMenuItem getState(): No Phone connection, skipping API call.");