1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-12 22:57:38 +02:00

Improved back button handling when note has been modified

This commit is contained in:
Laurent Cozic
2017-08-24 18:10:03 +00:00
parent d9c85a7275
commit 7aaf4fb491
6 changed files with 106 additions and 26 deletions

View File

@ -0,0 +1,43 @@
import { BackHandler } from 'react-native';
class BackButtonService {
static initialize(defaultHandler) {
this.defaultHandler_ = defaultHandler;
BackHandler.addEventListener('hardwareBackPress', async () => {
return this.back();
});
}
static async back() {
if (this.handlers_.length) {
let r = await this.handlers_[this.handlers_.length - 1]();
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);
}
static removeHandler(hanlder) {
for (let i = this.handlers_.length - 1; i >= 0; i--) {
const h = this.handlers_[i];
if (h === hanlder) this.handlers_.splice(i, 1);
}
}
}
BackButtonService.defaultHandler_ = null;
BackButtonService.handlers_ = [];
export { BackButtonService };