Home Information - Your home, your data, organized.
Python
Add full screen mode support for tablets via PWA (#224)
Tony Cce65e0b7ac45
diff --git a/src/hi/settings/base.py b/src/hi/settings/base.pyindex aea2bff3..f1b43851 100644--- a/src/hi/settings/base.py+++ b/src/hi/settings/base.py@@ -79,6 +79,10 @@ CSP_FONT_SRC = ('data:',) + ENV.EXTRA_CSP_URLS+CSP_WORKER_SRC = (+ "'self'",+) + ENV.EXTRA_CSP_URLS+LANGUAGE_CODE = 'en-us'TIME_ZONE = 'UTC'USE_I18N = Truediff --git a/src/hi/static/css/main.css b/src/hi/static/css/main.cssindex cd9e0d5c..a0333151 100644--- a/src/hi/static/css/main.css+++ b/src/hi/static/css/main.css@@ -1253,3 +1253,86 @@ g[hover] {font-weight: 600;font-size: 1.1rem;}++/* ================================================+ PWA-specific styles for full screen mode+ ================================================ */++/* Fullscreen mode styles when app is installed as PWA */+@media all and (display-mode: fullscreen) {+ body {+ /* Remove any default margins/padding that might appear */+ margin: 0;+ padding: 0;+ /* Ensure content uses full viewport */+ min-height: 100vh;+ /* Prevent overscroll bounce on iOS */+ overscroll-behavior: none;+ }++ /* Hide browser UI elements that might still be visible */+ .browser-ui-hint {+ display: none;+ }+}++/* Standalone mode styles (iOS Safari, Chrome mobile) */+@media all and (display-mode: standalone) {+ body {+ margin: 0;+ padding: 0;+ min-height: 100vh;+ overscroll-behavior: none;+ /* Add safe area padding for devices with notches */+ padding-top: env(safe-area-inset-top);+ padding-bottom: env(safe-area-inset-bottom);+ padding-left: env(safe-area-inset-left);+ padding-right: env(safe-area-inset-right);+ }++ /* Adjust main content area for safe areas */+ .container-fluid {+ padding-left: max(15px, env(safe-area-inset-left));+ padding-right: max(15px, env(safe-area-inset-right));+ }+}++/* Landscape orientation optimizations (tablet primary use case) */+@media (orientation: landscape) and ((display-mode: fullscreen) or (display-mode: standalone)) {+ /* Optimize layout for landscape orientation */+ .row {+ /* Ensure full width usage in landscape */+ margin-left: 0;+ margin-right: 0;+ }++ /* Tablet-specific touch optimizations */+ button, .btn, .form-control, select {+ /* Ensure touch targets are at least 44px (Apple HIG) */+ min-height: 44px;+ /* Add more comfortable padding for touch */+ padding: 10px 16px;+ }++ /* Optimize modal dialogs for landscape */+ .modal-dialog {+ max-width: 80vw;+ margin: 2vh auto;+ }+}++/* High DPI display optimizations */+@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {+ /* Ensure crisp icons and graphics on retina displays */+ img, svg {+ image-rendering: -webkit-optimize-contrast;+ image-rendering: crisp-edges;+ }+}++/* PWA installation prompt hiding (when app is already installed) */+@media (display-mode: standalone), (display-mode: fullscreen) {+ .pwa-install-prompt {+ display: none !important;+ }+}diff --git a/src/hi/static/img/hi-icon-512x512.png b/src/hi/static/img/hi-icon-512x512.pngnew file mode 100644index 00000000..be867647Binary files /dev/null and b/src/hi/static/img/hi-icon-512x512.png differdiff --git a/src/hi/templates/pages/base.html b/src/hi/templates/pages/base.htmlindex 7ac77148..7c22c674 100644--- a/src/hi/templates/pages/base.html+++ b/src/hi/templates/pages/base.html@@ -7,12 +7,29 @@<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><title>{% block head_title %}Home Information{% endblock %}</title><meta name="viewport" content="width=device-width, initial-scale=1">++ <!-- PWA Configuration -->+ <link rel="manifest" href="/manifest.json">+ <meta name="theme-color" content="#177072">++ <!-- Apple PWA Configuration -->+ <meta name="apple-mobile-web-app-capable" content="yes">+ <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">+ <meta name="apple-mobile-web-app-title" content="Hi">++ <!-- Standard Icons --><link rel="shortcut icon" type="image/png" href="{% static 'favicon.png' %}"/>- <link rel="icon" type="image/png" href="{% static 'img/hi-icon-16x16.png' %}" sizes="16x16">- <link rel="icon" type="image/png" href="{% static 'img/hi-icon-32x32.png' %}" sizes="32x32">- <link rel="icon" type="image/png" href="{% static 'img/hi-icon-96x96.png' %}" sizes="96x96">- <link rel="icon" type="image/png" href="{% static 'img/hi-icon-128x128.png' %}" sizes="128x128">- <link rel="icon" type="image/png" href="{% static 'img/hi-icon-196x196.png' %}" sizes="196x196">+ <link rel="icon" type="image/png" href="{% static 'img/hi-icon-16x16.png' %}" sizes="16x16">+ <link rel="icon" type="image/png" href="{% static 'img/hi-icon-32x32.png' %}" sizes="32x32">+ <link rel="icon" type="image/png" href="{% static 'img/hi-icon-96x96.png' %}" sizes="96x96">+ <link rel="icon" type="image/png" href="{% static 'img/hi-icon-128x128.png' %}" sizes="128x128">+ <link rel="icon" type="image/png" href="{% static 'img/hi-icon-196x196.png' %}" sizes="196x196">+ <link rel="icon" type="image/png" href="{% static 'img/hi-icon-512x512.png' %}" sizes="512x512">++ <!-- Apple Touch Icons -->+ <link rel="apple-touch-icon" href="{% static 'img/hi-icon-128x128.png' %}" sizes="128x128">+ <link rel="apple-touch-icon" href="{% static 'img/hi-icon-196x196.png' %}" sizes="196x196">+ <link rel="apple-touch-icon" href="{% static 'img/hi-icon-512x512.png' %}" sizes="512x512">{% stylesheet 'css_head' %}{% block head_css %}{% endblock %}{% block head_javascript %}{% endblock %}@@ -40,6 +57,21 @@{% javascript 'js_after_content' %}{% block footer_post_javascript %}{% endblock %}+ <!-- Service Worker Registration -->+ <script>+ if ('serviceWorker' in navigator) {+ window.addEventListener('load', function() {+ navigator.serviceWorker.register('/service-worker.js')+ .then(function(registration) {+ console.log('ServiceWorker registration successful with scope: ', registration.scope);+ })+ .catch(function(error) {+ console.log('ServiceWorker registration failed: ', error);+ });+ });+ }+ </script>+{% if initial_modal_template_name|default:None %}<div id="antinode-initial-modal">{% include initial_modal_template_name %}diff --git a/src/hi/templates/service-worker.js b/src/hi/templates/service-worker.jsindex b9893174..70a813f0 100644--- a/src/hi/templates/service-worker.js+++ b/src/hi/templates/service-worker.js@@ -2,3 +2,138 @@var CACHE_VERSION = 1;var OFFLINE_CACHE_NAME = 'offline-cache-' + CACHE_VERSION;+var STATIC_CACHE_NAME = 'static-cache-' + CACHE_VERSION;++// Assets to cache for offline use+var STATIC_ASSETS = [+ '/',+ '{% static "css/main.css" %}',+ '{% static "css/icons.css" %}',+ '{% static "css/attribute.css" %}',+ '{% static "js/jquery-3.7.0.min.js" %}',+ '{% static "js/antinode.js" %}',+ '{% static "js/main.js" %}',+ '{% static "bootstrap/css/bootstrap.css" %}',+ '{% static "bootstrap/js/bootstrap.js" %}',+ '{% static "img/hi-icon-128x128.png" %}',+ '{% static "img/hi-icon-196x196.png" %}',+ '{% static "img/hi-icon-512x512.png" %}',+ '{% static "favicon.png" %}'+];++// Install event - cache static assets+self.addEventListener('install', function(event) {+ console.log('Service Worker: Installing...');+ event.waitUntil(+ caches.open(STATIC_CACHE_NAME)+ .then(function(cache) {+ console.log('Service Worker: Caching static assets');+ return cache.addAll(STATIC_ASSETS);+ })+ .then(function() {+ console.log('Service Worker: Installation complete');+ return self.skipWaiting();+ })+ );+});++// Activate event - clean up old caches+self.addEventListener('activate', function(event) {+ console.log('Service Worker: Activating...');+ event.waitUntil(+ caches.keys().then(function(cacheNames) {+ return Promise.all(+ cacheNames.map(function(cacheName) {+ if (cacheName !== STATIC_CACHE_NAME && cacheName !== OFFLINE_CACHE_NAME) {+ console.log('Service Worker: Deleting old cache:', cacheName);+ return caches.delete(cacheName);+ }+ })+ );+ }).then(function() {+ console.log('Service Worker: Activation complete');+ return self.clients.claim();+ })+ );+});++// Fetch event - serve from cache when possible+self.addEventListener('fetch', function(event) {+ // Only handle GET requests+ if (event.request.method !== 'GET') {+ return;+ }++ // Skip non-HTTP(S) requests+ if (!event.request.url.startsWith('http')) {+ return;+ }++ event.respondWith(+ caches.match(event.request)+ .then(function(response) {+ // Return cached version if available+ if (response) {+ return response;+ }++ // For HTML pages, try network first, fall back to cache+ if (event.request.headers.get('accept').includes('text/html')) {+ return fetch(event.request)+ .then(function(response) {+ // Clone the response for caching+ var responseToCache = response.clone();++ // Cache successful responses+ if (response.status === 200) {+ caches.open(OFFLINE_CACHE_NAME)+ .then(function(cache) {+ cache.put(event.request, responseToCache);+ });+ }++ return response;+ })+ .catch(function() {+ // Network failed, try to serve from cache+ return caches.match('/')+ .then(function(response) {+ return response || new Response('Offline - Please check your connection', {+ status: 503,+ statusText: 'Service Unavailable',+ headers: new Headers({+ 'Content-Type': 'text/html'+ })+ });+ });+ });+ }++ // For other resources, try network first+ return fetch(event.request)+ .then(function(response) {+ // Clone the response for caching+ var responseToCache = response.clone();++ // Cache successful responses for static assets+ if (response.status === 200 &&+ (event.request.url.includes('/static/') ||+ event.request.url.includes('.css') ||+ event.request.url.includes('.js') ||+ event.request.url.includes('.png') ||+ event.request.url.includes('.ico'))) {+ caches.open(STATIC_CACHE_NAME)+ .then(function(cache) {+ cache.put(event.request, responseToCache);+ });+ }++ return response;+ })+ .catch(function() {+ // Network failed for static asset, serve from cache if available+ return caches.match(event.request);+ });+ })+ );+});diff --git a/src/hi/urls.py b/src/hi/urls.pyindex c5808c26..0f8a8e30 100644--- a/src/hi/urls.py+++ b/src/hi/urls.py@@ -20,6 +20,8 @@ urlpatterns = [re_path(r'^(?P<filename>(service-worker.js))$',views.home_javascript_files, name='home-javascript-files'),+ re_path( r'^manifest.json$', views.ManifestView.as_view(), name='manifest' ),+path('admin/', admin.site.urls),diff --git a/src/hi/views.py b/src/hi/views.pyindex f3acc586..ed8e5501 100644--- a/src/hi/views.py+++ b/src/hi/views.py@@ -183,7 +183,7 @@ class HomeView( View ):class StartView( View ):def get(self, request, *args, **kwargs):-+# This view only for first time users (when no LocationViews exist)if LocationView.objects.all().exists():redirect_url = reverse( 'home' )@@ -197,9 +197,64 @@ class StartView( View ):ProfileType.TWO_STORY,ProfileType.APARTMENT,]-- context = {++ context = {'profile_type_list': profile_type_list,}return render( request, 'pages/start.html', context )+++class ManifestView( View ):++ def get(self, request, *args, **kwargs):+ """+ Serves the PWA manifest.json for full screen mode support.+ Configured for landscape orientation (tablet primary use case).+ """+ manifest_data = {+ "name": "Home Information",+ "short_name": "Hi",+ "description": "Smart home monitoring and control system",+ "start_url": "/",+ "scope": "/",+ "display": "fullscreen",+ "orientation": "landscape",+ "theme_color": "#177072",+ "background_color": "#fdfdfd",+ "icons": [+ {+ "src": "/static/img/hi-icon-16x16.png",+ "sizes": "16x16",+ "type": "image/png"+ },+ {+ "src": "/static/img/hi-icon-32x32.png",+ "sizes": "32x32",+ "type": "image/png"+ },+ {+ "src": "/static/img/hi-icon-96x96.png",+ "sizes": "96x96",+ "type": "image/png"+ },+ {+ "src": "/static/img/hi-icon-128x128.png",+ "sizes": "128x128",+ "type": "image/png"+ },+ {+ "src": "/static/img/hi-icon-196x196.png",+ "sizes": "196x196",+ "type": "image/png"+ },+ {+ "src": "/static/img/hi-icon-512x512.png",+ "sizes": "512x512",+ "type": "image/png",+ "purpose": "any maskable"+ }+ ]+ }++ return JsonResponse( manifest_data, json_dumps_params={'indent': 2} )