@@ -290,11 +290,7 @@ async fn handle_host_mode(client: &mut Client, peer_id: PeerId, app: Arc<Mutex<A
290290 |file| {
291291 let reader = BufReader :: new ( file) ;
292292 let mut buffer = String :: new ( ) ;
293- // Read up to 1000 UTF-8 characters (not bytes)
294- reader
295- . take ( 4000 ) // Read up to 4000 bytes, adjust as needed for long UTF-8 chars
296- . read_to_string ( & mut buffer)
297- . ok ( ) ;
293+ reader. take ( 4000 ) . read_to_string ( & mut buffer) . ok ( ) ;
298294 buffer. chars ( ) . take ( 1000 ) . collect ( )
299295 } ,
300296 )
@@ -313,12 +309,17 @@ async fn handle_host_mode(client: &mut Client, peer_id: PeerId, app: Arc<Mutex<A
313309 }
314310 } ;
315311
316- client
312+ // Only send updates if there are changes
313+ if let Err ( e) = client
317314 . insert_directory_items ( peer_id, directory_items)
318315 . await
319- . unwrap ( ) ;
316+ {
317+ tracing:: error!( "Failed to send directory items: {}" , e) ;
318+ break ;
319+ }
320320
321- tokio:: time:: sleep ( tokio:: time:: Duration :: from_millis ( 100 ) ) . await ;
321+ // Sleep for a shorter duration to be more responsive
322+ tokio:: time:: sleep ( tokio:: time:: Duration :: from_millis ( 50 ) ) . await ;
322323 }
323324}
324325
@@ -338,6 +339,7 @@ async fn handle_download_mode(
338339 client. dial ( target_peer_id, target_peer_addr) . await . unwrap ( ) ;
339340 tokio:: time:: sleep ( tokio:: time:: Duration :: from_secs ( 1 ) ) . await ;
340341
342+ // Initial directory request
341343 match client. request_directory ( target_peer_id) . await {
342344 Ok ( display_response) => {
343345 let mut items = display_response. items ;
@@ -357,6 +359,46 @@ async fn handle_download_mode(
357359 app. current_path = std:: path:: PathBuf :: new ( ) ;
358360 app. populate_directory_items ( ) ;
359361 }
362+
363+ // Start a background task to handle directory updates
364+ let mut client_clone = client. clone ( ) ;
365+ let app_clone = app. clone ( ) ;
366+ tokio:: spawn ( async move {
367+ loop {
368+ tokio:: time:: sleep ( tokio:: time:: Duration :: from_millis ( 100 ) ) . await ;
369+ match client_clone. request_directory ( target_peer_id) . await {
370+ Ok ( display_response) => {
371+ let mut items = display_response. items ;
372+ items. sort_by ( |a, b| match ( a. is_dir , b. is_dir ) {
373+ ( true , false ) => std:: cmp:: Ordering :: Less ,
374+ ( false , true ) => std:: cmp:: Ordering :: Greater ,
375+ _ => match a. depth . cmp ( & b. depth ) {
376+ std:: cmp:: Ordering :: Equal => {
377+ a. name . to_lowercase ( ) . cmp ( & b. name . to_lowercase ( ) )
378+ }
379+ other => other,
380+ } ,
381+ } ) ;
382+
383+ let mut app = app_clone. lock ( ) ;
384+ if app. all_shared_items != items {
385+ app. all_shared_items . clone_from ( & items) ;
386+ app. directory_items = items;
387+ app. populate_directory_items ( ) ;
388+ // Notify UI to refresh
389+ if let Some ( refresh_sender) = & app. refresh_sender {
390+ let _ = refresh_sender. try_send ( ( ) ) ;
391+ }
392+ }
393+ }
394+ Err ( e) => {
395+ tracing:: error!( "Failed to request directory: {}" , e) ;
396+ break ;
397+ }
398+ }
399+ }
400+ } ) ;
401+
360402 Ok ( ( ) )
361403 }
362404 Err ( e) => {
0 commit comments