nuxt-start/plugins/capacitor.client.ts
2025-12-26 11:46:24 +01:00

94 lines
3.0 KiB
TypeScript

import { App } from '@capacitor/app'
import { StatusBar, Style } from '@capacitor/status-bar'
import { SplashScreen } from '@capacitor/splash-screen'
import { Haptics, ImpactStyle } from '@capacitor/haptics'
import { Capacitor } from '@capacitor/core'
export default defineNuxtPlugin((nuxtApp) => {
// Csak natív platformon fut
if (!Capacitor.isNativePlatform()) {
return
}
const router = useRouter()
// Android hardware back button kezelése
App.addListener('backButton', async ({ canGoBack }) => {
// Ha van history, navigáljunk vissza
if (router.currentRoute.value.path !== '/' && window.history.length > 1) {
router.back()
} else {
// Ha a főoldalon vagyunk, minimalizáljuk az appot
App.minimizeApp()
}
})
// Status bar beállítása dark/light mode alapján
const setupStatusBar = async (isDark: boolean) => {
try {
if (Capacitor.getPlatform() === 'android') {
if (isDark) {
// Dark mode: sötét háttér, világos ikonok
await StatusBar.setBackgroundColor({ color: '#0a0a0a' }) // surface-950
await StatusBar.setStyle({ style: Style.Dark }) // világos ikonok
} else {
// Light mode: világos háttér, sötét ikonok
await StatusBar.setBackgroundColor({ color: '#fafafa' }) // surface-50
await StatusBar.setStyle({ style: Style.Light }) // sötét ikonok
}
}
} catch (e) {
console.warn('StatusBar setup error:', e)
}
}
// Splash screen elrejtése amikor az app betöltődött
const hideSplash = async () => {
try {
await SplashScreen.hide({
fadeOutDuration: 300
})
} catch (e) {
console.warn('SplashScreen hide error:', e)
}
}
// Dark mode figyelése
const darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)')
// Dark mode változás figyelése
darkModeQuery.addEventListener('change', (e) => {
setupStatusBar(e.matches)
})
// App mounted hook
nuxtApp.hooks.hook('app:mounted', async () => {
// Kezdeti beállítás a jelenlegi dark mode állapot alapján
await setupStatusBar(darkModeQuery.matches)
// Kis késleltetés hogy a UI renderelődjön
setTimeout(() => {
hideSplash()
}, 500)
})
// Haptics helper függvények exportálása
return {
provide: {
haptics: {
// Könnyű rezgés (pl. gomb kattintás)
light: () => Haptics.impact({ style: ImpactStyle.Light }),
// Közepes rezgés (pl. toggle váltás)
medium: () => Haptics.impact({ style: ImpactStyle.Medium }),
// Erős rezgés (pl. sikeres művelet)
heavy: () => Haptics.impact({ style: ImpactStyle.Heavy }),
// Szelekció rezgés
selection: () => Haptics.selectionStart(),
// Értesítés rezgések
success: () => Haptics.notification({ type: 'SUCCESS' as any }),
warning: () => Haptics.notification({ type: 'WARNING' as any }),
error: () => Haptics.notification({ type: 'ERROR' as any }),
}
}
}
})