@@ -117,17 +117,36 @@ function formatTime(seconds) {
117117 return `${ String ( hours ) . padStart ( 2 , '0' ) } :${ String ( minutes ) . padStart ( 2 , '0' ) } :${ String ( secs ) . padStart ( 2 , '0' ) } ` ;
118118}
119119
120+ // Function to record usage data
121+ function recordUsage ( deviceName , currentMa , duration , volume , ppm ) {
122+ const now = new Date ( ) ;
123+ const date = now . toLocaleDateString ( ) ;
124+ const time = now . toLocaleTimeString ( ) ;
125+ const usage = { date, time, device : deviceName , currentMa, duration, volume, ppm } ;
126+
127+ let stats = JSON . parse ( localStorage . getItem ( 'usageStats' ) ) || [ ] ;
128+ stats . push ( usage ) ;
129+ localStorage . setItem ( 'usageStats' , JSON . stringify ( stats ) ) ;
130+ }
131+
132+ // Updated calculateTime to record usage
120133function calculateTime ( ) {
121- const currentMa = parseFloat ( document . getElementById ( 'device' ) . value ) || 0 ; // Current in mA from dropdown
122- const volumeMl = parseFloat ( document . getElementById ( 'volume' ) . value ) || 0 ; // Volume in mL
134+ const deviceSelect = document . getElementById ( 'device' ) ;
135+ const currentMa = parseFloat ( deviceSelect . value ) || 0 ;
136+ const volumeMl = parseFloat ( document . getElementById ( 'volume' ) . value ) || 0 ;
123137 const desiredPpm = parseFloat ( document . getElementById ( 'desiredPpm' ) . value ) || 0 ;
124138
125139 if ( currentMa > 0 && volumeMl > 0 && desiredPpm > 0 ) {
126- const current = currentMa / 1000 ; // mA to A
127- const volume = volumeMl / 1000 ; // mL to L
140+ const current = currentMa / 1000 ; // Convert mA to A
141+ const volume = volumeMl / 1000 ; // Convert mL to L
128142 timeInSeconds = ( desiredPpm * volume * z * faradayConstant ) / ( 1000 * current * molarMassSilver ) ;
129143 remainingTime = timeInSeconds ;
130- updateUIText ( { time : formatTime ( timeInSeconds ) } ) ;
144+ const duration = formatTime ( timeInSeconds ) ;
145+ updateUIText ( { time : duration } ) ;
146+
147+ // Record the usage
148+ const deviceName = deviceSelect . options [ deviceSelect . selectedIndex ] . text . split ( ' (' ) [ 0 ] ;
149+ recordUsage ( deviceName , currentMa , duration , volumeMl , desiredPpm ) ;
131150 } else {
132151 alert ( translations . error ) ;
133152 }
@@ -218,6 +237,65 @@ function handleServiceWorkerUpdates() {
218237 }
219238}
220239
240+ // Function to show the statistics screen
241+ function showStatistics ( ) {
242+ const statsContainer = document . getElementById ( 'statistics' ) ;
243+ const mainContainer = document . querySelector ( '.container' ) ;
244+ mainContainer . style . display = 'none' ;
245+ statsContainer . style . display = 'block' ;
246+
247+ const statsBody = document . getElementById ( 'statsBody' ) ;
248+ statsBody . innerHTML = '' ;
249+
250+ const stats = JSON . parse ( localStorage . getItem ( 'usageStats' ) ) || [ ] ;
251+ stats . forEach ( stat => {
252+ const row = document . createElement ( 'tr' ) ;
253+ row . innerHTML = `
254+ <td>${ stat . date } </td>
255+ <td>${ stat . time } </td>
256+ <td>${ stat . device } </td>
257+ <td>${ stat . currentMa } </td>
258+ <td>${ stat . duration } </td>
259+ <td>${ stat . volume } </td>
260+ <td>${ stat . ppm } </td>
261+ ` ;
262+ statsBody . appendChild ( row ) ;
263+ } ) ;
264+ }
265+
266+ // Function to hide the statistics screen
267+ function hideStatistics ( ) {
268+ const statsContainer = document . getElementById ( 'statistics' ) ;
269+ const mainContainer = document . querySelector ( '.container' ) ;
270+ statsContainer . style . display = 'none' ;
271+ mainContainer . style . display = 'block' ;
272+ }
273+
274+ // Function to export statistics as CSV
275+ function exportCSV ( ) {
276+ const stats = JSON . parse ( localStorage . getItem ( 'usageStats' ) ) || [ ] ;
277+ if ( stats . length === 0 ) {
278+ alert ( translations . noData ) ;
279+ return ;
280+ }
281+
282+ const headers = 'Date,Time,Device,Current (mA),Duration (hh:mm:ss),Volume (mL),PPM\n' ;
283+ const csvRows = stats . map ( stat =>
284+ `${ stat . date } ,${ stat . time } ,${ stat . device } ,${ stat . currentMa } ,${ stat . duration } ,${ stat . volume } ,${ stat . ppm } `
285+ ) ;
286+ const csvContent = headers + csvRows . join ( '\n' ) ;
287+
288+ const blob = new Blob ( [ csvContent ] , { type : 'text/csv' } ) ;
289+ const url = URL . createObjectURL ( blob ) ;
290+ const a = document . createElement ( 'a' ) ;
291+ a . href = url ;
292+ a . download = 'SilverTimer_Usage_Statistics.csv' ;
293+ document . body . appendChild ( a ) ;
294+ a . click ( ) ;
295+ document . body . removeChild ( a ) ;
296+ URL . revokeObjectURL ( url ) ;
297+ }
298+
221299// Initialize with translations, devices, and SW update handling
222300window . onload = ( ) => {
223301 loadTranslationsAndDevices ( ) ;
0 commit comments