Settings reverted to a static class

This commit is contained in:
Philip Abbey
2023-12-31 15:55:59 +00:00
parent 56155f5f5c
commit ae72313f5e
7 changed files with 49 additions and 60 deletions

View File

@ -122,25 +122,26 @@ class HomeAssistantApp extends Application.AppBase {
strNoJson = WatchUi.loadResource($.Rez.Strings.NoJson); strNoJson = WatchUi.loadResource($.Rez.Strings.NoJson);
strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr); strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr);
strTrailingSlashErr = WatchUi.loadResource($.Rez.Strings.TrailingSlashErr); strTrailingSlashErr = WatchUi.loadResource($.Rez.Strings.TrailingSlashErr);
initResources();
mQuitTimer = new QuitTimer(); mQuitTimer = new QuitTimer();
Settings.update();
initResources();
if (Settings.get().getApiKey().length() == 0) { if (Settings.getApiKey().length() == 0) {
if (Globals.scDebug) { 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 + "."); return ErrorView.create(strNoApiKey + ".");
} else if (Settings.get().getApiUrl().length() == 0) { } else if (Settings.getApiUrl().length() == 0) {
if (Globals.scDebug) { 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 + "."); 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) { if (Globals.scDebug) {
System.println("HomeAssistantApp getInitialView(): API URL must not have a trailing slash '/'."); System.println("HomeAssistantApp getInitialView(): API URL must not have a trailing slash '/'.");
} }
return ErrorView.create(strTrailingSlashErr + "."); return ErrorView.create(strTrailingSlashErr + ".");
} else if (Settings.get().getConfigUrl().length() == 0) { } else if (Settings.getConfigUrl().length() == 0) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp getInitialView(): No configuration URL in the application settings."); System.println("HomeAssistantApp getInitialView(): No configuration URL in the application settings.");
} }
@ -228,7 +229,7 @@ class HomeAssistantApp extends Application.AppBase {
if (!mIsGlance) { if (!mIsGlance) {
mHaMenu = new HomeAssistantView(data, null); mHaMenu = new HomeAssistantView(data, null);
mQuitTimer.begin(); mQuitTimer.begin();
if (Settings.get().get().getIsWidgetStartNoTap()) { if (Settings.getIsWidgetStartNoTap()) {
// As soon as the menu has been fetched start show the menu of items. // 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 // This behaviour is inconsistent with the standard Garmin User Interface, but has been
// requested by users so has been made the non-default option. // requested by users so has been made the non-default option.
@ -260,7 +261,7 @@ class HomeAssistantApp extends Application.AppBase {
(:glance) (:glance)
function fetchMenuConfig() as Void { function fetchMenuConfig() as Void {
if (Settings.get().getConfigUrl().equals("")) { if (Settings.getConfigUrl().equals("")) {
mMenuStatus = strUnconfigured; mMenuStatus = strUnconfigured;
WatchUi.requestUpdate(); WatchUi.requestUpdate();
} else { } else {
@ -286,7 +287,7 @@ class HomeAssistantApp extends Application.AppBase {
mMenuStatus = strUnavailable; mMenuStatus = strUnavailable;
} else { } else {
Communications.makeWebRequest( Communications.makeWebRequest(
Settings.get().getConfigUrl(), Settings.getConfigUrl(),
null, null,
{ {
:method => Communications.HTTP_REQUEST_METHOD_GET, :method => Communications.HTTP_REQUEST_METHOD_GET,
@ -382,7 +383,7 @@ class HomeAssistantApp extends Application.AppBase {
(:glance) (:glance)
function fetchApiStatus() as Void { function fetchApiStatus() as Void {
if (Settings.get().getApiUrl().equals("")) { if (Settings.getApiUrl().equals("")) {
mApiStatus = strUnconfigured; mApiStatus = strUnconfigured;
WatchUi.requestUpdate(); WatchUi.requestUpdate();
} else { } else {
@ -408,12 +409,12 @@ class HomeAssistantApp extends Application.AppBase {
} }
} else { } else {
Communications.makeWebRequest( Communications.makeWebRequest(
Settings.get().getApiUrl() + "/", Settings.getApiUrl() + "/",
null, null,
{ {
:method => Communications.HTTP_REQUEST_METHOD_GET, :method => Communications.HTTP_REQUEST_METHOD_GET,
:headers => { :headers => {
"Authorization" => "Bearer " + Settings.get().getApiKey() "Authorization" => "Bearer " + Settings.getApiKey()
}, },
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
}, },
@ -470,6 +471,7 @@ class HomeAssistantApp extends Application.AppBase {
mIsGlance = true; mIsGlance = true;
initResources(); initResources();
updateGlance(); updateGlance();
Settings.update();
mTimer = new Timer.Timer(); mTimer = new Timer.Timer();
mTimer.start(method(:updateGlance), Globals.scApiBackoff, true); mTimer.start(method(:updateGlance), Globals.scApiBackoff, true);
return [new HomeAssistantGlanceView(self)]; return [new HomeAssistantGlanceView(self)];
@ -487,7 +489,7 @@ class HomeAssistantApp extends Application.AppBase {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp onSettingsChanged()"); 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 // Called each time the Registered Temporal Event is to be invoked. So the object is created each time on request and

View File

@ -40,7 +40,7 @@ class HomeAssistantConfirmationDelegate extends WatchUi.ConfirmationDelegate {
function initialize(callback as Method() as Void) { function initialize(callback as Method() as Void) {
WatchUi.ConfirmationDelegate.initialize(); WatchUi.ConfirmationDelegate.initialize();
mConfirmMethod = callback; mConfirmMethod = callback;
var timeout = Settings.get().getConfirmTimeout(); // ms var timeout = Settings.getConfirmTimeout(); // ms
if (timeout > 0) { if (timeout > 0) {
mTimer = new Timer.Timer(); mTimer = new Timer.Timer();
mTimer.start(method(:onTimeout), timeout, true); mTimer.start(method(:onTimeout), timeout, true);

View File

@ -39,7 +39,7 @@ class HomeAssistantMenuItemFactory {
}; };
mMenuItemOptions = { mMenuItemOptions = {
:alignment => Settings.get().getMenuAlignment() :alignment => Settings.getMenuAlignment()
}; };
strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap); 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 { function toggle(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null) as WatchUi.MenuItem {
return new HomeAssistantToggleMenuItem( return new HomeAssistantToggleMenuItem(
label, label,
Settings.get().getMenuStyle() == Settings.MENU_STYLE_TEXT ? mLabelToggle : null, Settings.getMenuStyle() == Settings.MENU_STYLE_TEXT ? mLabelToggle : null,
identifier, identifier,
false, false,
mMenuItemOptions mMenuItemOptions
@ -80,7 +80,7 @@ class HomeAssistantMenuItemFactory {
service as Lang.String or Null, service as Lang.String or Null,
confirm as Lang.Boolean confirm as Lang.Boolean
) as WatchUi.MenuItem { ) as WatchUi.MenuItem {
if (Settings.get().getMenuStyle() == Settings.MENU_STYLE_TEXT) { if (Settings.getMenuStyle() == Settings.MENU_STYLE_TEXT) {
return new HomeAssistantMenuItem( return new HomeAssistantMenuItem(
label, label,
strMenuItemTap, strMenuItemTap,
@ -105,7 +105,7 @@ class HomeAssistantMenuItemFactory {
} }
function group(definition as Lang.Dictionary) as WatchUi.MenuItem { 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); return new HomeAssistantViewMenuItem(definition);
} else { } else {
return new HomeAssistantViewIconMenuItem(definition, mGroupTypeIcon, mMenuItemOptions); return new HomeAssistantViewIconMenuItem(definition, mGroupTypeIcon, mMenuItemOptions);

View File

@ -129,7 +129,7 @@ class HomeAssistantService {
ErrorView.show(strNoInternet + "."); ErrorView.show(strNoInternet + ".");
} else { } else {
// Can't use null for substring() parameters due to API version level. // 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) { if (Globals.scDebug) {
System.println("HomeAssistantService call() URL=" + url); System.println("HomeAssistantService call() URL=" + url);
System.println("HomeAssistantService call() service=" + service); System.println("HomeAssistantService call() service=" + service);
@ -143,7 +143,7 @@ class HomeAssistantService {
:method => Communications.HTTP_REQUEST_METHOD_POST, :method => Communications.HTTP_REQUEST_METHOD_POST,
:headers => { :headers => {
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON,
"Authorization" => "Bearer " + Settings.get().getApiKey() "Authorization" => "Bearer " + Settings.getApiKey()
}, },
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON, :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
:context => identifier :context => identifier

View File

@ -178,7 +178,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
ErrorView.show(strNoInternet + "."); ErrorView.show(strNoInternet + ".");
getApp().setApiStatus(strUnavailable); getApp().setApiStatus(strUnavailable);
} else { } else {
var url = Settings.get().getApiUrl() + "/states/" + mIdentifier; var url = Settings.getApiUrl() + "/states/" + mIdentifier;
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem getState() URL=" + url); System.println("HomeAssistantToggleMenuItem getState() URL=" + url);
} }
@ -188,7 +188,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
{ {
:method => Communications.HTTP_REQUEST_METHOD_GET, :method => Communications.HTTP_REQUEST_METHOD_GET,
:headers => { :headers => {
"Authorization" => "Bearer " + Settings.get().getApiKey() "Authorization" => "Bearer " + Settings.getApiKey()
}, },
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
}, },
@ -286,7 +286,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
// Updated SDK and got a new error // Updated SDK and got a new error
// ERROR: venu: Cannot find symbol ':substring' on type 'PolyType<Null or $.Toybox.Lang.Object>'. // ERROR: venu: Cannot find symbol ':substring' on type 'PolyType<Null or $.Toybox.Lang.Object>'.
var id = mIdentifier as Lang.String; var id = mIdentifier as Lang.String;
var url = Settings.get().getApiKey() + "/services/"; var url = Settings.getApiUrl() + "/services/";
if (s) { if (s) {
url = url + id.substring(0, id.find(".")) + "/turn_on"; url = url + id.substring(0, id.find(".")) + "/turn_on";
} else { } else {
@ -305,7 +305,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
:method => Communications.HTTP_REQUEST_METHOD_POST, :method => Communications.HTTP_REQUEST_METHOD_POST,
:headers => { :headers => {
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON,
"Authorization" => "Bearer " + Settings.get().getApiKey() "Authorization" => "Bearer " + Settings.getApiKey()
}, },
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
}, },

View File

@ -38,7 +38,7 @@ class QuitTimer extends Timer.Timer {
} }
function begin() { function begin() {
var api_timeout = Settings.get().getAppTimeout(); // ms var api_timeout = Settings.getAppTimeout(); // ms
if (api_timeout > 0) { if (api_timeout > 0) {
start(method(:exitApp), api_timeout, false); start(method(:exitApp), api_timeout, false);
} }

View File

@ -28,30 +28,24 @@ using Toybox.Time;
(:glance, :background) (:glance, :background)
class Settings { class Settings {
private static var instance;
public static const MENU_STYLE_ICONS = 0; public static const MENU_STYLE_ICONS = 0;
public static const MENU_STYLE_TEXT = 1; public static const MENU_STYLE_TEXT = 1;
private var mApiKey as Lang.String = ""; private static var mApiKey as Lang.String = "";
private var mApiUrl as Lang.String = ""; private static var mApiUrl as Lang.String = "";
private var mConfigUrl as Lang.String = ""; private static var mConfigUrl as Lang.String = "";
private var mAppTimeout as Lang.Number = 0; // seconds private static var mAppTimeout as Lang.Number = 0; // seconds
private var mConfirmTimeout as Lang.Number = 3; // seconds private static var mConfirmTimeout as Lang.Number = 3; // seconds
private var mMenuStyle as Lang.Number = MENU_STYLE_ICONS; private static var mMenuStyle as Lang.Number = MENU_STYLE_ICONS;
private var mMenuAlignment as Lang.Number = WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT; private static var mMenuAlignment as Lang.Number = WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT;
private var mIsWidgetStartNoTap as Lang.Boolean = false; private static var mIsWidgetStartNoTap as Lang.Boolean = false;
private var mIsBatteryLevelEnabled as Lang.Boolean = false; private static var mIsBatteryLevelEnabled as Lang.Boolean = false;
private var mBatteryRefreshRate as Lang.Number = 15; // minutes private static var mBatteryRefreshRate as Lang.Number = 15; // minutes
private var mIsApp as Lang.Boolean = false; private static var mIsApp as Lang.Boolean = false;
private function initialize() {
mIsApp = getApp().getIsApp();
update();
}
// Called on application start and then whenever the settings are changed. // Called on application start and then whenever the settings are changed.
function update() { static function update() {
mIsApp = getApp().getIsApp();
mApiKey = Properties.getValue("api_key"); mApiKey = Properties.getValue("api_key");
mApiUrl = Properties.getValue("api_url"); mApiUrl = Properties.getValue("api_url");
mConfigUrl = Properties.getValue("config_url"); mConfigUrl = Properties.getValue("config_url");
@ -94,42 +88,35 @@ class Settings {
} }
} }
static function get() as Settings { static function getApiKey() as Lang.String {
if (instance == null) {
instance = new Settings();
}
return instance;
}
function getApiKey() as Lang.String {
return mApiKey; return mApiKey;
} }
function getApiUrl() as Lang.String { static function getApiUrl() as Lang.String {
return mApiUrl; return mApiUrl;
} }
function getConfigUrl() as Lang.String { static function getConfigUrl() as Lang.String {
return mConfigUrl; return mConfigUrl;
} }
function getAppTimeout() as Lang.Number { static function getAppTimeout() as Lang.Number {
return mAppTimeout * 1000; // Convert to milliseconds return mAppTimeout * 1000; // Convert to milliseconds
} }
function getConfirmTimeout() as Lang.Number { static function getConfirmTimeout() as Lang.Number {
return mConfirmTimeout * 1000; // Convert to milliseconds 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 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 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; return mIsWidgetStartNoTap;
} }