mirror of
				https://github.com/house-of-abbey/GarminHomeAssistant.git
				synced 2025-11-04 08:58:13 +00:00 
			
		
		
		
	Initial version with "auto quit"
Quit the application after a user settable period of time based on a timeout value from the settings.
This commit is contained in:
		@@ -132,11 +132,13 @@ class AlertDelegate extends WatchUi.InputDelegate {
 | 
			
		||||
 | 
			
		||||
    function onKey(evt) {
 | 
			
		||||
        mView.dismiss();
 | 
			
		||||
        getApp().getQuitTimer().reset();
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function onTap(evt) {
 | 
			
		||||
        mView.dismiss();
 | 
			
		||||
        getApp().getQuitTimer().reset();
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -82,6 +82,7 @@ class ErrorDelegate extends WatchUi.BehaviorDelegate {
 | 
			
		||||
        WatchUi.BehaviorDelegate.initialize();
 | 
			
		||||
    }
 | 
			
		||||
    function onBack() {
 | 
			
		||||
        getApp().getQuitTimer().reset();
 | 
			
		||||
        WatchUi.popView(WatchUi.SLIDE_DOWN);
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -25,6 +25,7 @@ using Toybox.Application.Properties;
 | 
			
		||||
 | 
			
		||||
class HomeAssistantApp extends Application.AppBase {
 | 
			
		||||
    private var mHaMenu;
 | 
			
		||||
    private var quitTimer            as QuitTimer;
 | 
			
		||||
    private var strNoApiKey          as Lang.String;
 | 
			
		||||
    private var strNoApiUrl          as Lang.String;
 | 
			
		||||
    private var strNoConfigUrl       as Lang.String;
 | 
			
		||||
@@ -52,14 +53,18 @@ class HomeAssistantApp extends Application.AppBase {
 | 
			
		||||
        strConfigUrlNotFound = WatchUi.loadResource($.Rez.Strings.ConfigUrlNotFound);
 | 
			
		||||
        strUnhandledHttpErr  = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr);
 | 
			
		||||
        strTrailingSlashErr  = WatchUi.loadResource($.Rez.Strings.TrailingSlashErr);
 | 
			
		||||
        quitTimer            = new QuitTimer();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // onStart() is called on application start up
 | 
			
		||||
    function onStart(state as Lang.Dictionary?) as Void {
 | 
			
		||||
        quitTimer.begin();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // onStop() is called when your application is exiting
 | 
			
		||||
    function onStop(state as Lang.Dictionary?) as Void {}
 | 
			
		||||
    function onStop(state as Lang.Dictionary?) as Void {
 | 
			
		||||
        quitTimer.stop();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Return the initial view of your application here
 | 
			
		||||
    function getInitialView() as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>? {
 | 
			
		||||
@@ -174,6 +179,10 @@ class HomeAssistantApp extends Application.AppBase {
 | 
			
		||||
        mNextItemToUpdate = (mNextItemToUpdate + 1) % itu.size();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function getQuitTimer() as QuitTimer{
 | 
			
		||||
        return quitTimer;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getApp() as HomeAssistantApp {
 | 
			
		||||
 
 | 
			
		||||
@@ -40,6 +40,7 @@ class HomeAssistantConfirmationDelegate extends WatchUi.ConfirmationDelegate {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function onResponse(response) as Lang.Boolean {
 | 
			
		||||
        getApp().getQuitTimer().reset();
 | 
			
		||||
        if (response == WatchUi.CONFIRM_YES) {
 | 
			
		||||
            confirmMethod.invoke();
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -107,7 +107,23 @@ class HomeAssistantViewDelegate extends WatchUi.Menu2InputDelegate {
 | 
			
		||||
        Menu2InputDelegate.initialize();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function onBack() {
 | 
			
		||||
        getApp().getQuitTimer().reset();
 | 
			
		||||
        WatchUi.popView(WatchUi.SLIDE_RIGHT);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Only for CheckboxMenu
 | 
			
		||||
    function onDone() {
 | 
			
		||||
        getApp().getQuitTimer().reset();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Only for CustomMenu
 | 
			
		||||
    function onFooter() {
 | 
			
		||||
        getApp().getQuitTimer().reset();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function onSelect(item as WatchUi.MenuItem) as Void {
 | 
			
		||||
        getApp().getQuitTimer().reset();
 | 
			
		||||
        if (item instanceof HomeAssistantToggleMenuItem) {
 | 
			
		||||
            var haToggleItem = item as HomeAssistantToggleMenuItem;
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
@@ -147,8 +163,9 @@ class HomeAssistantViewDelegate extends WatchUi.Menu2InputDelegate {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function onBack() {
 | 
			
		||||
        WatchUi.popView(WatchUi.SLIDE_RIGHT);
 | 
			
		||||
    // Only for CustomMenu
 | 
			
		||||
    function onTitle() {
 | 
			
		||||
        getApp().getQuitTimer().reset();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										56
									
								
								source/QuitTimer.mc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								source/QuitTimer.mc
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,56 @@
 | 
			
		||||
//-----------------------------------------------------------------------------------
 | 
			
		||||
//
 | 
			
		||||
// 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.
 | 
			
		||||
//
 | 
			
		||||
// J D Abbey & P A Abbey, 28 December 2022
 | 
			
		||||
//
 | 
			
		||||
//
 | 
			
		||||
// Description:
 | 
			
		||||
//
 | 
			
		||||
// Quit the application after a period of inactivity in order to save the battery.
 | 
			
		||||
//
 | 
			
		||||
//-----------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
using Toybox.Lang;
 | 
			
		||||
using Toybox.Timer;
 | 
			
		||||
using Toybox.Application.Properties;
 | 
			
		||||
using Toybox.WatchUi;
 | 
			
		||||
 | 
			
		||||
class QuitTimer extends Timer.Timer {
 | 
			
		||||
    private var api_timeout;
 | 
			
		||||
    
 | 
			
		||||
    function initialize() {
 | 
			
		||||
        Timer.Timer.initialize();
 | 
			
		||||
        // Timer needs delay in milliseconds.
 | 
			
		||||
        api_timeout = (Properties.getValue("app_timeout") as Lang.Number) * 1000;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function exitApp() as Void {
 | 
			
		||||
        if (Globals.scDebug) {
 | 
			
		||||
            System.println("QuitTimer exitApp(): Exiting");
 | 
			
		||||
        }
 | 
			
		||||
         // This will exit the system cleanly from any point within an app.
 | 
			
		||||
        System.exit();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function begin() {
 | 
			
		||||
        if (api_timeout > 0) {
 | 
			
		||||
            start(method(:exitApp), api_timeout, false);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function reset() {
 | 
			
		||||
        if (Globals.scDebug) {
 | 
			
		||||
            System.println("QuitTimer reset(): Restarted quit timer");
 | 
			
		||||
        }
 | 
			
		||||
        stop();
 | 
			
		||||
        begin();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user