import { Capacitor } from "@capacitor/core"; import { QRScannerService } from "./types"; import { CapacitorQRScanner } from "./CapacitorQRScanner"; import { WebDialogQRScanner } from "./WebDialogQRScanner"; import { logger } from "@/utils/logger"; /** * Factory class for creating QR scanner instances based on platform */ export class QRScannerFactory { private static instance: QRScannerService | null = null; private static isNativePlatform(): boolean { const capacitorNative = Capacitor.isNativePlatform(); const isMobile = __IS_MOBILE__; const platform = Capacitor.getPlatform(); logger.log('Platform detection:', { capacitorNative, isMobile, platform, userAgent: navigator.userAgent }); // Force native scanner on Android/iOS if (platform === 'android' || platform === 'ios') { return true; } return capacitorNative || isMobile; } /** * Get a QR scanner instance appropriate for the current platform */ static getInstance(): QRScannerService { if (!this.instance) { const isNative = this.isNativePlatform(); logger.log(`Creating QR scanner for platform: ${isNative ? 'native' : 'web'}`); if (isNative) { logger.log('Using native MLKit scanner'); this.instance = new CapacitorQRScanner(); } else if (__USE_QR_READER__) { logger.log('Using web QR scanner'); this.instance = new WebDialogQRScanner(); } else { throw new Error("No QR scanner implementation available for this platform"); } } return this.instance!; // We know it's not null here } /** * Clean up the current scanner instance */ static async cleanup(): Promise { if (this.instance) { await this.instance.cleanup(); this.instance = null; } } }