Android-Back: @capacitor/app verdrahtet, Zurueck-Geste beendet die App nicht mehr
Capacitor 7 behandelt Zurueck gar nicht selbst, das macht @capacitor/app -- das Plugin fehlte, also lief jede Zurueck-Geste an der WebView vorbei direkt in finish(). Die History-Eintraege pro Sheet (#63) haben nur im Browser gewirkt. Neu: lib/back.js entscheidet Sheet -> Seite -> zweimal Zurueck zum Beenden (exitApp, weil das Plugin mit registriertem Listener nie mehr selbst beendet), gesperrte Sheets schlucken Zurueck weiterhin. Version auf 1.2.8/versionCode 9.
This commit is contained in:
1 parent
e69673d3cd
commit
19a441d93e
21 files changed
+215
-5
No files matched your search
@@ -0,0 +1,62 @@
|
||||
// Android system back (the gesture and the hardware/nav button are the same event).
|
||||
//
|
||||
// Capacitor's BridgeActivity does not handle back at all — without a JS listener the
|
||||
// activity simply finishes, so a back swipe quit the app from anywhere, whatever the
|
||||
// WebView history said. That made the sheet history entries from #63 dead weight in the
|
||||
// packaged app: they only ever worked in a browser tab, where the browser walks history
|
||||
// itself.
|
||||
//
|
||||
// Once a `backButton` listener exists, the plugin stops falling through to finish() —
|
||||
// back can then ONLY leave the app through App.exitApp(), which is why the last step is
|
||||
// an explicit press-again-to-exit rather than a silent no-op.
|
||||
import { MOBILE } from './mobile.js'
|
||||
import { useUI } from '../store/useUI.js'
|
||||
import { t } from './i18n.js'
|
||||
|
||||
// How long a first back press stays armed as "press again to exit" (the toast shows 2.2s).
|
||||
export const EXIT_WINDOW = 2000
|
||||
|
||||
// What a back press means, given what is on screen. Pure, so the table below is testable.
|
||||
export function decideBack({ top, canGoBack, exitArmed }) {
|
||||
if (top) return top.locked ? 'ignore' : 'sheet' // a locked sheet swallows back on purpose
|
||||
if (canGoBack) return 'history' // a page/tab we pushed ourselves
|
||||
return exitArmed ? 'exit' : 'arm-exit'
|
||||
}
|
||||
|
||||
export function makeBackHandler({ getSheets, closeSheet, toast, goBack, exit, now = Date.now }) {
|
||||
let armedAt = -Infinity
|
||||
return (ev = {}) => {
|
||||
const sheets = getSheets()
|
||||
const action = decideBack({
|
||||
top: sheets[sheets.length - 1],
|
||||
canGoBack: !!ev.canGoBack,
|
||||
exitArmed: now() - armedAt < EXIT_WINDOW,
|
||||
})
|
||||
if (action !== 'arm-exit') armedAt = -Infinity
|
||||
if (action === 'sheet') closeSheet(sheets[sheets.length - 1].id)
|
||||
else if (action === 'history') goBack()
|
||||
else if (action === 'arm-exit') { armedAt = now(); toast(t('Press back again to exit')) }
|
||||
else if (action === 'exit') exit()
|
||||
return action
|
||||
}
|
||||
}
|
||||
|
||||
// Registers the listener for the native shell. No-op in web builds, where the browser
|
||||
// (or the PWA shell) walks history on its own and there is no plugin to talk to.
|
||||
export async function initBackButton() {
|
||||
if (!MOBILE) return () => {}
|
||||
try {
|
||||
const { App } = await import('@capacitor/app')
|
||||
const handler = makeBackHandler({
|
||||
getSheets: () => useUI.getState().sheets,
|
||||
closeSheet: id => useUI.getState().closeSheet(id),
|
||||
toast: msg => useUI.getState().toast(msg),
|
||||
goBack: () => window.history.back(),
|
||||
exit: () => App.exitApp(),
|
||||
})
|
||||
const sub = await App.addListener('backButton', handler)
|
||||
return () => sub.remove()
|
||||
} catch (e) {
|
||||
return () => {} // no plugin (older install): leave back to the default behaviour
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { decideBack, makeBackHandler, EXIT_WINDOW } from './back.js'
|
||||
|
||||
// Back on Android has to serve three layers with one event: the sheet stack on top, the
|
||||
// page history under it, and — because a registered listener stops the plugin from ever
|
||||
// finishing the activity itself — a deliberate way out of the app at the bottom.
|
||||
describe('decideBack', () => {
|
||||
it('dismisses the top sheet before touching history', () => {
|
||||
expect(decideBack({ top: { id: 's1' }, canGoBack: true, exitArmed: false })).toBe('sheet')
|
||||
})
|
||||
|
||||
it('lets a locked sheet swallow the press', () => {
|
||||
expect(decideBack({ top: { id: 's1', locked: true }, canGoBack: true, exitArmed: true })).toBe('ignore')
|
||||
})
|
||||
|
||||
it('walks history when nothing is open', () => {
|
||||
expect(decideBack({ top: undefined, canGoBack: true, exitArmed: false })).toBe('history')
|
||||
})
|
||||
|
||||
it('arms the exit at the bottom of the stack, and exits on the second press', () => {
|
||||
expect(decideBack({ top: undefined, canGoBack: false, exitArmed: false })).toBe('arm-exit')
|
||||
expect(decideBack({ top: undefined, canGoBack: false, exitArmed: true })).toBe('exit')
|
||||
})
|
||||
})
|
||||
|
||||
function harness() {
|
||||
const calls = { closed: [], toasts: [], back: 0, exit: 0 }
|
||||
let sheets = []
|
||||
let now = 1000
|
||||
const handler = makeBackHandler({
|
||||
getSheets: () => sheets,
|
||||
closeSheet: id => { calls.closed.push(id); sheets = sheets.filter(s => s.id !== id) },
|
||||
toast: m => calls.toasts.push(m),
|
||||
goBack: () => { calls.back++ },
|
||||
exit: () => { calls.exit++ },
|
||||
now: () => now,
|
||||
})
|
||||
return { calls, handler, setSheets: s => { sheets = s }, tick: ms => { now += ms } }
|
||||
}
|
||||
|
||||
describe('makeBackHandler', () => {
|
||||
it('closes stacked sheets one press at a time', () => {
|
||||
const h = harness()
|
||||
h.setSheets([{ id: 'a' }, { id: 'b' }])
|
||||
expect(h.handler({ canGoBack: true })).toBe('sheet')
|
||||
expect(h.handler({ canGoBack: true })).toBe('sheet')
|
||||
expect(h.calls.closed).toEqual(['b', 'a'])
|
||||
expect(h.calls.back).toBe(0)
|
||||
})
|
||||
|
||||
it('does not close a locked sheet, and does not arm the exit behind it', () => {
|
||||
const h = harness()
|
||||
h.setSheets([{ id: 'a', locked: true }])
|
||||
expect(h.handler({ canGoBack: false })).toBe('ignore')
|
||||
expect(h.calls.closed).toEqual([])
|
||||
expect(h.calls.toasts).toEqual([])
|
||||
expect(h.calls.exit).toBe(0)
|
||||
})
|
||||
|
||||
it('goes back a page while the WebView still has app history', () => {
|
||||
const h = harness()
|
||||
expect(h.handler({ canGoBack: true })).toBe('history')
|
||||
expect(h.calls.back).toBe(1)
|
||||
expect(h.calls.exit).toBe(0)
|
||||
})
|
||||
|
||||
// The whole point of the fix: one press at the root must never be the end of the app.
|
||||
it('warns once, then exits on a second press inside the window', () => {
|
||||
const h = harness()
|
||||
expect(h.handler({ canGoBack: false })).toBe('arm-exit')
|
||||
expect(h.calls.toasts).toHaveLength(1)
|
||||
expect(h.calls.exit).toBe(0)
|
||||
h.tick(EXIT_WINDOW - 1)
|
||||
expect(h.handler({ canGoBack: false })).toBe('exit')
|
||||
expect(h.calls.exit).toBe(1)
|
||||
})
|
||||
|
||||
it('re-arms rather than exiting once the window has passed', () => {
|
||||
const h = harness()
|
||||
h.handler({ canGoBack: false })
|
||||
h.tick(EXIT_WINDOW + 1)
|
||||
expect(h.handler({ canGoBack: false })).toBe('arm-exit')
|
||||
expect(h.calls.exit).toBe(0)
|
||||
expect(h.calls.toasts).toHaveLength(2)
|
||||
})
|
||||
|
||||
// A sheet opened (or a page pushed) after the warning means the user is still using the
|
||||
// app — the next back belongs to that sheet, and the one after must warn again.
|
||||
it('disarms the exit as soon as another press does real work', () => {
|
||||
const h = harness()
|
||||
h.handler({ canGoBack: false })
|
||||
h.setSheets([{ id: 'a' }])
|
||||
expect(h.handler({ canGoBack: true })).toBe('sheet')
|
||||
expect(h.handler({ canGoBack: false })).toBe('arm-exit')
|
||||
expect(h.calls.exit).toBe(0)
|
||||
})
|
||||
|
||||
it('treats a missing event payload as no history', () => {
|
||||
const h = harness()
|
||||
expect(h.handler()).toBe('arm-exit')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user