Merge pull request #28 from j-a-n/confirm_timeout

Auto close confirmation dialog after timeout
This commit is contained in:
Philip Abbey
2023-12-11 08:22:10 +00:00
committed by GitHub
3 changed files with 32 additions and 1 deletions

View File

@ -30,6 +30,12 @@
--> -->
<property id="app_timeout" type="number">0</property> <property id="app_timeout" type="number">0</property>
<!--
After this time (in seconds), a confirmation dialog for an action is automatically closed and the action is cancelled.
Set to 0 to disable the timeout. The default value is 3 seconds.
-->
<property id="confirm_timeout" type="number">3</property>
<property id="types_representation" type="boolean"></property> <property id="types_representation" type="boolean"></property>
<property id="menu_alignment" type="boolean"></property> <property id="menu_alignment" type="boolean"></property>

View File

@ -53,6 +53,16 @@
/> />
</setting> </setting>
<setting
propertyKey="@Properties.confirm_timeout"
title="After this time (in seconds), a confirmation dialog for an action is automatically closed and the action is cancelled. Set to 0 to disable the timeout."
>
<settingConfig
type="numeric"
min="0"
/>
</setting>
<setting <setting
propertyKey="@Properties.types_representation" propertyKey="@Properties.types_representation"
title="Representing types with icons (off) or with labels (on)" title="Representing types with icons (off) or with labels (on)"

View File

@ -22,9 +22,10 @@ using Toybox.Lang;
// Required for callback method definition // Required for callback method definition
typedef Method as Toybox.Lang.Method; typedef Method as Toybox.Lang.Method;
using Toybox.WatchUi; using Toybox.WatchUi;
using Toybox.Timer;
using Toybox.Application.Properties;
class HomeAssistantConfirmation extends WatchUi.Confirmation { class HomeAssistantConfirmation extends WatchUi.Confirmation {
function initialize() { function initialize() {
WatchUi.Confirmation.initialize(WatchUi.loadResource($.Rez.Strings.Confirm)); WatchUi.Confirmation.initialize(WatchUi.loadResource($.Rez.Strings.Confirm));
} }
@ -33,17 +34,31 @@ class HomeAssistantConfirmation extends WatchUi.Confirmation {
class HomeAssistantConfirmationDelegate extends WatchUi.ConfirmationDelegate { class HomeAssistantConfirmationDelegate extends WatchUi.ConfirmationDelegate {
private var confirmMethod; private var confirmMethod;
private var timeout;
function initialize(callback as Method() as Void) { function initialize(callback as Method() as Void) {
WatchUi.ConfirmationDelegate.initialize(); WatchUi.ConfirmationDelegate.initialize();
confirmMethod = callback; confirmMethod = callback;
var timeoutSeconds = Properties.getValue("confirm_timeout") as Lang.Number;
if (timeoutSeconds > 0) {
timeout = new Timer.Timer();
timeout.start(method(:onTimeout), timeoutSeconds * 1000, true);
}
} }
function onResponse(response) as Lang.Boolean { function onResponse(response) as Lang.Boolean {
getApp().getQuitTimer().reset(); getApp().getQuitTimer().reset();
if (timeout) {
timeout.stop();
}
if (response == WatchUi.CONFIRM_YES) { if (response == WatchUi.CONFIRM_YES) {
confirmMethod.invoke(); confirmMethod.invoke();
} }
return true; return true;
} }
function onTimeout() as Void {
timeout.stop();
WatchUi.popView(WatchUi.SLIDE_RIGHT);
}
} }