Smoother daily use: remember profile, work offline, fix install #1

Merged
krburchel merged 1 commits from smoother-shopping into main 2026-06-13 14:24:03 +00:00
6 changed files with 115 additions and 6 deletions

No files matched your search

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

+23 -2
View File
@@ -6,6 +6,11 @@
<title>Kevin & Chey's Grocery List</title>
<link rel="manifest" href="manifest.json"/>
<meta name="theme-color" content="#0f4c35"/>
<meta name="mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="default"/>
<meta name="apple-mobile-web-app-title" content="Grocery List"/>
<link rel="apple-touch-icon" href="apple-touch-icon.png"/>
<link rel="preconnect" href="https://fonts.googleapis.com"/>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin/>
<link href="https://fonts.googleapis.com/css2?family=DM+Serif+Display&family=DM+Sans:wght@300;400;500;600&display=swap" rel="stylesheet"/>
@@ -468,7 +473,7 @@ body.shopping-mode .cat-empty-placeholder{display:none}
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js";
import { getFirestore, collection, addDoc, onSnapshot, updateDoc, deleteDoc, doc, setDoc } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-firestore.js";
import { initializeFirestore, persistentLocalCache, persistentMultipleTabManager, collection, addDoc, onSnapshot, updateDoc, deleteDoc, doc, setDoc } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-firestore.js";
const firebaseConfig = {
apiKey: "AIzaSyDMwLUUxeJXQYpXK3d25hGUeZ5zsu-4bBc",
@@ -480,7 +485,12 @@ const firebaseConfig = {
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
// Offline-first: serve reads from an IndexedDB cache and queue writes while the
// signal is bad (the whole point of using this in a grocery store). Multi-tab
// manager keeps the cache consistent if the app is open on more than one tab.
const db = initializeFirestore(app, {
localCache: persistentLocalCache({ tabManager: persistentMultipleTabManager() })
});
// ── CONSTANTS ────────────────────────────────────────────────────────────────
const CATS = ['Produce','Dairy','Meat','Bakery','Pantry','Frozen','Beverages','Other'];
@@ -526,6 +536,8 @@ function timeAgo(ts){
// ── STATE ────────────────────────────────────────────────────────────────────
let currentProfile = 'Kevin';
// Remember who's using this device so Chey isn't re-credited to Kevin every load.
try { const sp = localStorage.getItem('profile'); if(sp==='Kevin'||sp==='Chey') currentProfile = sp; } catch(_){}
let selectedCat = 'Produce';
let selectedStore = 'Any';
let storeFilter = 'All';
@@ -1287,6 +1299,7 @@ document.addEventListener('click', async e => {
if(action==='set-profile'){
currentProfile=val;
try{ localStorage.setItem('profile', val); }catch(_){}
document.querySelectorAll('.profile-btn').forEach(b=>b.classList.toggle('active',b.dataset.val===val));
}
else if(action==='set-tab'){
@@ -1931,6 +1944,14 @@ document.getElementById('inv-item-input').addEventListener('keydown', async func
renderCatPills();
renderStorePills();
renderInvCatSelect();
// Reflect the restored profile (Task 1) on the toggle buttons.
document.querySelectorAll('.profile-btn').forEach(b=>b.classList.toggle('active', b.dataset.val===currentProfile));
// Register the service worker so the app shell opens even on dead store wifi.
if('serviceWorker' in navigator){
window.addEventListener('load', ()=>{
navigator.serviceWorker.register('sw.js').catch(err=>console.warn('SW registration failed:', err));
});
}
// Restore shopping-mode UI state on load
if(shoppingMode){
document.body.classList.add('shopping-mode');
+12 -4
View File
@@ -1,15 +1,23 @@
{
"name": "Kevin & Chey Grocery List",
"short_name": "Grocery List",
"start_url": "/",
"start_url": "./",
"scope": "./",
"display": "standalone",
"background_color": "#faf8f3",
"theme_color": "#0f4c35",
"icons": [
{
"src": "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/1f6d2.png",
"sizes": "72x72",
"type": "image/png"
"src": "icon-192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "any maskable"
},
{
"src": "icon-512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "any maskable"
}
]
}
+80
View File
@@ -0,0 +1,80 @@
// Service worker for the Kevin & Chey grocery list.
// Goal: the app shell opens instantly and even on dead grocery-store wifi.
// Firestore traffic is deliberately NOT cached here — the Firestore SDK has its
// own IndexedDB offline layer (persistentLocalCache) that queues writes and
// serves cached reads. We only cache the static shell + SDK modules + fonts.
const VERSION = 'v1';
const SHELL = 'grocery-shell-' + VERSION;
const RUNTIME = 'grocery-runtime-' + VERSION;
const SHELL_ASSETS = [
'./',
'./index.html',
'./manifest.json',
'./icon-192.png',
'./icon-512.png',
'./apple-touch-icon.png'
];
self.addEventListener('install', e => {
e.waitUntil(
caches.open(SHELL)
.then(c => c.addAll(SHELL_ASSETS))
.then(() => self.skipWaiting())
);
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys()
.then(keys => Promise.all(
keys.filter(k => k !== SHELL && k !== RUNTIME).map(k => caches.delete(k))
))
.then(() => self.clients.claim())
);
});
self.addEventListener('fetch', e => {
const req = e.request;
if (req.method !== 'GET') return;
const url = new URL(req.url);
const host = url.hostname;
// Let the Firebase SDK manage its own network (Firestore listen/write channels,
// auth, installations). Fonts are on *.googleapis.com too, so allow those.
if (host.endsWith('googleapis.com') && !host.startsWith('fonts.')) return;
if (host.endsWith('firebaseio.com')) return;
// Recipe search / CORS proxies are live API calls — don't serve them stale.
if (/themealdb\.com|allorigins\.win|corsproxy\.io/.test(host)) return;
// Navigations: network-first so updates ship, fall back to the cached shell offline.
if (req.mode === 'navigate') {
e.respondWith(
fetch(req)
.then(res => {
const copy = res.clone();
caches.open(SHELL).then(c => c.put('./index.html', copy));
return res;
})
.catch(() => caches.match('./index.html').then(r => r || caches.match('./')))
);
return;
}
// Everything else (app code, Firebase SDK modules, fonts, icons):
// cache-first, populating the runtime cache as resources are fetched.
e.respondWith(
caches.match(req).then(cached => {
if (cached) return cached;
return fetch(req).then(res => {
if (res && (res.ok || res.type === 'opaque')) {
const copy = res.clone();
caches.open(RUNTIME).then(c => c.put(req, copy));
}
return res;
}).catch(() => cached);
})
);
});