Move the Capacitor code to the mobile overlay. [skip build]
Checks / checks (push) Successful in 2m6s
Trigger cinny-mobile / dispatch (push) Successful in 2s

This commit is contained in:
2026-11-02 01:07:20 +11:00
parent 1e3d630b1a
commit 8d6aaa619f
6 changed files with 5 additions and 1522 deletions
+2 -115
View File
@@ -109,20 +109,6 @@ export const isYouTubeStreamingAvailable = (): boolean => {
/** Callback for notification tap actions */
let notificationTapCallback: ((path: string) => void) | null = null;
const ANDROID_NOTIFICATION_SMALL_ICON = 'ic_stat_paarrot';
const ANDROID_NOTIFICATION_ICON_COLOR = '#FF8A00';
const CAPACITOR_LOCAL_NOTIFICATIONS_MODULE_ID = '@capacitor/local-notifications';
type CapacitorLocalNotificationsModule = typeof import('@capacitor/local-notifications');
/**
* Lazy-load Capacitor local notifications at runtime only.
* Vite must not pre-resolve this module because some desktop builds do not include it.
*/
const importCapacitorLocalNotifications = async (): Promise<CapacitorLocalNotificationsModule> =>
import(/* @vite-ignore */ CAPACITOR_LOCAL_NOTIFICATIONS_MODULE_ID);
/**
* Bring the app window to the front and focus it
*/
@@ -166,24 +152,6 @@ export const setupNotificationTapListener = async (
return;
}
}
if (isCapacitorNative()) {
try {
const { LocalNotifications } = await importCapacitorLocalNotifications();
await LocalNotifications.addListener(
'localNotificationActionPerformed',
async (event: any) => {
await focusWindow();
const path = event?.notification?.extra?.path;
if (path && typeof path === 'string' && notificationTapCallback) {
notificationTapCallback(path);
}
},
);
} catch (err) {
console.warn('Failed to set up Capacitor notification tap listener:', err);
}
}
};
/**
@@ -264,32 +232,8 @@ const sendBrowserNotification = (options: {
return notification;
};
/** Flag to track if notification channel has been created on Android */
let notificationChannelCreated = false;
const ensureCapacitorNotificationChannel = async (): Promise<void> => {
if (notificationChannelCreated || !isAndroid()) return;
try {
const { LocalNotifications } = await importCapacitorLocalNotifications();
await LocalNotifications.createChannel({
id: 'messages',
name: 'Messages',
description: 'Message notifications from Matrix',
importance: 5,
sound: 'default',
visibility: 1,
vibration: true,
lights: true,
});
notificationChannelCreated = true;
} catch (err) {
console.warn('Failed to create Capacitor notification channel:', err);
}
};
/**
* Request notification permission across Electron, Tauri, Capacitor, and browser
* Request notification permission across Electron, Tauri, and browser
*/
export const requestSystemNotificationPermission = async (): Promise<boolean> => {
if (isElectron()) {
@@ -298,37 +242,12 @@ export const requestSystemNotificationPermission = async (): Promise<boolean> =>
return permission === 'granted';
}
if (isCapacitorNative()) {
try {
const { LocalNotifications } = await importCapacitorLocalNotifications();
let perm = await LocalNotifications.checkPermissions();
if (perm.display !== 'granted') {
perm = await LocalNotifications.requestPermissions();
}
return perm.display === 'granted';
} catch (err) {
console.warn('Capacitor notification permission request failed:', err);
return false;
}
}
if (!('Notification' in window)) return false;
const permission = await Notification.requestPermission();
return permission === 'granted';
};
export const getSystemNotificationPermissionState = async (): Promise<PermissionState> => {
if (isCapacitorNative()) {
try {
const { LocalNotifications } = await importCapacitorLocalNotifications();
const perm = await LocalNotifications.checkPermissions();
return perm.display === 'granted' ? 'granted' : 'prompt';
} catch (err) {
console.warn('Failed to check Capacitor notification permission:', err);
return 'denied';
}
}
if ('Notification' in window) {
if (window.Notification.permission === 'default') {
return 'prompt';
@@ -340,7 +259,7 @@ export const getSystemNotificationPermissionState = async (): Promise<Permission
};
/**
* Send a notification on Electron or Capacitor, falling back to the browser Notification API
* Send a notification on Electron, falling back to the browser Notification API
*/
export const sendNotification = async (options: {
title: string;
@@ -369,38 +288,6 @@ export const sendNotification = async (options: {
}
}
if (isCapacitorNative()) {
try {
const { LocalNotifications } = await importCapacitorLocalNotifications();
let perm = await LocalNotifications.checkPermissions();
if (perm.display !== 'granted') {
perm = await LocalNotifications.requestPermissions();
}
if (perm.display === 'granted') {
await ensureCapacitorNotificationChannel();
const id = Math.floor(Date.now() % 2147483647);
await LocalNotifications.schedule({
notifications: [
{
id,
title,
body,
channelId: isAndroid() ? 'messages' : undefined,
smallIcon: isAndroid() ? ANDROID_NOTIFICATION_SMALL_ICON : undefined,
iconColor: isAndroid() ? ANDROID_NOTIFICATION_ICON_COLOR : undefined,
extra: path ? { path } : undefined,
},
],
});
}
return;
} catch (err) {
console.warn('Capacitor notification failed:', err);
}
}
// Fallback to browser notification
sendBrowserNotification({ title, body, icon, onClick });
};