Walking Step Counter
Log Daily Steps
Your Step History
Daily Steps Progress
Steps Log
| Date | Steps |
|---|
AI Coach Analysis
Your AI coach is analyzing your activity...
No steps logged yet. Start logging to see your progress!
Not enough data to analyze. Keep logging your steps!
"; loadingEl.classList.add('hidden'); containerEl.classList.remove('hidden'); return; } const prompt = `You are a friendly and motivational fitness coach. A user is tracking their daily steps. Here is their recent history:\n${stepsData}\nPlease analyze their activity. Comment on their consistency, celebrate high-step days, and identify periods of lower activity. Provide 2-3 encouraging and practical tips to help them stay consistent or increase their step count. Keep the tone positive and supportive. Format your response using simple HTML (h3, p, ul, li).`; const apiKey = ""; // API key is handled by the environment const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=${apiKey}`; const payload = { contents: [{ parts: [{ text: prompt }] }] }; try { const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (!response.ok) throw new Error(`API request failed with status ${response.status}`); const result = await response.json(); const analysisText = result.candidates?.[0]?.content?.parts?.[0]?.text; if (analysisText) { containerEl.innerHTML = analysisText; } else { throw new Error("No content received from API."); } } catch (error) { console.error("Gemini API Error:", error); containerEl.innerHTML = 'Sorry, the AI coach is unavailable right now. Please try again later.
'; } finally { loadingEl.classList.add('hidden'); containerEl.classList.remove('hidden'); } } elements.analyzeStepsBtn.addEventListener('click', analyzeStepsWithGemini); // --- PDF DOWNLOAD FUNCTIONALITY --- async function downloadPDF() { const { jsPDF } = window.jspdf; const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' }); const contentArea = elements.pdfContentAreaSteps; if (!contentArea) { console.error("PDF content area not found!"); return; } document.body.classList.add('pdf-export-mode'); try { const canvas = await html2canvas(contentArea, { scale: 2, useCORS: true, logging: false }); const imgData = canvas.toDataURL('image/png'); const imgProps = pdf.getImageProperties(imgData); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width; let heightLeft = pdfHeight; let position = 0; const pageHeight = pdf.internal.pageSize.getHeight(); pdf.addImage(imgData, 'PNG', 0, position, pdfWidth, pdfHeight); heightLeft -= pageHeight; while (heightLeft > 0) { position = heightLeft - pdfHeight; pdf.addPage(); pdf.addImage(imgData, 'PNG', 0, position, pdfWidth, pdfHeight); heightLeft -= pageHeight; } pdf.save('Daily_Steps_Progress_Report.pdf'); } catch (error) { console.error("Error generating PDF:", error); alert("An error occurred while generating the PDF. Please try again."); } finally { document.body.classList.remove('pdf-export-mode'); } } elements.downloadStepsPdfBtn.addEventListener('click', downloadPDF); // --- INITIALIZATION --- function initializeTool() { elements.stepsDate.valueAsDate = new Date(); updateStepTrackerView(); } initializeTool(); });