clean up
This commit is contained in:
370
public/css/styles.css
Normal file
370
public/css/styles.css
Normal file
@@ -0,0 +1,370 @@
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
font-family: Arial, sans-serif;
|
||||
}
|
||||
|
||||
body {
|
||||
background-color: #f5f5f5;
|
||||
color: #333;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
.app-container {
|
||||
max-width: 100%;
|
||||
margin: 0 auto;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.header {
|
||||
background-color: #2c3e50;
|
||||
color: white;
|
||||
padding: 1rem;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
width: 100%;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.game-controls {
|
||||
text-align: center;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.game-button {
|
||||
background-color: #3498db;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 4px;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.header-buttons {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.header-button {
|
||||
background-color: transparent;
|
||||
color: white;
|
||||
border: none;
|
||||
font-size: 1.2rem;
|
||||
cursor: pointer;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
.header-button:hover {
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.carousel-container {
|
||||
margin-top: 70px;
|
||||
margin-bottom: 60px; /* Add some space for the footer */
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
flex: 1;
|
||||
touch-action: pan-x;
|
||||
}
|
||||
|
||||
.carousel {
|
||||
display: flex;
|
||||
transition: transform 0.3s ease;
|
||||
height: calc(100vh - 70px);
|
||||
}
|
||||
|
||||
/* Adjust the preview image in the modal to maintain consistency */
|
||||
#imagePreview.player-image {
|
||||
width: 180px; /* Slightly smaller than the main display but still larger than original 120px */
|
||||
height: 180px;
|
||||
margin: 0.5rem auto;
|
||||
}
|
||||
|
||||
.player-card {
|
||||
min-width: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 2rem; /* Increased from 1rem for more spacing */
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.active-player {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.inactive-player {
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
/* New styles for timer active state */
|
||||
.player-timer {
|
||||
font-size: 4rem;
|
||||
font-weight: bold;
|
||||
margin: 1rem 0;
|
||||
padding: 0.5rem 1.5rem;
|
||||
border-radius: 12px;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
/* Timer background effect when game is running */
|
||||
.timer-active {
|
||||
background-color: #ffecee; /* Light red base color */
|
||||
box-shadow: 0 0 15px rgba(231, 76, 60, 0.5);
|
||||
animation: pulsate 1.5s ease-out infinite;
|
||||
}
|
||||
|
||||
/* Timer of a player that has run out of time */
|
||||
.timer-finished {
|
||||
color: #e74c3c;
|
||||
text-decoration: line-through;
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
/* Pulsating animation */
|
||||
@keyframes pulsate {
|
||||
0% {
|
||||
box-shadow: 0 0 0 0 rgba(231, 76, 60, 0.7);
|
||||
background-color: #ffecee;
|
||||
}
|
||||
50% {
|
||||
box-shadow: 0 0 20px 0 rgba(231, 76, 60, 0.5);
|
||||
background-color: #ffe0e0;
|
||||
}
|
||||
100% {
|
||||
box-shadow: 0 0 0 0 rgba(231, 76, 60, 0.7);
|
||||
background-color: #ffecee;
|
||||
}
|
||||
}
|
||||
|
||||
.player-image {
|
||||
width: 240px; /* Doubled from 120px */
|
||||
height: 240px; /* Doubled from 120px */
|
||||
border-radius: 50%;
|
||||
background-color: #ddd;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-bottom: 2rem; /* Increased from 1rem */
|
||||
overflow: hidden;
|
||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); /* Added shadow for better visual presence */
|
||||
}
|
||||
|
||||
.player-image img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.player-image i {
|
||||
font-size: 6rem; /* Doubled from 3rem */
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.player-name {
|
||||
font-size: 3rem; /* Doubled from 1.5rem */
|
||||
margin-bottom: 1rem; /* Increased from 0.5rem */
|
||||
font-weight: bold;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.modal {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-color: rgba(0, 0, 0, 0.7);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 20;
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
transition: opacity 0.3s ease;
|
||||
}
|
||||
|
||||
.modal.active {
|
||||
opacity: 1;
|
||||
pointer-events: auto;
|
||||
}
|
||||
|
||||
.modal-content {
|
||||
background-color: white;
|
||||
padding: 2rem;
|
||||
border-radius: 8px;
|
||||
width: 90%;
|
||||
max-width: 500px;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.form-group label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.form-group input {
|
||||
width: 100%;
|
||||
padding: 0.5rem;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.form-buttons {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.form-buttons button {
|
||||
padding: 0.5rem 1rem;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
flex: 1;
|
||||
margin: 0 0.5rem;
|
||||
}
|
||||
|
||||
.form-buttons button:first-child {
|
||||
margin-left: 0;
|
||||
}
|
||||
|
||||
.form-buttons button:last-child {
|
||||
margin-right: 0;
|
||||
}
|
||||
|
||||
.delete-button-container {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.save-button {
|
||||
background-color: #27ae60;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.cancel-button {
|
||||
background-color: #e74c3c;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.delete-button {
|
||||
background-color: #e74c3c;
|
||||
color: white;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* Add these styles to your styles.css file */
|
||||
|
||||
.image-input-container {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
align-items: center;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.camera-button {
|
||||
background-color: #3498db;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 0.5rem;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 5px;
|
||||
}
|
||||
|
||||
.camera-button:hover {
|
||||
background-color: #2980b9;
|
||||
}
|
||||
|
||||
/* Optional: Hide the default file input appearance and use a custom button */
|
||||
input[type="file"] {
|
||||
max-width: 120px;
|
||||
}
|
||||
|
||||
.camera-container {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-color: #000;
|
||||
z-index: 30;
|
||||
display: none;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.camera-container.active {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.camera-view {
|
||||
flex: 1;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.camera-view video {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.camera-controls {
|
||||
display: flex;
|
||||
justify-content: space-around;
|
||||
padding: 1rem;
|
||||
background-color: #222;
|
||||
}
|
||||
|
||||
.camera-button-large {
|
||||
width: 60px;
|
||||
height: 60px;
|
||||
border-radius: 50%;
|
||||
background-color: #fff;
|
||||
border: 3px solid #3498db;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.camera-button-cancel {
|
||||
background-color: #e74c3c;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.app-footer {
|
||||
background-color: #2c3e50;
|
||||
color: white;
|
||||
padding: 1rem;
|
||||
text-align: center;
|
||||
font-size: 0.9rem;
|
||||
margin-top: auto; /* This pushes the footer to the bottom when possible */
|
||||
}
|
||||
|
||||
.author-info {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 0.3rem;
|
||||
}
|
||||
BIN
public/favicon.ico
Normal file
BIN
public/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
BIN
public/icons/android-chrome-192x192.png
Normal file
BIN
public/icons/android-chrome-192x192.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.9 KiB |
BIN
public/icons/android-chrome-512x512.png
Normal file
BIN
public/icons/android-chrome-512x512.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
BIN
public/icons/apple-touch-icon.png
Normal file
BIN
public/icons/apple-touch-icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.2 KiB |
BIN
public/icons/favicon-16x16.png
Normal file
BIN
public/icons/favicon-16x16.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 398 B |
BIN
public/icons/favicon-32x32.png
Normal file
BIN
public/icons/favicon-32x32.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 770 B |
130
public/index.html
Normal file
130
public/index.html
Normal file
@@ -0,0 +1,130 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta name="theme-color" content="#2c3e50">
|
||||
<title>Game Timer</title>
|
||||
<link rel="manifest" href="manifest.json">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
|
||||
<link rel="stylesheet" href="css/styles.css">
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div class="app-container">
|
||||
<header class="header">
|
||||
<div class="game-controls">
|
||||
<button id="gameButton" class="game-button">Start Game</button>
|
||||
</div>
|
||||
<div class="header-buttons">
|
||||
<button id="resetButton" class="header-button" title="Reset All Data">
|
||||
<i class="fas fa-redo-alt"></i>
|
||||
</button>
|
||||
<button id="addPlayerButton" class="header-button" title="Add New Player">
|
||||
<i class="fas fa-user-plus"></i>
|
||||
</button>
|
||||
<button id="setupButton" class="header-button" title="Setup Current Player">
|
||||
<i class="fas fa-cog"></i>
|
||||
</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="carousel-container">
|
||||
<div id="carousel" class="carousel">
|
||||
<!-- Player cards will be dynamically inserted here -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Player Edit Modal -->
|
||||
<div id="playerModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<h2 id="modalTitle">Edit Player</h2>
|
||||
<form id="playerForm">
|
||||
<div class="form-group">
|
||||
<label for="playerName">Name</label>
|
||||
<input type="text" id="playerName" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="playerImage">Image</label>
|
||||
<div class="image-input-container">
|
||||
<input type="file" id="playerImage" accept="image/*">
|
||||
<button type="button" id="cameraButton" class="camera-button">
|
||||
<i class="fas fa-camera"></i> Take Photo
|
||||
</button>
|
||||
</div>
|
||||
<div id="imagePreview" class="player-image" style="margin-top: 0.5rem;">
|
||||
<i class="fas fa-user"></i>
|
||||
</div>
|
||||
</div>
|
||||
<div id="playerTimeContainer" class="form-group">
|
||||
<label for="playerTime">Time (minutes)</label>
|
||||
<input type="number" id="playerTime" min="1" max="180" value="5" required>
|
||||
</div>
|
||||
<div id="remainingTimeContainer" class="form-group" style="display: none;">
|
||||
<label for="playerRemainingTime">Remaining Time (MM:SS)</label>
|
||||
<input type="text" id="playerRemainingTime" pattern="[0-9]{2}:[0-9]{2}" placeholder="05:00">
|
||||
<small>Format: Minutes:Seconds (e.g., 05:30)</small>
|
||||
</div>
|
||||
<div class="form-buttons">
|
||||
<button type="button" id="cancelButton" class="cancel-button">Cancel</button>
|
||||
<button type="submit" class="save-button">Save</button>
|
||||
</div>
|
||||
<div class="form-buttons delete-button-container">
|
||||
<button type="button" id="deletePlayerButton" class="delete-button">Delete Player</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Reset Confirmation Modal -->
|
||||
<div id="resetModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<h2>Reset All Data</h2>
|
||||
<p>Are you sure you want to reset all players and timers? This action cannot be undone.</p>
|
||||
<div class="form-buttons">
|
||||
<button type="button" id="resetCancelButton" class="cancel-button">Cancel</button>
|
||||
<button type="button" id="resetConfirmButton" class="save-button">Reset</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Camera Capture UI -->
|
||||
<div id="cameraContainer" class="camera-container">
|
||||
<div class="camera-view">
|
||||
<video id="cameraView" autoplay playsinline></video>
|
||||
<canvas id="cameraCanvas" style="display: none;"></canvas>
|
||||
</div>
|
||||
<div class="camera-controls">
|
||||
<button id="cameraCancelButton" class="camera-button-cancel">Cancel</button>
|
||||
<button id="cameraCaptureButton" class="camera-button-large"></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Main application script -->
|
||||
<script type="module" src="../src/js/app.js"></script>
|
||||
<script>
|
||||
if ("serviceWorker" in navigator) {
|
||||
navigator.serviceWorker.register("sw.js")
|
||||
.then(() => console.log("Service Worker Registered"))
|
||||
.catch((err) => console.log("Service Worker Failed", err));
|
||||
}
|
||||
</script>
|
||||
<script>
|
||||
// Request notification permission on page load
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
if ('Notification' in window) {
|
||||
Notification.requestPermission().then(permission => {
|
||||
console.log('Notification permission:', permission);
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<footer class="app-footer">
|
||||
<div class="author-info">
|
||||
<p>Vibe coded by Martin</p>
|
||||
<p>Version 0.0.1</p>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
55
public/manifest.json
Normal file
55
public/manifest.json
Normal file
@@ -0,0 +1,55 @@
|
||||
{
|
||||
"name": "Game Timer PWA",
|
||||
"short_name": "Game Timer",
|
||||
"description": "Multi-player chess-like timer with carousel navigation",
|
||||
"start_url": "./index.html",
|
||||
"display": "standalone",
|
||||
"background_color": "#f5f5f5",
|
||||
"theme_color": "#2c3e50",
|
||||
"icons": [
|
||||
{
|
||||
"src": "./icons/android-chrome-192x192.png",
|
||||
"sizes": "192x192",
|
||||
"type": "image/png"
|
||||
},
|
||||
{
|
||||
"src": "./icons/android-chrome-512x512.png",
|
||||
"sizes": "512x512",
|
||||
"type": "image/png"
|
||||
},
|
||||
{
|
||||
"src": "./icons/apple-touch-icon.png",
|
||||
"sizes": "180x180",
|
||||
"type": "image/png"
|
||||
},
|
||||
{
|
||||
"src": "./icons/favicon-32x32.png",
|
||||
"sizes": "32x32",
|
||||
"type": "image/png"
|
||||
},
|
||||
{
|
||||
"src": "./icons/favicon-16x16.png",
|
||||
"sizes": "16x16",
|
||||
"type": "image/png"
|
||||
}
|
||||
],
|
||||
"screenshots": [
|
||||
{
|
||||
"src": "../docs/screenshots/screenshot1.png",
|
||||
"sizes": "2604x2269",
|
||||
"type": "image/png",
|
||||
"form_factor": "wide"
|
||||
},
|
||||
{
|
||||
"src": "../docs/screenshots/screenshot2.png",
|
||||
"sizes": "1082x2402",
|
||||
"type": "image/png"
|
||||
}
|
||||
],
|
||||
"url_handlers": [
|
||||
{
|
||||
"origin": "https://game-timer.virtonline.eu"
|
||||
}
|
||||
],
|
||||
"gcm_sender_id": "103953800507"
|
||||
}
|
||||
19
public/site.webmanifest
Normal file
19
public/site.webmanifest
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "Game Timer",
|
||||
"short_name": "Game Timer",
|
||||
"icons": [
|
||||
{
|
||||
"src": "./icons/android-chrome-192x192.png",
|
||||
"sizes": "192x192",
|
||||
"type": "image/png"
|
||||
},
|
||||
{
|
||||
"src": "./icons/android-chrome-512x512.png",
|
||||
"sizes": "512x512",
|
||||
"type": "image/png"
|
||||
}
|
||||
],
|
||||
"theme_color": "#ffffff",
|
||||
"background_color": "#ffffff",
|
||||
"display": "standalone"
|
||||
}
|
||||
192
public/sw.js
Normal file
192
public/sw.js
Normal file
@@ -0,0 +1,192 @@
|
||||
// Service Worker version
|
||||
const CACHE_VERSION = 'v1.0.0';
|
||||
const CACHE_NAME = `game-timer-${CACHE_VERSION}`;
|
||||
|
||||
// Files to cache
|
||||
const CACHE_FILES = [
|
||||
'./',
|
||||
'./index.html',
|
||||
'../src/js/app.js',
|
||||
'../src/js/ui/audio.js',
|
||||
'./css/styles.css',
|
||||
'./manifest.json',
|
||||
'./icons/android-chrome-192x192.png',
|
||||
'./icons/android-chrome-512x512.png',
|
||||
'./icons/apple-touch-icon.png',
|
||||
'./icons/favicon-32x32.png',
|
||||
'./icons/favicon-16x16.png'
|
||||
];
|
||||
|
||||
// Install event - Cache files
|
||||
self.addEventListener('install', event => {
|
||||
console.log('[ServiceWorker] Install');
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME)
|
||||
.then(cache => {
|
||||
console.log('[ServiceWorker] Caching app shell');
|
||||
return cache.addAll(CACHE_FILES);
|
||||
})
|
||||
.then(() => {
|
||||
console.log('[ServiceWorker] Skip waiting on install');
|
||||
return self.skipWaiting();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// Activate event - Clean old caches
|
||||
self.addEventListener('activate', event => {
|
||||
console.log('[ServiceWorker] Activate');
|
||||
event.waitUntil(
|
||||
caches.keys().then(keyList => {
|
||||
return Promise.all(keyList.map(key => {
|
||||
if (key !== CACHE_NAME) {
|
||||
console.log('[ServiceWorker] Removing old cache', key);
|
||||
return caches.delete(key);
|
||||
}
|
||||
}));
|
||||
})
|
||||
.then(() => {
|
||||
console.log('[ServiceWorker] Claiming clients');
|
||||
return self.clients.claim();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// Helper function to determine if a response should be cached
|
||||
function shouldCacheResponse(request, response) {
|
||||
// Only cache GET requests
|
||||
if (request.method !== 'GET') return false;
|
||||
|
||||
// Don't cache errors
|
||||
if (!response || response.status !== 200) return false;
|
||||
|
||||
// Check if URL should be cached
|
||||
const url = new URL(request.url);
|
||||
|
||||
// Don't cache query parameters (except common ones for content)
|
||||
if (url.search && !url.search.match(/\?(v|version|cache)=/)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
self.addEventListener('push', event => {
|
||||
console.log('[ServiceWorker] Push received');
|
||||
|
||||
let pushData = {
|
||||
title: 'Flic Action',
|
||||
body: 'Button pressed!',
|
||||
data: {
|
||||
action: 'Unknown',
|
||||
button: 'Unknown',
|
||||
batteryLevel: undefined
|
||||
}
|
||||
};
|
||||
|
||||
// --- Attempt to parse data payload ---
|
||||
if (event.data) {
|
||||
try {
|
||||
const parsedData = event.data.json();
|
||||
console.log('[ServiceWorker] Push data:', parsedData);
|
||||
|
||||
// Use parsed data for notification and message
|
||||
pushData = {
|
||||
title: parsedData.title || pushData.title,
|
||||
body: parsedData.body || pushData.body,
|
||||
data: parsedData.data || pushData.data // Expecting { action: 'SingleClick', button: 'game-button', batteryLevel: 75 }
|
||||
};
|
||||
|
||||
} catch (e) {
|
||||
console.error('[ServiceWorker] Error parsing push data:', e);
|
||||
// Use default notification if parsing fails
|
||||
pushData.body = event.data.text() || pushData.body; // Fallback to text
|
||||
}
|
||||
} else {
|
||||
console.log('[ServiceWorker] Push event but no data');
|
||||
}
|
||||
|
||||
// --- Send message to client(s) ---
|
||||
const messagePayload = {
|
||||
type: 'flic-action', // Custom message type
|
||||
action: pushData.data.action, // e.g., 'SingleClick', 'DoubleClick', 'Hold'
|
||||
button: pushData.data.button, // e.g., the button name
|
||||
timestamp: pushData.data.timestamp, // e.g., the timestamp of the action
|
||||
batteryLevel: pushData.data.batteryLevel // e.g., the battery level percentage
|
||||
};
|
||||
|
||||
// Send message to all open PWA windows controlled by this SW
|
||||
event.waitUntil(
|
||||
self.clients.matchAll({
|
||||
type: 'window', // Only target window clients
|
||||
includeUncontrolled: true // Include clients that might not be fully controlled yet
|
||||
}).then(clientList => {
|
||||
if (!clientList || clientList.length === 0) {
|
||||
console.log('[ServiceWorker] No client windows found to send message to.');
|
||||
// If no window is open, we MUST show a notification
|
||||
return self.registration.showNotification(pushData.title, {
|
||||
body: pushData.body,
|
||||
icon: './icons/android-chrome-192x192.png', // Optional: path to an icon
|
||||
data: pushData.data // Pass data if needed when notification is clicked
|
||||
});
|
||||
}
|
||||
|
||||
// Post message to each client
|
||||
let messageSent = false;
|
||||
clientList.forEach(client => {
|
||||
console.log(`[ServiceWorker] Posting message to client: ${client.id}`, messagePayload);
|
||||
client.postMessage(messagePayload);
|
||||
messageSent = true; // Mark that we at least tried to send a message
|
||||
});
|
||||
|
||||
// Decide whether to still show a notification even if a window is open.
|
||||
// Generally good practice unless you are SURE the app will handle it visibly.
|
||||
// You might choose *not* to show a notification if a client was found and focused.
|
||||
// For simplicity here, we'll still show one. Adjust as needed.
|
||||
if (!messageSent) { // Only show notification if no message was sent? Or always show?
|
||||
return self.registration.showNotification(pushData.title, {
|
||||
body: pushData.body,
|
||||
icon: './icons/android-chrome-192x192.png',
|
||||
data: pushData.data
|
||||
});
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// --- Show a notification (Important!) ---
|
||||
// Push notifications generally REQUIRE showing a notification to the user
|
||||
// unless the PWA is already in the foreground AND handles the event visually.
|
||||
// It's safer to always show one unless you have complex foreground detection.
|
||||
/* This part is now handled inside the clients.matchAll promise */
|
||||
/*
|
||||
const notificationOptions = {
|
||||
body: pushData.body,
|
||||
icon: './icons/android-chrome-192x192.png', // Optional: path to an icon
|
||||
data: pushData.data // Attach data if needed when notification is clicked
|
||||
};
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(pushData.title, notificationOptions)
|
||||
);
|
||||
*/
|
||||
});
|
||||
|
||||
// This helps with navigation after app is installed
|
||||
self.addEventListener('notificationclick', event => {
|
||||
console.log('[ServiceWorker] Notification click received');
|
||||
|
||||
event.notification.close();
|
||||
|
||||
// Handle the notification click
|
||||
event.waitUntil(
|
||||
self.clients.matchAll({ type: 'window' })
|
||||
.then(clientList => {
|
||||
for (const client of clientList) {
|
||||
if (client.url.startsWith(self.location.origin) && 'focus' in client) {
|
||||
return client.focus();
|
||||
}
|
||||
}
|
||||
|
||||
if (self.clients.openWindow) {
|
||||
return self.clients.openWindow('./');
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user