|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { PluginListenerHandle } from '@capacitor/core' |
| 3 | +
|
| 4 | +import { App } from '@capacitor/app' |
| 5 | +import { Capacitor } from '@capacitor/core' |
| 6 | +import { LocalNotifications } from '@capacitor/local-notifications' |
| 7 | +import { useSettingsAudioDevice } from '@proj-airi/stage-ui/stores/settings' |
| 8 | +import { useLocalStorage } from '@vueuse/core' |
| 9 | +import { AndroidSettings, IOSSettings, NativeSettings } from 'capacitor-native-settings' |
| 10 | +import { storeToRefs } from 'pinia' |
| 11 | +import { onMounted, onUnmounted, shallowRef } from 'vue' |
| 12 | +import { useI18n } from 'vue-i18n' |
| 13 | +
|
| 14 | +import PermissionCard from './permission-card.vue' |
| 15 | +
|
| 16 | +import { MicrophonePermission } from '../../modules/microphone-permission' |
| 17 | +
|
| 18 | +const { t } = useI18n() |
| 19 | +const audioDeviceStore = useSettingsAudioDevice() |
| 20 | +const { permissionGranted: webMicrophonePermissionGranted } = storeToRefs(audioDeviceStore) |
| 21 | +
|
| 22 | +const isNativePlatform = Capacitor.isNativePlatform() |
| 23 | +const isAndroid = Capacitor.getPlatform() === 'android' |
| 24 | +
|
| 25 | +const notificationPermissionGranted = shallowRef(false) |
| 26 | +const platformMicrophonePermissionGranted = shallowRef(false) |
| 27 | +const requestingNotificationPermission = shallowRef(false) |
| 28 | +const requestingMicrophonePermission = shallowRef(false) |
| 29 | +const microphonePermissionRequested = useLocalStorage('permissions/microphone/requested', false) |
| 30 | +
|
| 31 | +let appStateListener: Promise<PluginListenerHandle> | undefined |
| 32 | +
|
| 33 | +async function refreshNotificationPermission() { |
| 34 | + const permission = await LocalNotifications.checkPermissions() |
| 35 | + notificationPermissionGranted.value = permission.display === 'granted' |
| 36 | +} |
| 37 | +
|
| 38 | +async function refreshMicrophonePermission() { |
| 39 | + if (isAndroid) { |
| 40 | + const permission = await MicrophonePermission.checkPermission() |
| 41 | + platformMicrophonePermissionGranted.value = permission.granted |
| 42 | + return |
| 43 | + } |
| 44 | +
|
| 45 | + const permission = await navigator.permissions?.query({ name: 'microphone' }).catch(() => undefined) |
| 46 | + platformMicrophonePermissionGranted.value = permission |
| 47 | + ? permission.state === 'granted' |
| 48 | + : webMicrophonePermissionGranted.value |
| 49 | +} |
| 50 | +
|
| 51 | +async function refreshPermissionStates() { |
| 52 | + await Promise.all([ |
| 53 | + refreshNotificationPermission().catch(error => console.error('Unable to refresh notification permission:', error)), |
| 54 | + refreshMicrophonePermission().catch(error => console.error('Unable to refresh microphone permission:', error)), |
| 55 | + ]) |
| 56 | +} |
| 57 | +
|
| 58 | +async function requestNotificationPermission() { |
| 59 | + requestingNotificationPermission.value = true |
| 60 | + try { |
| 61 | + const beforeRequest = await LocalNotifications.checkPermissions() |
| 62 | + if (beforeRequest.display === 'granted') { |
| 63 | + notificationPermissionGranted.value = true |
| 64 | + return |
| 65 | + } |
| 66 | +
|
| 67 | + if (beforeRequest.display === 'denied') { |
| 68 | + notificationPermissionGranted.value = false |
| 69 | + if (isNativePlatform) { |
| 70 | + await NativeSettings.open({ |
| 71 | + optionAndroid: AndroidSettings.AppNotification, |
| 72 | + optionIOS: IOSSettings.AppNotification, |
| 73 | + }) |
| 74 | + } |
| 75 | + return |
| 76 | + } |
| 77 | +
|
| 78 | + const requested = await LocalNotifications.requestPermissions() |
| 79 | + notificationPermissionGranted.value = requested.display === 'granted' |
| 80 | + } |
| 81 | + finally { |
| 82 | + requestingNotificationPermission.value = false |
| 83 | + } |
| 84 | +} |
| 85 | +
|
| 86 | +async function requestMicrophonePermission() { |
| 87 | + requestingMicrophonePermission.value = true |
| 88 | + try { |
| 89 | + await refreshMicrophonePermission() |
| 90 | + if (platformMicrophonePermissionGranted.value) |
| 91 | + return |
| 92 | +
|
| 93 | + if (microphonePermissionRequested.value && isNativePlatform) { |
| 94 | + await NativeSettings.open({ |
| 95 | + optionAndroid: AndroidSettings.ApplicationDetails, |
| 96 | + optionIOS: IOSSettings.App, |
| 97 | + }) |
| 98 | + return |
| 99 | + } |
| 100 | +
|
| 101 | + // Persist before requesting so later native clicks take the settings route, including after denial. |
| 102 | + microphonePermissionRequested.value = true |
| 103 | + await audioDeviceStore.askPermission() |
| 104 | + await refreshMicrophonePermission() |
| 105 | + } |
| 106 | + finally { |
| 107 | + requestingMicrophonePermission.value = false |
| 108 | + } |
| 109 | +} |
| 110 | +
|
| 111 | +onMounted(() => { |
| 112 | + void refreshPermissionStates() |
| 113 | +
|
| 114 | + if (isNativePlatform) { |
| 115 | + appStateListener = App.addListener('appStateChange', ({ isActive }) => { |
| 116 | + if (isActive) |
| 117 | + void refreshPermissionStates() |
| 118 | + }) |
| 119 | + } |
| 120 | +}) |
| 121 | +
|
| 122 | +onUnmounted(() => { |
| 123 | + void appStateListener?.then(listener => listener.remove()) |
| 124 | +}) |
| 125 | +</script> |
| 126 | + |
| 127 | +<template> |
| 128 | + <div :class="['flex flex-col gap-4']"> |
| 129 | + <PermissionCard |
| 130 | + :title="t('settings.dialogs.onboarding.permissions.notificationsTitle')" |
| 131 | + :description="t('settings.dialogs.onboarding.permissions.notificationsDescription')" |
| 132 | + :action-label="t('settings.dialogs.onboarding.permissions.requestAction')" |
| 133 | + :granted="notificationPermissionGranted" |
| 134 | + :disabled="requestingNotificationPermission" |
| 135 | + @request="requestNotificationPermission" |
| 136 | + /> |
| 137 | + |
| 138 | + <PermissionCard |
| 139 | + :title="t('settings.dialogs.onboarding.permissions.microphoneTitle')" |
| 140 | + :description="t('settings.dialogs.onboarding.permissions.microphoneDescription')" |
| 141 | + :action-label="t('settings.dialogs.onboarding.permissions.requestAction')" |
| 142 | + :granted="platformMicrophonePermissionGranted" |
| 143 | + :disabled="requestingMicrophonePermission" |
| 144 | + @request="requestMicrophonePermission" |
| 145 | + /> |
| 146 | + </div> |
| 147 | +</template> |
0 commit comments