From ae72313f5ecb87a2de3b53e5945068e37764f976 Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Sun, 31 Dec 2023 15:55:59 +0000 Subject: [PATCH] Settings reverted to a static class --- source/HomeAssistantApp.mc | 30 +++++++------- source/HomeAssistantConfirmation.mc | 2 +- source/HomeAssistantMenuItemFactory.mc | 8 ++-- source/HomeAssistantService.mc | 4 +- source/HomeAssistantToggleMenuItem.mc | 8 ++-- source/QuitTimer.mc | 2 +- source/Settings.mc | 55 ++++++++++---------------- 7 files changed, 49 insertions(+), 60 deletions(-) diff --git a/source/HomeAssistantApp.mc b/source/HomeAssistantApp.mc index 85950ed..3b4ba2b 100644 --- a/source/HomeAssistantApp.mc +++ b/source/HomeAssistantApp.mc @@ -122,25 +122,26 @@ class HomeAssistantApp extends Application.AppBase { strNoJson = WatchUi.loadResource($.Rez.Strings.NoJson); strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr); strTrailingSlashErr = WatchUi.loadResource($.Rez.Strings.TrailingSlashErr); - initResources(); mQuitTimer = new QuitTimer(); + Settings.update(); + initResources(); - if (Settings.get().getApiKey().length() == 0) { + if (Settings.getApiKey().length() == 0) { if (Globals.scDebug) { - System.println("HomeAssistantApp getInitialView(): No API key in the application Settings.get()."); + System.println("HomeAssistantApp getInitialView(): No API key in the application Settings."); } return ErrorView.create(strNoApiKey + "."); - } else if (Settings.get().getApiUrl().length() == 0) { + } else if (Settings.getApiUrl().length() == 0) { if (Globals.scDebug) { - System.println("HomeAssistantApp getInitialView(): No API URL in the application Settings.get()."); + System.println("HomeAssistantApp getInitialView(): No API URL in the application Settings."); } return ErrorView.create(strNoApiUrl + "."); - } else if (Settings.get().getApiUrl().substring(-1, Settings.get().getApiUrl().length()).equals("/")) { + } else if (Settings.getApiUrl().substring(-1, Settings.getApiUrl().length()).equals("/")) { if (Globals.scDebug) { System.println("HomeAssistantApp getInitialView(): API URL must not have a trailing slash '/'."); } return ErrorView.create(strTrailingSlashErr + "."); - } else if (Settings.get().getConfigUrl().length() == 0) { + } else if (Settings.getConfigUrl().length() == 0) { if (Globals.scDebug) { System.println("HomeAssistantApp getInitialView(): No configuration URL in the application settings."); } @@ -228,7 +229,7 @@ class HomeAssistantApp extends Application.AppBase { if (!mIsGlance) { mHaMenu = new HomeAssistantView(data, null); mQuitTimer.begin(); - if (Settings.get().get().getIsWidgetStartNoTap()) { + if (Settings.getIsWidgetStartNoTap()) { // As soon as the menu has been fetched start show the menu of items. // This behaviour is inconsistent with the standard Garmin User Interface, but has been // requested by users so has been made the non-default option. @@ -260,7 +261,7 @@ class HomeAssistantApp extends Application.AppBase { (:glance) function fetchMenuConfig() as Void { - if (Settings.get().getConfigUrl().equals("")) { + if (Settings.getConfigUrl().equals("")) { mMenuStatus = strUnconfigured; WatchUi.requestUpdate(); } else { @@ -286,7 +287,7 @@ class HomeAssistantApp extends Application.AppBase { mMenuStatus = strUnavailable; } else { Communications.makeWebRequest( - Settings.get().getConfigUrl(), + Settings.getConfigUrl(), null, { :method => Communications.HTTP_REQUEST_METHOD_GET, @@ -382,7 +383,7 @@ class HomeAssistantApp extends Application.AppBase { (:glance) function fetchApiStatus() as Void { - if (Settings.get().getApiUrl().equals("")) { + if (Settings.getApiUrl().equals("")) { mApiStatus = strUnconfigured; WatchUi.requestUpdate(); } else { @@ -408,12 +409,12 @@ class HomeAssistantApp extends Application.AppBase { } } else { Communications.makeWebRequest( - Settings.get().getApiUrl() + "/", + Settings.getApiUrl() + "/", null, { :method => Communications.HTTP_REQUEST_METHOD_GET, :headers => { - "Authorization" => "Bearer " + Settings.get().getApiKey() + "Authorization" => "Bearer " + Settings.getApiKey() }, :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON }, @@ -470,6 +471,7 @@ class HomeAssistantApp extends Application.AppBase { mIsGlance = true; initResources(); updateGlance(); + Settings.update(); mTimer = new Timer.Timer(); mTimer.start(method(:updateGlance), Globals.scApiBackoff, true); return [new HomeAssistantGlanceView(self)]; @@ -487,7 +489,7 @@ class HomeAssistantApp extends Application.AppBase { if (Globals.scDebug) { System.println("HomeAssistantApp onSettingsChanged()"); } - Settings.get().update(); + Settings.update(); } // Called each time the Registered Temporal Event is to be invoked. So the object is created each time on request and diff --git a/source/HomeAssistantConfirmation.mc b/source/HomeAssistantConfirmation.mc index 38ed9f6..b7f57b9 100644 --- a/source/HomeAssistantConfirmation.mc +++ b/source/HomeAssistantConfirmation.mc @@ -40,7 +40,7 @@ class HomeAssistantConfirmationDelegate extends WatchUi.ConfirmationDelegate { function initialize(callback as Method() as Void) { WatchUi.ConfirmationDelegate.initialize(); mConfirmMethod = callback; - var timeout = Settings.get().getConfirmTimeout(); // ms + var timeout = Settings.getConfirmTimeout(); // ms if (timeout > 0) { mTimer = new Timer.Timer(); mTimer.start(method(:onTimeout), timeout, true); diff --git a/source/HomeAssistantMenuItemFactory.mc b/source/HomeAssistantMenuItemFactory.mc index 1e84ef5..d9e46cc 100644 --- a/source/HomeAssistantMenuItemFactory.mc +++ b/source/HomeAssistantMenuItemFactory.mc @@ -39,7 +39,7 @@ class HomeAssistantMenuItemFactory { }; mMenuItemOptions = { - :alignment => Settings.get().getMenuAlignment() + :alignment => Settings.getMenuAlignment() }; strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap); @@ -67,7 +67,7 @@ class HomeAssistantMenuItemFactory { function toggle(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null) as WatchUi.MenuItem { return new HomeAssistantToggleMenuItem( label, - Settings.get().getMenuStyle() == Settings.MENU_STYLE_TEXT ? mLabelToggle : null, + Settings.getMenuStyle() == Settings.MENU_STYLE_TEXT ? mLabelToggle : null, identifier, false, mMenuItemOptions @@ -80,7 +80,7 @@ class HomeAssistantMenuItemFactory { service as Lang.String or Null, confirm as Lang.Boolean ) as WatchUi.MenuItem { - if (Settings.get().getMenuStyle() == Settings.MENU_STYLE_TEXT) { + if (Settings.getMenuStyle() == Settings.MENU_STYLE_TEXT) { return new HomeAssistantMenuItem( label, strMenuItemTap, @@ -105,7 +105,7 @@ class HomeAssistantMenuItemFactory { } function group(definition as Lang.Dictionary) as WatchUi.MenuItem { - if (Settings.get().getMenuStyle() == Settings.MENU_STYLE_TEXT) { + if (Settings.getMenuStyle() == Settings.MENU_STYLE_TEXT) { return new HomeAssistantViewMenuItem(definition); } else { return new HomeAssistantViewIconMenuItem(definition, mGroupTypeIcon, mMenuItemOptions); diff --git a/source/HomeAssistantService.mc b/source/HomeAssistantService.mc index 4588b8b..ab7e6c6 100644 --- a/source/HomeAssistantService.mc +++ b/source/HomeAssistantService.mc @@ -129,7 +129,7 @@ class HomeAssistantService { ErrorView.show(strNoInternet + "."); } else { // Can't use null for substring() parameters due to API version level. - var url = Settings.get().getApiUrl() + "/services/" + service.substring(0, service.find(".")) + "/" + service.substring(service.find(".")+1, service.length()); + var url = Settings.getApiUrl() + "/services/" + service.substring(0, service.find(".")) + "/" + service.substring(service.find(".")+1, service.length()); if (Globals.scDebug) { System.println("HomeAssistantService call() URL=" + url); System.println("HomeAssistantService call() service=" + service); @@ -143,7 +143,7 @@ class HomeAssistantService { :method => Communications.HTTP_REQUEST_METHOD_POST, :headers => { "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, - "Authorization" => "Bearer " + Settings.get().getApiKey() + "Authorization" => "Bearer " + Settings.getApiKey() }, :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON, :context => identifier diff --git a/source/HomeAssistantToggleMenuItem.mc b/source/HomeAssistantToggleMenuItem.mc index 388cc5f..4502948 100644 --- a/source/HomeAssistantToggleMenuItem.mc +++ b/source/HomeAssistantToggleMenuItem.mc @@ -178,7 +178,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { ErrorView.show(strNoInternet + "."); getApp().setApiStatus(strUnavailable); } else { - var url = Settings.get().getApiUrl() + "/states/" + mIdentifier; + var url = Settings.getApiUrl() + "/states/" + mIdentifier; if (Globals.scDebug) { System.println("HomeAssistantToggleMenuItem getState() URL=" + url); } @@ -188,7 +188,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { { :method => Communications.HTTP_REQUEST_METHOD_GET, :headers => { - "Authorization" => "Bearer " + Settings.get().getApiKey() + "Authorization" => "Bearer " + Settings.getApiKey() }, :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON }, @@ -286,7 +286,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { // Updated SDK and got a new error // ERROR: venu: Cannot find symbol ':substring' on type 'PolyType'. var id = mIdentifier as Lang.String; - var url = Settings.get().getApiKey() + "/services/"; + var url = Settings.getApiUrl() + "/services/"; if (s) { url = url + id.substring(0, id.find(".")) + "/turn_on"; } else { @@ -305,7 +305,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { :method => Communications.HTTP_REQUEST_METHOD_POST, :headers => { "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, - "Authorization" => "Bearer " + Settings.get().getApiKey() + "Authorization" => "Bearer " + Settings.getApiKey() }, :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON }, diff --git a/source/QuitTimer.mc b/source/QuitTimer.mc index 5dd1663..a5901b8 100644 --- a/source/QuitTimer.mc +++ b/source/QuitTimer.mc @@ -38,7 +38,7 @@ class QuitTimer extends Timer.Timer { } function begin() { - var api_timeout = Settings.get().getAppTimeout(); // ms + var api_timeout = Settings.getAppTimeout(); // ms if (api_timeout > 0) { start(method(:exitApp), api_timeout, false); } diff --git a/source/Settings.mc b/source/Settings.mc index 323a4b6..8faa54f 100644 --- a/source/Settings.mc +++ b/source/Settings.mc @@ -28,30 +28,24 @@ using Toybox.Time; (:glance, :background) class Settings { - private static var instance; - public static const MENU_STYLE_ICONS = 0; public static const MENU_STYLE_TEXT = 1; - private var mApiKey as Lang.String = ""; - private var mApiUrl as Lang.String = ""; - private var mConfigUrl as Lang.String = ""; - private var mAppTimeout as Lang.Number = 0; // seconds - private var mConfirmTimeout as Lang.Number = 3; // seconds - private var mMenuStyle as Lang.Number = MENU_STYLE_ICONS; - private var mMenuAlignment as Lang.Number = WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT; - private var mIsWidgetStartNoTap as Lang.Boolean = false; - private var mIsBatteryLevelEnabled as Lang.Boolean = false; - private var mBatteryRefreshRate as Lang.Number = 15; // minutes - private var mIsApp as Lang.Boolean = false; - - private function initialize() { - mIsApp = getApp().getIsApp(); - update(); - } + private static var mApiKey as Lang.String = ""; + private static var mApiUrl as Lang.String = ""; + private static var mConfigUrl as Lang.String = ""; + private static var mAppTimeout as Lang.Number = 0; // seconds + private static var mConfirmTimeout as Lang.Number = 3; // seconds + private static var mMenuStyle as Lang.Number = MENU_STYLE_ICONS; + private static var mMenuAlignment as Lang.Number = WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT; + private static var mIsWidgetStartNoTap as Lang.Boolean = false; + private static var mIsBatteryLevelEnabled as Lang.Boolean = false; + private static var mBatteryRefreshRate as Lang.Number = 15; // minutes + private static var mIsApp as Lang.Boolean = false; // Called on application start and then whenever the settings are changed. - function update() { + static function update() { + mIsApp = getApp().getIsApp(); mApiKey = Properties.getValue("api_key"); mApiUrl = Properties.getValue("api_url"); mConfigUrl = Properties.getValue("config_url"); @@ -94,42 +88,35 @@ class Settings { } } - static function get() as Settings { - if (instance == null) { - instance = new Settings(); - } - return instance; - } - - function getApiKey() as Lang.String { + static function getApiKey() as Lang.String { return mApiKey; } - function getApiUrl() as Lang.String { + static function getApiUrl() as Lang.String { return mApiUrl; } - function getConfigUrl() as Lang.String { + static function getConfigUrl() as Lang.String { return mConfigUrl; } - function getAppTimeout() as Lang.Number { + static function getAppTimeout() as Lang.Number { return mAppTimeout * 1000; // Convert to milliseconds } - function getConfirmTimeout() as Lang.Number { + static function getConfirmTimeout() as Lang.Number { return mConfirmTimeout * 1000; // Convert to milliseconds } - function getMenuStyle() as Lang.Number { + static function getMenuStyle() as Lang.Number { return mMenuStyle; // Either MENU_STYLE_ICONS or MENU_STYLE_TEXT } - function getMenuAlignment() as Lang.Number { + static function getMenuAlignment() as Lang.Number { return mMenuAlignment; // Either WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT or WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT } - function getIsWidgetStartNoTap() as Lang.Boolean { + static function getIsWidgetStartNoTap() as Lang.Boolean { return mIsWidgetStartNoTap; }