2017-11-03 02:09:34 +02:00
|
|
|
const { BackHandler } = require('react-native');
|
2017-08-24 20:10:03 +02:00
|
|
|
|
|
|
|
class BackButtonService {
|
|
|
|
static initialize(defaultHandler) {
|
|
|
|
this.defaultHandler_ = defaultHandler;
|
|
|
|
|
|
|
|
BackHandler.addEventListener('hardwareBackPress', async () => {
|
|
|
|
return this.back();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static async back() {
|
|
|
|
if (this.handlers_.length) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const r = await this.handlers_[this.handlers_.length - 1]();
|
2017-08-24 20:10:03 +02:00
|
|
|
if (r) return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.defaultHandler_();
|
|
|
|
}
|
|
|
|
|
|
|
|
static addHandler(handler) {
|
|
|
|
for (let i = this.handlers_.length - 1; i >= 0; i--) {
|
|
|
|
const h = this.handlers_[i];
|
|
|
|
if (h === handler) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.handlers_.push(handler);
|
|
|
|
}
|
|
|
|
|
2024-02-26 18:53:48 +02:00
|
|
|
static removeHandler(handler) {
|
2017-08-24 20:10:03 +02:00
|
|
|
for (let i = this.handlers_.length - 1; i >= 0; i--) {
|
|
|
|
const h = this.handlers_[i];
|
2024-02-26 18:53:48 +02:00
|
|
|
if (h === handler) this.handlers_.splice(i, 1);
|
2017-08-24 20:10:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BackButtonService.defaultHandler_ = null;
|
|
|
|
BackButtonService.handlers_ = [];
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { BackButtonService };
|