diff --git a/src/app/utils/backgroundSync.ts b/src/app/utils/backgroundSync.ts index 9f31676..dbe334c 100644 --- a/src/app/utils/backgroundSync.ts +++ b/src/app/utils/backgroundSync.ts @@ -1,6 +1,13 @@ -import { Capacitor, registerPlugin, type PluginListenerHandle } from '@capacitor/core'; +import type { PluginListenerHandle } from '@capacitor/core'; import type { IPusherRequest, MatrixClient } from 'matrix-js-sdk'; +const CAPACITOR_CORE_MODULE_ID = '@capacitor/core'; + +type CapacitorCoreModule = typeof import('@capacitor/core'); + +const loadCapacitorCore = async (): Promise => + import(/* @vite-ignore */ CAPACITOR_CORE_MODULE_ID); + type UnifiedPushStatus = { running: boolean; endpoint: string; @@ -67,14 +74,15 @@ type StoredPusherState = { appId: string; }; -const MatrixBackgroundSync = registerPlugin('MatrixBackgroundSync'); const DEFAULT_UNIFIED_PUSH_GATEWAY = 'https://matrix.gateway.unifiedpush.org/_matrix/push/v1/notify'; const PUSHER_APP_ID_BASE = 'com.paarrot.app.android'; const PUSHER_STORAGE_PREFIX = 'paarrot.unifiedpush'; /** Returns true when the current platform is Android Capacitor. */ -export const isBackgroundSyncSupported = (): boolean => - Capacitor.isNativePlatform() && Capacitor.getPlatform() === 'android'; +export const isBackgroundSyncSupported = (): boolean => { + const capacitor = (window as typeof window & { Capacitor?: CapacitorCoreModule['Capacitor'] }).Capacitor; + return Boolean(capacitor?.isNativePlatform?.() && capacitor.getPlatform() === 'android'); +}; const getStoredPusherKey = (userId: string | null, deviceId: string | null): string => `${PUSHER_STORAGE_PREFIX}:${userId ?? 'unknown'}:${deviceId ?? 'unknown'}`; @@ -169,6 +177,9 @@ class AndroidUnifiedPushManager { /** Start native UnifiedPush registration and synchronize the Matrix pusher. */ async start(mx: MatrixClient): Promise { + const { Capacitor, registerPlugin } = await loadCapacitorCore(); + const MatrixBackgroundSync = registerPlugin('MatrixBackgroundSync'); + console.log('[BackgroundSync] start() called, platform:', Capacitor.getPlatform(), 'isNative:', Capacitor.isNativePlatform()); if (!isBackgroundSyncSupported()) { @@ -216,6 +227,9 @@ class AndroidUnifiedPushManager { async stop(): Promise { if (!isBackgroundSyncSupported()) return; + const { registerPlugin } = await loadCapacitorCore(); + const MatrixBackgroundSync = registerPlugin('MatrixBackgroundSync'); + const client = this.client; if (client) { const deviceId = client.getDeviceId(); @@ -280,6 +294,8 @@ class AndroidUnifiedPushManager { /** Re-opens distributor selection once after ACTION_REQUIRED and logs actionable status. */ private async tryRequestDistributorSetup(): Promise { try { + const { registerPlugin } = await loadCapacitorCore(); + const MatrixBackgroundSync = registerPlugin('MatrixBackgroundSync'); const setupResult = await MatrixBackgroundSync.requestDistributorSetup(); console.warn('[BackgroundSync] requestDistributorSetup result:', setupResult); const status = await this.safeGetStatus(); @@ -335,6 +351,9 @@ class AndroidUnifiedPushManager { private async ensureListeners(): Promise { if (this.listenersReady) return; + const { registerPlugin } = await loadCapacitorCore(); + const MatrixBackgroundSync = registerPlugin('MatrixBackgroundSync'); + this.listenerHandles = [ await MatrixBackgroundSync.addListener('unifiedPushNewEndpoint', (event) => { void this.handleNewEndpoint(event).catch((err) => { @@ -363,6 +382,9 @@ class AndroidUnifiedPushManager { const client = this.client; if (!client) return; + const { registerPlugin } = await loadCapacitorCore(); + const MatrixBackgroundSync = registerPlugin('MatrixBackgroundSync'); + const status = await this.safeGetStatus(); console.log('[BackgroundSync] Native status before pusher sync:', status); if (status?.registered && status.endpoint) { @@ -435,6 +457,8 @@ class AndroidUnifiedPushManager { /** Read native registration state without failing the caller. */ private async safeGetStatus(): Promise { try { + const { registerPlugin } = await loadCapacitorCore(); + const MatrixBackgroundSync = registerPlugin('MatrixBackgroundSync'); return await MatrixBackgroundSync.getStatus(); } catch (err) { console.warn('[BackgroundSync] Failed to read UnifiedPush status:', err); @@ -458,6 +482,8 @@ export const triggerBackgroundSyncPing = async (reason?: string): Promise if (!isBackgroundSyncSupported()) return; try { + const { registerPlugin } = await loadCapacitorCore(); + const MatrixBackgroundSync = registerPlugin('MatrixBackgroundSync'); await MatrixBackgroundSync.triggerPing({ reason }); } catch (err) { console.warn('[BackgroundSync] triggerPing failed:', err); @@ -479,6 +505,8 @@ export const setAppForegroundState = async (foreground: boolean): Promise if (!isBackgroundSyncSupported()) return; try { + const { registerPlugin } = await loadCapacitorCore(); + const MatrixBackgroundSync = registerPlugin('MatrixBackgroundSync'); await MatrixBackgroundSync.setAppForeground({ foreground }); } catch (err) { console.warn('[BackgroundSync] setAppForeground failed:', err); @@ -497,6 +525,8 @@ export const requestResetPushRegistration = async (): Promise<{ success: boolean } try { + const { registerPlugin } = await loadCapacitorCore(); + const MatrixBackgroundSync = registerPlugin('MatrixBackgroundSync'); const result = await MatrixBackgroundSync.requestDistributorSetup(); console.log('[BackgroundSync] requestDistributorSetup completed:', result); return result ?? { success: false }; @@ -510,6 +540,8 @@ export const requestResetPushRegistration = async (): Promise<{ success: boolean export const getBackgroundSyncStatus = async (): Promise => { if (!isBackgroundSyncSupported()) return undefined; try { + const { registerPlugin } = await loadCapacitorCore(); + const MatrixBackgroundSync = registerPlugin('MatrixBackgroundSync'); return await MatrixBackgroundSync.getStatus(); } catch (err) { console.warn('[BackgroundSync] getStatus failed:', err);