Refactored MenuItems creation

This commit is contained in:
SomeoneOnEarth
2023-11-17 01:29:57 +01:00
parent 081a41737f
commit d93aa78686
3 changed files with 120 additions and 67 deletions

View File

@ -0,0 +1,105 @@
//-----------------------------------------------------------------------------------
//
// Distributed under MIT Licence
// See https://github.com/house-of-abbey/GarminHomeAssistant/blob/main/LICENSE.
//
//-----------------------------------------------------------------------------------
//
// GarminHomeAssistant is a Garmin IQ application written in Monkey C and routinely
// tested on a Venu 2 device. The source code is provided at:
// https://github.com/house-of-abbey/GarminHomeAssistant.
//
// P A Abbey & J D Abbey & SomeoneOnEarth, 17 November 2023
//
//
// Description:
//
// MenuItems Factory.
//
//-----------------------------------------------------------------------------------
using Toybox.Application;
using Toybox.Lang;
using Toybox.WatchUi;
class HomeAssistantMenuItemFactory {
private var mRightLabelAlignement;
private var mLabelToggle;
private var strMenuItemTap;
private var bLeanDesign;
private var mTapIcon;
private static var instance;
private function initialize() {
mLabelToggle = {
:enabled => WatchUi.loadResource($.Rez.Strings.MenuItemOn) as Lang.String,
:disabled => WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String
};
bLeanDesign = Application.Properties.getValue("lean_ui") as Lang.Boolean;
mRightLabelAlignement = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT};
strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap);
mTapIcon = new WatchUi.Bitmap({
:rezId=>$.Rez.Drawables.TapIcon
});
}
static function create() {
if (instance == null) {
instance = new HomeAssistantMenuItemFactory();
}
return instance;
}
function toggle(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null) as WatchUi.MenuItem{
var subLabel = null;
if (bLeanDesign == false){
subLabel=mLabelToggle;
}
return new HomeAssistantToggleMenuItem(
label,
subLabel,
identifier,
false,
null
);
}
function tap(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null, service as Lang.String or Null) as WatchUi.MenuItem{
if (bLeanDesign) {
return new HomeAssistantIconMenuItem(
label,
null,
identifier,
service,
mTapIcon,
mRightLabelAlignement
);
} else {
return new HomeAssistantMenuItem(
label,
strMenuItemTap,
identifier,
service,
null
);
}
}
function group(definition as Lang.Dictionary) as WatchUi.MenuItem{
if (bLeanDesign) {
return new HomeAssistantViewIconMenuItem(definition);
} else {
return new HomeAssistantViewMenuItem(definition);
}
}
}

View File

@ -28,7 +28,6 @@ class HomeAssistantView extends WatchUi.Menu2 {
// List of items that need to have their status updated periodically // List of items that need to have their status updated periodically
hidden var mListToggleItems = []; hidden var mListToggleItems = [];
hidden var mListMenuItems = []; hidden var mListMenuItems = [];
hidden var mListIconMenuItems = [];
function initialize( function initialize(
definition as Lang.Dictionary, definition as Lang.Dictionary,
@ -39,17 +38,6 @@ class HomeAssistantView extends WatchUi.Menu2 {
} or Null } or Null
) { ) {
var toggle_obj = null;
var strMenuItemTap = null;
if ((Application.Properties.getValue("lean_ui") as Lang.Boolean) == false){
toggle_obj = {
:enabled => WatchUi.loadResource($.Rez.Strings.MenuItemOn) as Lang.String,
:disabled => WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String
};
strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap);
}
if (options == null) { if (options == null) {
options = { options = {
:title => definition.get("title") as Lang.String :title => definition.get("title") as Lang.String
@ -67,71 +55,31 @@ class HomeAssistantView extends WatchUi.Menu2 {
var service = items[i].get("service") as Lang.String or Null; var service = items[i].get("service") as Lang.String or Null;
if (type != null && name != null && entity != null) { if (type != null && name != null && entity != null) {
if (type.equals("toggle")) { if (type.equals("toggle")) {
var item = new HomeAssistantToggleMenuItem( var item = HomeAssistantMenuItemFactory.create().toggle(name, entity);
name,
toggle_obj,
entity,
false,
null
);
addItem(item); addItem(item);
mListToggleItems.add(item); mListToggleItems.add(item);
} else if (type.equals("tap") && service != null) { } else if (type.equals("tap") && service != null) {
if ((Application.Properties.getValue("lean_ui") as Lang.Boolean) == true){ addItem( HomeAssistantMenuItemFactory.create().tap(name, entity, service));
var icon = new WatchUi.Bitmap({
:rezId=>Rez.Drawables.TapIcon
});
var alignement = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT};
addItem(
new HomeAssistantIconMenuItem(
name,
strMenuItemTap,
entity,
service,
icon,
alignement
)
);
} else {
addItem(
new HomeAssistantMenuItem(
name,
strMenuItemTap,
entity,
service,
null
)
);
}
} else if (type.equals("group")) { } else if (type.equals("group")) {
if ((Application.Properties.getValue("lean_ui") as Lang.Boolean) == true){ var item = HomeAssistantMenuItemFactory.create().group(items[i]);
var item = new HomeAssistantViewIconMenuItem(items[i]);
addItem(item);
mListIconMenuItems.add(item);
} else {
var item = new HomeAssistantViewMenuItem(items[i]);
addItem(item); addItem(item);
mListMenuItems.add(item); mListMenuItems.add(item);
} }
} }
} }
} }
}
function getItemsToUpdate() as Lang.Array<HomeAssistantToggleMenuItem> { function getItemsToUpdate() as Lang.Array<HomeAssistantToggleMenuItem> {
var fullList = []; var fullList = [];
var lmi = mListMenuItems as Lang.Array<HomeAssistantViewMenuItem>; var lmi = mListMenuItems as Lang.Array<WatchUi.MenuItem>;
for(var i = 0; i < lmi.size(); i++) { for(var i = 0; i < mListMenuItems.size(); i++) {
fullList.addAll(lmi[i].getMenuView().getItemsToUpdate()); var item = lmi[i];
if (item instanceof HomeAssistantViewMenuItem) {
fullList.addAll(item.getMenuView().getItemsToUpdate());
} else if (item instanceof HomeAssistantViewIconMenuItem) {
fullList.addAll(item.getMenuView().getItemsToUpdate());
} }
var limi = mListIconMenuItems as Lang.Array<HomeAssistantViewIconMenuItem>;
for(var i = 0; i < limi.size(); i++) {
fullList.addAll(limi[i].getMenuView().getItemsToUpdate());
} }
return fullList.addAll(mListToggleItems); return fullList.addAll(mListToggleItems);

View File

@ -9,12 +9,12 @@
// 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, 31 October 2023 // P A Abbey & J D Abbey & SomeoneOnEarth, 16 November 2023
// //
// //
// Description: // Description:
// //
// Menu button that opens a sub-menu. // Menu button with an icon that opens a sub-menu.
// //
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------