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