@@ -13,6 +13,7 @@ use crate::sharing::{
1313 peers:: { Friend , Permission } ,
1414} ;
1515use std:: path:: PathBuf ;
16+ use std:: sync:: Mutex ;
1617use std:: time:: Duration ;
1718use zgui:: prelude:: * ;
1819use zgui:: reactive:: UnsyncCallback ;
@@ -304,6 +305,31 @@ fn change(
304305 } ) ;
305306}
306307
308+ /// Pairing outlives the page: Android drops the window while Roamlog is hidden (e.g. behind the
309+ /// share sheet), and the page rebuilt on return picks the session up again.
310+ struct Session {
311+ pairing : Pairing ,
312+ /// Group being invited to; page title says so.
313+ inviting : Option < String > ,
314+ /// Started on the Groups page; a failure returns there.
315+ from_groups : bool ,
316+ /// Who and which group the last confirm step was about; Done arrives after Waiting.
317+ confirmed : Option < ( String , Option < String > , bool ) > ,
318+ /// Invitation expiry for the countdown.
319+ expires : Option < u64 > ,
320+ }
321+
322+ static SESSION : Mutex < Option < Session > > = Mutex :: new ( None ) ;
323+
324+ fn session < R > ( f : impl FnOnce ( & mut Option < Session > ) -> R ) -> R {
325+ f ( & mut SESSION . lock ( ) . expect ( "pairing lock" ) )
326+ }
327+
328+ /// Pairing in progress; its endpoint holds the identity, so sharing must not restart meanwhile.
329+ pub ( crate ) fn pairing_open ( ) -> bool {
330+ session ( |x| x. is_some ( ) )
331+ }
332+
307333pub fn sheet ( ) -> impl IntoView {
308334 view ! {
309335 Friends ( )
@@ -318,7 +344,6 @@ pub fn Friends() -> impl IntoView {
318344 let saved_name = std:: fs:: read_to_string ( directory. join ( "sharing-name" ) ) . unwrap_or_default ( ) ;
319345 let name = RwSignal :: new_local ( saved_name) ;
320346 let loaded: RwSignal < Option < Loaded > , LocalStorage > = RwSignal :: new_local ( None ) ;
321- let pairing: StoredValue < Option < Pairing > , LocalStorage > = StoredValue :: new_local ( None ) ;
322347 let step = RwSignal :: new_local ( PairStep :: Idle ) ;
323348 let qr: StoredValue < Option < ImageBytes > , LocalStorage > = StoredValue :: new_local ( None ) ;
324349 let qr_url = RwSignal :: new_local ( None :: < String > ) ;
@@ -341,14 +366,24 @@ pub fn Friends() -> impl IntoView {
341366 } ;
342367 // Pairing panel: open while adding a friend, joining from a link, or pairing.
343368 let adding = RwSignal :: new_local ( false ) ;
344- // Who and which group the last confirm step was about; Done arrives after Waiting.
345- let last_confirm = StoredValue :: new_local ( None :: < ( String , Option < String > , bool ) > ) ;
369+ let groups_open = RwSignal :: new_local ( false ) ;
370+ // Group the shown invitation is for; page title says so.
371+ let inviting = RwSignal :: new_local ( None :: < String > ) ;
372+ // Back from another app mid-pairing: reopen the pairing panel where it was.
373+ if let Some ( ( group, from_groups) ) =
374+ session ( |x| x. as_ref ( ) . map ( |x| ( x. inviting . clone ( ) , x. from_groups ) ) )
375+ {
376+ inviting. set ( group) ;
377+ groups_open. set ( from_groups) ;
378+ adding. set ( true ) ;
379+ s. friends_open . set ( true ) ;
380+ }
346381 // Poll pairing progress and friend status while sheet is open.
347382 let timer = set_interval ( Duration :: from_millis ( 500 ) , move || {
348383 if !s. friends_open . get_untracked ( ) {
349384 return ;
350385 }
351- let next = pairing . with_value ( |p| p . as_ref ( ) . map ( Pairing :: step) ) ;
386+ let next = session ( |x| x . as_ref ( ) . map ( |x| x . pairing . step ( ) ) ) ;
352387 if let Some ( next) = next
353388 && step. with_untracked ( |old| old != & next)
354389 {
@@ -372,20 +407,35 @@ pub fn Friends() -> impl IntoView {
372407 ..
373408 } = & next
374409 {
375- last_confirm. set_value ( Some ( ( name. clone ( ) , group. clone ( ) , * joining) ) ) ;
410+ let confirmed = Some ( ( name. clone ( ) , group. clone ( ) , * joining) ) ;
411+ session ( |x| {
412+ if let Some ( x) = x {
413+ x. confirmed = confirmed;
414+ }
415+ } ) ;
376416 }
417+ let confirmed = session ( |x| x. as_ref ( ) . and_then ( |x| x. confirmed . clone ( ) ) ) ;
377418 if let PairStep :: Done ( message) = & next {
378- let text = match last_confirm . get_value ( ) {
419+ let text = match confirmed . clone ( ) {
379420 Some ( ( _, Some ( group) , true ) ) => format ! ( "Joined {group}." ) ,
380421 Some ( ( name, Some ( group) , false ) ) => format ! ( "Added {name} to {group}." ) ,
381422 _ => message. clone ( ) ,
382423 } ;
383424 s. message . set ( text) ;
384425 adding. set ( false ) ;
385426 }
427+ // Group pairing failed: back to the Groups page with the reason, not Add friend.
428+ if let PairStep :: Failed ( error) = & next
429+ && ( groups_open. get_untracked ( )
430+ || inviting. get_untracked ( ) . is_some ( )
431+ || confirmed. is_some_and ( |( _, group, _) | group. is_some ( ) ) )
432+ {
433+ s. message . set ( pairing_error ( error) ) ;
434+ adding. set ( false ) ;
435+ }
386436 step. set ( next) ;
387437 if finished {
388- pairing . set_value ( None ) ;
438+ session ( |x| * x = None ) ;
389439 qr. set_value ( None ) ;
390440 qr_url. set ( None ) ;
391441 // Pairing endpoint closed; let service rebind its node.
@@ -410,8 +460,6 @@ pub fn Friends() -> impl IntoView {
410460 } ) ;
411461 on_cleanup_local ( move || drop ( opened) ) ;
412462 let join_access = RwSignal :: new_local ( Permission :: TripsOnly ) ;
413- // Group the shown invitation is for; page title says so.
414- let inviting = RwSignal :: new_local ( None :: < String > ) ;
415463 let begin = move |link : Option < String > , group : Option < GroupRef > | {
416464 let display = name. get_untracked ( ) . trim ( ) . to_owned ( ) ;
417465 if display. is_empty ( ) {
@@ -423,12 +471,22 @@ pub fn Friends() -> impl IntoView {
423471 let _ = std:: fs:: write ( directory. join ( "sharing-name" ) , & display) ;
424472 crate :: platform:: suspend_sharing ( & directory) ;
425473 join_access. set ( Permission :: TripsOnly ) ;
426- inviting. set ( group. as_ref ( ) . map ( |g| g. name . clone ( ) ) ) ;
474+ let group_name = group. as_ref ( ) . map ( |g| g. name . clone ( ) ) ;
475+ inviting. set ( group_name. clone ( ) ) ;
427476 step. set ( PairStep :: Starting ) ;
428- pairing. set_value ( Some ( Pairing :: start ( directory, display, link, group) ) ) ;
477+ let pairing = Pairing :: start ( directory, display, link, group) ;
478+ session ( |x| {
479+ * x = Some ( Session {
480+ pairing,
481+ inviting : group_name,
482+ from_groups : groups_open. get_untracked ( ) ,
483+ confirmed : None ,
484+ expires : None ,
485+ } )
486+ } ) ;
429487 } ;
430488 let cancel = move || {
431- pairing . set_value ( None ) ;
489+ session ( |x| * x = None ) ;
432490 qr. set_value ( None ) ;
433491 qr_url. set ( None ) ;
434492 step. set ( PairStep :: Idle ) ;
@@ -444,7 +502,9 @@ pub fn Friends() -> impl IntoView {
444502 let invite_clock = zgui:: reactive:: RenderEffect :: new ( move |_| {
445503 if matches ! ( step. get( ) , PairStep :: Invite { .. } ) {
446504 if invite_until. with_untracked ( Option :: is_none) {
447- invite_until. set ( Some ( now_ms ( ) + 300_000 ) ) ;
505+ let fresh = now_ms ( ) + 300_000 ;
506+ let until = session ( |x| x. as_mut ( ) . map ( |x| * x. expires . get_or_insert ( fresh) ) ) ;
507+ invite_until. set ( Some ( until. unwrap_or ( fresh) ) ) ;
448508 }
449509 } else {
450510 invite_until. set ( None ) ;
@@ -463,7 +523,6 @@ pub fn Friends() -> impl IntoView {
463523 let group_name = RwSignal :: new_local ( String :: new ( ) ) ;
464524 let group_policy = RwSignal :: new_local ( "managed" . to_string ( ) ) ;
465525 let group_access = RwSignal :: new_local ( Permission :: Always ) ;
466- let groups_open = RwSignal :: new_local ( false ) ;
467526 let new_group = RwSignal :: new_local ( false ) ;
468527 let detail = s. friend_detail ;
469528 // Opened straight to a friend (e.g. from the map): drop sub-pages left open underneath,
@@ -595,9 +654,11 @@ pub fn Friends() -> impl IntoView {
595654 PairStep :: Confirm {
596655 group : Some ( group) , ..
597656 } => format ! ( "Invite to {group}" ) ,
598- _ => inviting
599- . get ( )
600- . map_or_else ( || "Add friend" . into ( ) , |group| format ! ( "Invite to {group}" ) ) ,
657+ _ => match inviting. get ( ) {
658+ Some ( group) => format ! ( "Invite to {group}" ) ,
659+ None if groups_open. get ( ) => "Join group" . into ( ) ,
660+ None => "Add friend" . into ( ) ,
661+ } ,
601662 }
602663 } else if let Some ( id) = detail. get ( ) {
603664 rows ( )
@@ -716,8 +777,8 @@ pub fn Friends() -> impl IntoView {
716777 }
717778 }
718779 row( class="row-actions" ) {
719- Button ( class="confirm-pair" , on: click=move |_|pairing . with_value ( |p |if let Some ( p ) =p { p . vote( true , if matches!( step. get_untracked( ) , PairStep :: Confirm { group: None , ..} ) { direct_access. get_untracked( ) } else { join_access. get_untracked( ) } ) } ) ) { "Codes match" }
720- Button ( variant=ButtonVariant :: Outline , class="reject-pair" , on: click=move |_|pairing . with_value ( |p |if let Some ( p ) =p { p . vote( false , join_access. get_untracked( ) ) } ) ) { "Codes differ — reject" }
780+ Button ( class="confirm-pair" , on: click=move |_|session ( |x |if let Some ( x ) =x { x . pairing . vote( true , if matches!( step. get_untracked( ) , PairStep :: Confirm { group: None , ..} ) { direct_access. get_untracked( ) } else { join_access. get_untracked( ) } ) } ) ) { "Codes match" }
781+ Button ( variant=ButtonVariant :: Outline , class="reject-pair" , on: click=move |_|session ( |x |if let Some ( x ) =x { x . pairing . vote( false , join_access. get_untracked( ) ) } ) ) { "Codes differ — reject" }
721782 }
722783 }
723784 if move ||matches!( step. get( ) , PairStep :: Waiting ) {
0 commit comments