feat: Refactor Android share and background sync utilities; streamline notification handling with dynamic imports

This commit is contained in:
2026-04-28 15:46:19 +10:00
parent ef0aa2a847
commit ffec244113
3 changed files with 75 additions and 533 deletions

View File

@@ -119,6 +119,48 @@ let notificationTapCallback: ((path: string) => void) | null = null;
const ANDROID_NOTIFICATION_SMALL_ICON = 'ic_stat_paarrot';
const ANDROID_NOTIFICATION_ICON_COLOR = '#FF8A00';
type CapacitorPermissionResult = {
display: string;
};
type CapacitorLocalNotificationChannel = {
id: string;
name: string;
description: string;
importance: number;
sound: string;
visibility: number;
vibration: boolean;
lights: boolean;
};
type CapacitorLocalNotificationItem = {
id: number;
title: string;
body: string;
channelId?: string;
smallIcon?: string;
iconColor?: string;
extra?: { path: string };
};
type CapacitorLocalNotificationsApi = {
addListener: (
eventName: string,
listenerFunc: (event: any) => void | Promise<void>
) => Promise<unknown>;
createChannel: (channel: CapacitorLocalNotificationChannel) => Promise<void>;
checkPermissions: () => Promise<CapacitorPermissionResult>;
requestPermissions: () => Promise<CapacitorPermissionResult>;
schedule: (options: { notifications: CapacitorLocalNotificationItem[] }) => Promise<void>;
};
const importCapacitorLocalNotifications = async (): Promise<CapacitorLocalNotificationsApi> => {
const pkg = '@capacitor' + '/local-notifications';
const mod = (await import(pkg)) as { LocalNotifications: CapacitorLocalNotificationsApi };
return mod.LocalNotifications;
};
/**
* Bring the Tauri window to the front and focus it
*/
@@ -196,7 +238,7 @@ export const setupNotificationTapListener = async (onTap: (path: string) => void
if (isCapacitorNative()) {
try {
const { LocalNotifications } = await import('@capacitor/local-notifications');
const LocalNotifications = await importCapacitorLocalNotifications();
await LocalNotifications.addListener('localNotificationActionPerformed', async (event: any) => {
await focusWindow();
const path = event?.notification?.extra?.path;
@@ -336,7 +378,7 @@ const ensureCapacitorNotificationChannel = async (): Promise<void> => {
if (notificationChannelCreated || !isAndroid()) return;
try {
const { LocalNotifications } = await import('@capacitor/local-notifications');
const LocalNotifications = await importCapacitorLocalNotifications();
await LocalNotifications.createChannel({
id: 'messages',
name: 'Messages',
@@ -365,7 +407,7 @@ export const requestSystemNotificationPermission = async (): Promise<boolean> =>
if (isCapacitorNative()) {
try {
const { LocalNotifications } = await import('@capacitor/local-notifications');
const LocalNotifications = await importCapacitorLocalNotifications();
let perm = await LocalNotifications.checkPermissions();
if (perm.display !== 'granted') {
perm = await LocalNotifications.requestPermissions();
@@ -385,7 +427,7 @@ export const requestSystemNotificationPermission = async (): Promise<boolean> =>
export const getSystemNotificationPermissionState = async (): Promise<PermissionState> => {
if (isCapacitorNative()) {
try {
const { LocalNotifications } = await import('@capacitor/local-notifications');
const LocalNotifications = await importCapacitorLocalNotifications();
const perm = await LocalNotifications.checkPermissions();
return perm.display === 'granted' ? 'granted' : 'prompt';
} catch (err) {
@@ -471,7 +513,7 @@ export const sendNotification = async (options: {
if (isCapacitorNative()) {
try {
const { LocalNotifications } = await import('@capacitor/local-notifications');
const LocalNotifications = await importCapacitorLocalNotifications();
let perm = await LocalNotifications.checkPermissions();
if (perm.display !== 'granted') {
perm = await LocalNotifications.requestPermissions();