@@ -135,23 +135,51 @@ <h1>Android File System Demo</h1>
135135
136136 log ( `📂 Directory selected: ${ dirHandle . name } ` ) ;
137137
138- // List contents
139- let count = 0 ;
138+ // List contents and count files
139+ const entries = [ ] ;
140140 log ( "--- Listing Files ---" ) ;
141141 for await ( const entry of dirHandle . values ( ) ) {
142+ entries . push ( entry ) ;
142143 log ( ` - ${ entry . kind } : ${ entry . name } ` ) ;
143- count ++ ;
144- if ( count > 5 ) { log ( "... (more files hidden)" ) ; break ; }
144+ }
145+ if ( entries . length === 0 ) {
146+ log ( " (empty folder)" ) ;
145147 }
146148
147- // Write a test file into this directory
148- log ( "📝 Creating 'test-log.txt' in this folder..." ) ;
149- const newFileHandle = await dirHandle . getFileHandle ( 'test-log.txt' , { create : true } ) ;
150- const writable = await newFileHandle . createWritable ( ) ;
151- await writable . write ( "This file was created by the web app inside your folder!" ) ;
152- await writable . close ( ) ;
149+ // Check if session-log.txt exists and read its content
150+ let existingLog = "" ;
151+ let visitCount = 1 ;
152+ try {
153+ const existingHandle = await dirHandle . getFileHandle ( 'session-log.txt' , { create : false } ) ;
154+ const file = await existingHandle . getFile ( ) ;
155+ existingLog = await file . text ( ) ;
156+ // Count previous visits
157+ const matches = existingLog . match ( / V i s i t # \d + / g) ;
158+ visitCount = matches ? matches . length + 1 : 1 ;
159+ log ( `📖 Found existing session-log.txt (${ visitCount - 1 } previous visits)` ) ;
160+ } catch {
161+ log ( "📝 No previous session-log.txt found, creating fresh log..." ) ;
162+ }
153163
154- log ( "✅ 'test-log.txt' created successfully inside folder." ) ;
164+ // Write/append to session log
165+ const timestamp = new Date ( ) . toISOString ( ) ;
166+ const newEntry = `\n--- Visit #${ visitCount } ---\nTimestamp: ${ timestamp } \nFiles found: ${ entries . length } \n` ;
167+
168+ const logHandle = await dirHandle . getFileHandle ( 'session-log.txt' , { create : true } ) ;
169+ const logWritable = await logHandle . createWritable ( ) ;
170+ await logWritable . write ( existingLog + newEntry ) ;
171+ await logWritable . close ( ) ;
172+ log ( `✅ Updated session-log.txt (Visit #${ visitCount } )` ) ;
173+
174+ // Create a unique timestamped file on each visit
175+ const uniqueFileName = `test-${ Date . now ( ) } .txt` ;
176+ const uniqueHandle = await dirHandle . getFileHandle ( uniqueFileName , { create : true } ) ;
177+ const uniqueWritable = await uniqueHandle . createWritable ( ) ;
178+ await uniqueWritable . write ( `Created during visit #${ visitCount } at ${ timestamp } ` ) ;
179+ await uniqueWritable . close ( ) ;
180+ log ( `✅ Created '${ uniqueFileName } '` ) ;
181+
182+ log ( `\n🎉 Demo complete! Reload page and select same folder to see persistence.` ) ;
155183
156184 } catch ( err ) {
157185 if ( err . name === 'AbortError' ) log ( "User cancelled folder picker." ) ;
0 commit comments