@@ -16,7 +16,7 @@ use popover::{AuthIntent, Popover};
1616use crate :: config:: Config ;
1717use beeps_core:: {
1818 sync:: { login, register, Client } ,
19- NodeId , Replica ,
19+ Document , NodeId , Replica ,
2020} ;
2121use chrono:: { DateTime , Local , Utc } ;
2222use crossterm:: event:: { Event , KeyCode , KeyEvent , KeyEventKind } ;
@@ -42,7 +42,12 @@ pub struct App {
4242
4343 /// If we're replacing the entire replica on the next sync (for example when
4444 /// initially logging in.)
45- replace_on_next_sync : bool ,
45+ in_first_sync : bool ,
46+
47+ /// The document we got on our first sync. We keep this separate to decide
48+ /// whether to replace our current document with this or merge them
49+ /// together.
50+ first_sync_document : Option < Document > ,
4651
4752 /// State of the pings table
4853 table_state : TableState ,
@@ -89,7 +94,8 @@ impl App {
8994 Ok ( Self {
9095 status_line : Some ( "Loaded replica" . to_string ( ) ) ,
9196 replica,
92- replace_on_next_sync : false ,
97+ in_first_sync : false ,
98+ first_sync_document : None ,
9399 client : auth,
94100 last_sync : None ,
95101 table_state : TableState :: new ( ) . with_selected ( 0 ) ,
@@ -104,7 +110,8 @@ impl App {
104110 status_line : None ,
105111 replica : Replica :: new ( NodeId :: random ( ) ) ,
106112 client : auth,
107- replace_on_next_sync : false ,
113+ in_first_sync : false ,
114+ first_sync_document : None ,
108115 last_sync : None ,
109116 table_state : TableState :: new ( ) . with_selected ( 0 ) ,
110117 popover : None ,
@@ -253,7 +260,7 @@ impl App {
253260 }
254261 Action :: LoggedIn ( client) => {
255262 self . client = Some ( client. clone ( ) ) ;
256- self . replace_on_next_sync = true ;
263+ self . in_first_sync = true ;
257264
258265 vec ! [
259266 Effect :: SaveSyncClientAuth ( client. clone( ) ) ,
@@ -273,12 +280,12 @@ impl App {
273280 Action :: Pulled ( resp) => {
274281 self . status_line = Some ( "Got a new doc from the server" . to_string ( ) ) ;
275282
276- if self . replace_on_next_sync {
277- self . replica . replace_doc ( resp. document ) ;
278- self . replace_on_next_sync = false ;
283+ if self . in_first_sync {
284+ self . first_sync_document = Some ( resp. document ) ;
285+ self . popover = Some ( Popover :: ConfirmReplaceOrMerge ) ;
279286 } else {
280287 self . replica . merge ( resp. document ) ;
281- }
288+ } ;
282289
283290 vec ! [ ]
284291 }
@@ -355,6 +362,25 @@ impl App {
355362 }
356363 _ => auth. handle_event ( key) ,
357364 } ,
365+ Some ( Popover :: ConfirmReplaceOrMerge ) => match key. code {
366+ KeyCode :: Char ( 'r' ) => {
367+ self . dismiss_popover ( ) ;
368+ self . in_first_sync = false ;
369+ if let Some ( document) = self . first_sync_document . take ( ) {
370+ self . replica . replace_doc ( document) ;
371+ effects. push ( Effect :: SaveReplica ( self . replica . clone ( ) ) ) ;
372+ }
373+ }
374+ KeyCode :: Char ( 'm' ) => {
375+ self . dismiss_popover ( ) ;
376+ self . in_first_sync = false ;
377+ if let Some ( document) = self . first_sync_document . take ( ) {
378+ self . replica . merge ( document) ;
379+ effects. push ( Effect :: SaveReplica ( self . replica . clone ( ) ) ) ;
380+ }
381+ }
382+ _ => ( ) ,
383+ } ,
358384 }
359385
360386 effects
0 commit comments