• There is an app (swp2clk) which allows the user to swipe back to the clock. To avoid interfering with apps that use the swipe gesture for their own purposes, you have to use the settings menu to configure which apps get the treatment. I like swiping back to the clock, but I find this configuration to be annoying (and don't like that swp2clk writes the app name to storage every time an app is loaded), so I'm trying to make a similar app that does this automatically: apps that do not register any swipe handlers will have swiping go back to the clock, and apps that do register swipe handlers will just have their swipe handlers run as usual.

    To do this, I'm trying to override Bangle.on(). The custom Bangle.on will pass all calls with an event type other than 'swipe' to the native Bangle.on (which I will store somewhere else) to avoid unnecessary complexity, but when the event type is 'swipe' the supplied function will be stored in an array. The native (non-overridden) Bangle.on will be called with an event type of 'swipe' and a function that will either call load() if there are no functions in the array, or call each function in the array (and not call load) if there's at least one. This should result in things appearing mostly unchanged, except for the illusion of a "default" swipe handler that goes back to the clock.

    I have developed the following code (autoswp2clk.boot.js) to try to achieve this:

    const AUTOSWP2CLK = {
      on: Bangle.on,                                  // Original, unmodified Bangle.on()
      removeListener: Bangle.removeListener,          // Original Bangle.removeListener()
      removeAllListeners: Bangle.removeAllListeners,  // Original Bangle.removeAllListeners()
      swipeHandlers: []                               // Array of swipe handlers
    };
    
    Bangle.on = (eventName, func) => {
      if (eventName == 'swipe') {
        AUTOSWP2CLK.swipeHandlers.push(func);
      } else {
        AUTOSWP2CLK.on(eventName, func);
      }
    };
    
    Bangle.removeListener = (eventName, func) => {
      if (eventName == 'swipe') {
        AUTOSWP2CLK.swipeHandlers.splice(AUTOSWP­2CLK.swipeHandlers.indexOf(func), 1);
      } else {
        AUTOSWP2CLK.removeListener(eventName, func);
      }
    };
    
    Bangle.removeAllListeners = (eventName) => {
      if (eventName == 'swipe') {
        AUTOSWP2CLK.swipeHandlers = [];
      } else {
        AUTOSWP2CLK.removeAllListeners(eventName­);
      }
    };
    
    // Using the old Bangle.on rather than the new one
    AUTOSWP2CLK.on('swipe', (dirLR, dirUD) => {
      if (AUTOSWP2CLK.swipeHandlers.length == 0) {
        load();
      } else {
        for (let func of AUTOSWP2CLK.swipeHandlers) {
          func(dirLR, dirUD);
        }
      }
    });
    

    (I also have a removeListener and removeAllListeners for compatibility, but have not tested them yet.)

    However, what actually happens is that this app breaks basically everything, because Bangle.on() no longer registers event listeners. If I manually call AUTOSWP2CLK.on() which should be the unmodified Bangle.on(), it also does nothing, implying that this has broken it. Is what I'm trying to do possible and I'm just doing it wrong, or is this not possible?

About

Avatar for user141569 @user141569 started