nuxt-start/plugins/capacitor.client.ts
2026-01-03 16:12:05 +01:00

126 lines
3.6 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(async (nuxtApp) => {
const siteData = ref()
const categoriesStore = useCategoriesStore()
const placeStore = useMyPlacesStore()
const authStore = useAuthStore()
const configStore = useMyConfigStore()
const { loadToken, getToken } = useAuthToken()
const { apiFetch } = useApi()
const token = await loadToken()
console.log(token)
const { data } = await apiFetch('/init',
{
headers: {
'auth-key': token || ''
}
}
)
if (data.value?.user) {
authStore.user = data.value.user
}
if (data.value?.config) {
configStore.config = data.value.config
}
if (data.value?.states) {
configStore.states = data.value.states
}
if (data.value?.categories) {
categoriesStore.categories = data.value.categories
placeStore.places = data.value.places
}
if (data.value?.countries) {
configStore.countries = data.value.countries
}
// 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') {
await StatusBar.setBackgroundColor({ color: '#262626' }) // surface-950
await StatusBar.setStyle({ style: Style.Dark }) // világos 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 }),
}
}
}
})