2023-01-20 07:32:08 +00:00
( function ( ) {
2023-01-22 08:47:48 +00:00
var DEFAULTS = {
mode : 0 ,
apps : [ ] ,
} ;
var settings = require ( "Storage" ) . readJSON ( "backswipe.json" , 1 ) || DEFAULTS ;
2023-01-20 08:32:11 +00:00
2023-01-20 07:32:08 +00:00
// Overrride the default setUI method, so we can save the back button callback
var setUI = Bangle . setUI ;
Bangle . setUI = function ( mode , cb ) {
var options = { } ;
if ( "object" == typeof mode ) {
options = mode ;
}
2023-01-20 08:32:11 +00:00
var currentFile = global . _ _FILE _ _ || "" ;
2023-02-04 21:36:16 +00:00
if ( global . BACK ) delete global . BACK ;
2023-02-07 18:40:07 +00:00
if ( options && options . back && enabledForApp ( currentFile ) ) {
2023-01-20 07:32:08 +00:00
global . BACK = options . back ;
}
setUI ( mode , cb ) ;
} ;
2023-02-07 18:33:57 +00:00
function countHandlers ( eventType ) {
if ( Bangle [ "#on" + eventType ] === undefined ) {
return 0 ;
} else if ( Bangle [ "#on" + eventType ] instanceof Array ) {
2023-10-10 09:50:07 +00:00
return Bangle [ "#on" + eventType ] . filter ( x => x ) . length ;
2023-02-07 19:28:22 +00:00
} else if ( Bangle [ "#on" + eventType ] !== undefined ) {
2023-02-07 18:33:57 +00:00
return 1 ;
}
}
2023-02-05 13:43:43 +00:00
function goBack ( lr , _ ) {
2023-01-20 07:32:08 +00:00
// if it is a left to right swipe
if ( lr === 1 ) {
// if we're in an app that has a back button, run the callback for it
2023-02-07 19:28:22 +00:00
if ( global . BACK && countHandlers ( "swipe" ) <= settings . standardNumSwipeHandlers && countHandlers ( "drag" ) <= settings . standardNumDragHandlers ) {
2023-01-20 07:32:08 +00:00
global . BACK ( ) ;
2024-11-05 16:56:46 +00:00
E . stopEventPropagation ( ) ;
2023-01-20 07:32:08 +00:00
}
}
}
2023-01-22 08:47:48 +00:00
// Check if the back button should be enabled for the current app
// app is the src file of the app
2023-01-20 08:32:11 +00:00
function enabledForApp ( app ) {
if ( ! settings ) return true ;
2023-01-22 08:47:48 +00:00
if ( settings . mode === 0 ) {
2024-04-29 17:02:29 +00:00
return ! ( settings . apps . filter ( ( a ) => ( a . src === app ) || ( a . files && a . files . includes ( app ) ) ) . length > 0 ) ; // The `a.src===app` and `a.files&&...` checks are for backwards compatibility. Otherwise only `a.files.includes(app)` is needed.
2023-01-22 08:47:48 +00:00
} else if ( settings . mode === 1 ) {
2024-04-29 17:02:29 +00:00
return settings . apps . filter ( ( a ) => ( a . src === app ) || ( a . files && a . files . includes ( app ) ) ) . length > 0 ;
2023-01-20 08:32:11 +00:00
} else {
2023-01-22 08:47:48 +00:00
return settings . mode === 2 ? true : false ;
2023-01-20 08:32:11 +00:00
}
}
2023-01-22 08:47:48 +00:00
2023-01-20 07:32:08 +00:00
// Listen to left to right swipe
2024-11-05 16:56:46 +00:00
Bangle . prependListener ( "swipe" , goBack ) ;
2023-01-20 07:32:08 +00:00
} ) ( ) ;