const UPower = imports.ui.status.power.UPower
const Main = imports.ui.main
const Lang = imports.lang

let battery = {
  watching: null,

  bind () {
    this.getBattery(proxy => {
      let update = Lang.bind(this, this.update)
      this.watching = proxy.connect('g-properties-changed', update)
    })
  },

  unbind () {
    this.getBattery(proxy => {
      proxy.disconnect(this.watching)
    })
  },

  show () {
    this.getBattery((proxy, icon) => {
      icon.show()
    })
  },

  hide () {
    this.getBattery((proxy, icon) => {
      icon.hide()
    })
  },

  update () {
    this.getBattery(p => {
      if (p.Type === UPower.DeviceKind.BATTERY && p.Percentage === 100) {
        this.hide()
      } else {
        this.show()
      }
    })
  },

  getBattery (callback) {
    let menu = Main.panel.statusArea.aggregateMenu
    if (menu && menu._power) {
      callback(menu._power._proxy, menu._power.indicators)
    }
  }
}

/* exported init, enable, disable */

function init () { }

function enable () {
  battery.bind().update()
}

function disable () {
  battery.unbind().show()
}

const UPower = imports.ui.status.power.UPower
const Main = imports.ui.main

let watching

function bind () {
  getBattery(proxy => {
    watching = proxy.connect('g-properties-changed', update)
  })
}

function unbind () {
  getBattery(proxy => {
    proxy.disconnect(watching)
  })
}

function show () {
  getBattery((proxy, icon) => {
    icon.show()
  })
}

function hide () {
  getBattery((proxy, icon) => {
    icon.hide()
  })
}

function update () {
  getBattery(proxy => {
    if (proxy.Type === UPower.DeviceKind.BATTERY && proxy.Percentage === 100) {
      hide()
    } else {
      show()
    }
  })
}

function getBattery (callback) {
  let menu = Main.panel.statusArea.aggregateMenu
  if (menu && menu._power) {
    callback(menu._power._proxy, menu._power.indicators)
  }
}

/* exported init, enable, disable */

function init () { }

function enable () {
  bind()
  update()
}

function disable () {
  unbind()
  show()
}

Clean up code