class PWAInstallPrompt { constructor() { this.deferredPrompt = null; this.isInstalled = false; this.installButton = null; this.installBanner = null; this.dismissedCount = 0; this.maxDismissals = 3; this.installationChecked = false; this.init(); } init() { this.checkInstallationStatus(); this.setupEventListeners(); this.createInstallButton(); this.loadInstallPreferences(); if (this.isIOSSafari()) { this.startInstallationMonitoring(); } } checkInstallationStatus() { const isStandalone = window.matchMedia('(display-mode: standalone)').matches; const isIOSStandalone = window.navigator.standalone === true; const hasInstallPreference = this.loadInstallPreferences().installed; if (isStandalone || isIOSStandalone || hasInstallPreference) { this.isInstalled = true; console.log('📱 App is already installed as PWA'); document.body.classList.add('pwa-installed'); document.body.classList.remove('pwa-browser'); this.hideInstallPrompts(); if (this.isIOSSafari()) { document.body.classList.add('ios-pwa'); } this.installationChecked = true; return true; } this.isInstalled = false; document.body.classList.add('pwa-browser'); document.body.classList.remove('pwa-installed'); if (this.isIOSSafari()) { document.body.classList.add('ios-safari'); } this.installationChecked = true; return false; } startInstallationMonitoring() { let wasStandalone = window.navigator.standalone; const checkStandalone = () => { const isStandalone = window.navigator.standalone; if (isStandalone && !wasStandalone && !this.isInstalled) { this.isInstalled = true; this.hideInstallPrompts(); this.showInstallSuccess(); document.body.classList.remove('pwa-browser', 'ios-safari'); document.body.classList.add('pwa-installed', 'ios-pwa'); this.saveInstallPreference('installed', true); } wasStandalone = isStandalone; }; setInterval(checkStandalone, 2000); window.addEventListener('visibilitychange', () => { if (!document.hidden) { setTimeout(checkStandalone, 1000); } }); window.addEventListener('focus', () => { setTimeout(checkStandalone, 500); }); } setupEventListeners() { window.addEventListener('beforeinstallprompt', (event) => { // Don't prevent default - let browser show its own banner this.deferredPrompt = event; if (this.checkInstallationStatus()) { return; } if (!this.isInstalled && this.shouldShowPrompt()) { setTimeout(() => this.showInstallOptions(), 1000); } }); window.addEventListener('appinstalled', () => { this.isInstalled = true; this.hideInstallPrompts(); this.showInstallSuccess(); this.saveInstallPreference('installed', true); document.body.classList.remove('pwa-browser', 'ios-safari'); document.body.classList.add('pwa-installed'); }); window.addEventListener('visibilitychange', () => { if (document.hidden) return; setTimeout(() => { const wasInstalled = this.isInstalled; this.checkInstallationStatus(); if (!wasInstalled && this.isInstalled) { this.hideInstallPrompts(); this.showInstallSuccess(); } }, 1000); }); window.addEventListener('focus', () => { setTimeout(() => { const wasInstalled = this.isInstalled; this.checkInstallationStatus(); if (!wasInstalled && this.isInstalled) { this.hideInstallPrompts(); this.showInstallSuccess(); } }, 500); }); } createInstallButton() { if (this.isInstalled) { return; } this.installButton = document.createElement('div'); this.installButton.id = 'pwa-install-button'; this.installButton.className = 'hidden fixed bottom-6 right-6 bg-gradient-to-r from-orange-500 to-orange-600 hover:from-orange-600 hover:to-orange-700 text-white px-6 py-3 rounded-full shadow-lg transition-all duration-300 z-50 flex items-center space-x-3 group'; const buttonText = this.isIOSSafari() ? 'Install App' : 'Install App'; const buttonIcon = this.isIOSSafari() ? 'fas fa-share' : 'fas fa-download'; this.installButton.innerHTML = ` ${buttonText} `; this.installButton.addEventListener('click', (e) => { if (!e.target.classList.contains('close-btn')) { this.handleInstallClick(); } }); const closeBtn = this.installButton.querySelector('.close-btn'); closeBtn.addEventListener('click', (e) => { e.stopPropagation(); this.dismissInstallPrompt(); }); document.body.appendChild(this.installButton); } createInstallBanner() { if (this.isInstalled || this.installBanner) { return; } this.installBanner = document.createElement('div'); this.installBanner.id = 'pwa-install-banner'; this.installBanner.className = 'pwa-install-banner fixed bottom-0 left-0 right-0 transform translate-y-full transition-transform duration-300 z-40'; this.installBanner.innerHTML = `
After installation, open SecureBit from your home screen for the best experience.
To install this app, look for the install option in your browser menu or address bar. Different browsers have different install methods.