diff --git a/source/HomeAssistantApp.mc b/source/HomeAssistantApp.mc index cfbc9e8..565ea92 100644 --- a/source/HomeAssistantApp.mc +++ b/source/HomeAssistantApp.mc @@ -100,14 +100,9 @@ class HomeAssistantApp extends Application.AppBase { System.println("HomeAssistantApp fetchMenuConfig(): No Internet connection, skipping API call."); } return [new ErrorView(strNoInternet + "."), new ErrorDelegate()] as Lang.Array; - }else if (mHaMenu != null ){ - // App.getApp().launchInitialView(); - return [mHaMenu, new WatchUi.BehaviorDelegate()] as Lang.Array; - // return mHaMenu.View; - // WatchUi.pushView(mHaMenu, new HomeAssistantViewDelegate(), WatchUi.SLIDE_IMMEDIATE); } else { fetchMenuConfig(); - return [new WatchUi.View(), new WatchUi.BehaviorDelegate()] as Lang.Array; + return [new RootView(self), new RootViewDelegate(self)] as Lang.Array; } } @@ -144,7 +139,7 @@ class HomeAssistantApp extends Application.AppBase { } else if (responseCode == 200) { mHaMenu = new HomeAssistantView(data, null); quitTimer.begin(); - WatchUi.pushView(mHaMenu, new HomeAssistantViewDelegate(), WatchUi.SLIDE_IMMEDIATE); + pushHomeAssistantMenuView(); mItemsToUpdate = mHaMenu.getItemsToUpdate(); // Start the continuous update process that continues for as long as the application is running. // The chain of functions from 'updateNextMenuItem()' calls 'updateNextMenuItem()' on completion. @@ -177,6 +172,14 @@ class HomeAssistantApp extends Application.AppBase { ); } + function homeAssistantMenuIsLoaded() as Lang.Boolean{ + return mHaMenu!=null; + } + + function pushHomeAssistantMenuView(){ + WatchUi.pushView(mHaMenu, new HomeAssistantViewDelegate(), WatchUi.SLIDE_IMMEDIATE); + } + // We need to spread out the API calls so as not to overload the results queue and cause Communications.BLE_QUEUE_FULL (-101) error. // This function is called by a timer every Globals.menuItemUpdateInterval ms. function updateNextMenuItem() as Void { diff --git a/source/RootView.mc b/source/RootView.mc new file mode 100644 index 0000000..4a913cd --- /dev/null +++ b/source/RootView.mc @@ -0,0 +1,60 @@ +import Toybox.Graphics; +import Toybox.Lang; +import Toybox.WatchUi; + +class RootView extends WatchUi.View { + + var width,height; + var mApp as HomeAssistantApp; + + function initialize(app as HomeAssistantApp) { + View.initialize(); + mApp=app; + } + + function onLayout(dc as Dc) as Void { + width=dc.getWidth(); + height=dc.getHeight(); + } + + function onUpdate(dc as Dc) as Void { + dc.setColor(Graphics.COLOR_BLACK,Graphics.COLOR_BLACK); + dc.clear(); + dc.setColor(Graphics.COLOR_BLUE,Graphics.COLOR_TRANSPARENT); + if(mApp.homeAssistantMenuIsLoaded()) { + dc.drawText(width/2,height/2,Graphics.FONT_SMALL,"Hit Back to Exit\nTap to stay",Graphics.TEXT_JUSTIFY_CENTER|Graphics.TEXT_JUSTIFY_VCENTER); + } else { + dc.drawText(width/2,height/2,Graphics.FONT_SMALL,"Loading...",Graphics.TEXT_JUSTIFY_CENTER|Graphics.TEXT_JUSTIFY_VCENTER); + } + } +} + +class RootViewDelegate extends WatchUi.BehaviorDelegate { + + var mApp as HomeAssistantApp; + + function initialize(app as HomeAssistantApp ) { + BehaviorDelegate.initialize(); + mApp=app; + } + + public function onTap(evt as ClickEvent) as Boolean { + return backToMainMenu(); + } + + public function onSelect() as Boolean { + return backToMainMenu(); + } + + function onMenu(){ + return backToMainMenu(); + } + + private function backToMainMenu() as Lang.Boolean{ + if(mApp.homeAssistantMenuIsLoaded()){ + mApp.pushHomeAssistantMenuView(); + return true; + } + return false; + } +}