diff --git a/README.md b/README.md index 7d5ccb3..5d27eef 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ DevRant client built with vanilla JavaScript, Web Components, and CSS. python3 proxy.py ``` -Open `http://localhost:8000` +Open `http://localhost:8101` ## Features diff --git a/js/app.js b/js/app.js index 00d9dd6..8c758d3 100644 --- a/js/app.js +++ b/js/app.js @@ -58,6 +58,33 @@ class Application { this.main = null; this.currentPage = null; this.isNavOpen = false; + this.deferredInstallPrompt = null; + this.setupInstallPrompt(); + } + + setupInstallPrompt() { + window.addEventListener('beforeinstallprompt', (e) => { + e.preventDefault(); + this.deferredInstallPrompt = e; + window.dispatchEvent(new CustomEvent('rantii:install-available')); + }); + + window.addEventListener('appinstalled', () => { + this.deferredInstallPrompt = null; + window.dispatchEvent(new CustomEvent('rantii:app-installed')); + }); + } + + canInstall() { + return this.deferredInstallPrompt !== null; + } + + async installApp() { + if (!this.deferredInstallPrompt) return false; + this.deferredInstallPrompt.prompt(); + const { outcome } = await this.deferredInstallPrompt.userChoice; + this.deferredInstallPrompt = null; + return outcome === 'accepted'; } async init() { diff --git a/js/pages/settings-page.js b/js/pages/settings-page.js index 305ebf7..80b4670 100644 --- a/js/pages/settings-page.js +++ b/js/pages/settings-page.js @@ -9,6 +9,7 @@ import { BaseComponent } from '../components/base-component.js'; class SettingsPage extends BaseComponent { init() { + this.installHandler = () => this.render(); this.render(); this.bindEvents(); } @@ -16,6 +17,7 @@ class SettingsPage extends BaseComponent { render() { const isLoggedIn = this.isLoggedIn(); const user = this.getCurrentUser(); + const canInstall = this.getApp()?.canInstall(); this.addClass('page', 'settings-page'); @@ -29,6 +31,18 @@ class SettingsPage extends BaseComponent {
Install Rantii for quick access
+ +