2222import com .google .firebase .auth .AuthResult ;
2323import com .google .firebase .auth .FirebaseAuth ;
2424import com .google .firebase .auth .FirebaseUser ;
25+ import com .google .firebase .database .DataSnapshot ;
26+ import com .google .firebase .database .DatabaseError ;
2527import com .google .firebase .database .FirebaseDatabase ;
28+ import com .google .firebase .database .GenericTypeIndicator ;
2629import com .google .firebase .database .Query ;
30+ import com .google .firebase .database .ValueEventListener ;
2731import com .google .firebase .remoteconfig .FirebaseRemoteConfig ;
2832import com .google .firebase .remoteconfig .FirebaseRemoteConfigSettings ;
2933
34+ import java .util .Map ;
35+
3036import io .fabric .sdk .android .Fabric ;
3137import tech .akpmakes .android .taskkeeper .firebase .WhenAdapter ;
3238import tech .akpmakes .android .taskkeeper .models .WhenEvent ;
@@ -40,6 +46,7 @@ public class MainActivity extends AppCompatActivity implements FirebaseAuth.Auth
4046 private RecyclerView mRecyclerView ;
4147 private FirebaseRecyclerAdapter mAdapter ;
4248 private LinearLayoutManager mLayoutManager ;
49+ private Map <String , WhenEvent > localCopyOfItems ;
4350
4451 protected void onCreate (Bundle savedInstanceState ) {
4552 super .onCreate (savedInstanceState );
@@ -94,6 +101,14 @@ private void applyRemoteConfig() {
94101
95102 public void onAuthStateChanged (@ NonNull FirebaseAuth auth ) {
96103 updateUI (auth .getCurrentUser ());
104+
105+ boolean isSignedIn = (auth .getCurrentUser () != null && !auth .getCurrentUser ().isAnonymous ());
106+
107+ if (mDBQuery != null && localCopyOfItems != null && isSignedIn ) {
108+ for (Map .Entry <String , WhenEvent > entry : localCopyOfItems .entrySet ()) {
109+ mDBQuery .getRef ().child (entry .getKey ()).setValue (entry .getValue ());
110+ }
111+ }
97112 }
98113
99114 @ Override
@@ -147,6 +162,17 @@ private void updateUI(FirebaseUser user) {
147162 mDBQuery .keepSynced (true );
148163 mAdapter = new WhenAdapter (this , mDBQuery );
149164 mRecyclerView .setAdapter (mAdapter );
165+
166+ mDBQuery .addValueEventListener (new ValueEventListener () {
167+ @ Override
168+ public void onDataChange (DataSnapshot dataSnapshot ) {
169+ GenericTypeIndicator <Map <String , WhenEvent >> t = new GenericTypeIndicator <Map <String , WhenEvent >>(){};
170+ localCopyOfItems = dataSnapshot .getValue (t );
171+ }
172+
173+ @ Override
174+ public void onCancelled (DatabaseError databaseError ) {}
175+ });
150176 }
151177
152178 @ Override
0 commit comments