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

View File

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

View File

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

View File

@ -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

View File

@ -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<Null or $.Toybox.Lang.Object>'.
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
},

View File

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

View File

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