From 11144c890b642f5ede94036832f60e2b6b25d0f8 Mon Sep 17 00:00:00 2001 From: Akhil Palla Date: Wed, 21 Sep 2016 00:43:32 -0700 Subject: [PATCH] Fix firebase bugs and add online sync feature in scout/super --- .../java/com/mvrt/scout/FirebaseUtils.java | 20 +++++++++ .../java/com/mvrt/scout/MainActivity.java | 3 -- .../com/mvrt/scout/StandScoutActivity.java | 16 +++++-- .../com/mvrt/superscout/FirebaseUtils.java | 20 +++++++++ .../mvrt/superscout/SuperDataFragment.java | 7 +-- .../mvrt/superscout/SuperScoutActivity.java | 43 ++++++++++++++++--- 6 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 scout/src/main/java/com/mvrt/scout/FirebaseUtils.java create mode 100644 superscout/src/main/java/com/mvrt/superscout/FirebaseUtils.java diff --git a/scout/src/main/java/com/mvrt/scout/FirebaseUtils.java b/scout/src/main/java/com/mvrt/scout/FirebaseUtils.java new file mode 100644 index 00000000..cbc38239 --- /dev/null +++ b/scout/src/main/java/com/mvrt/scout/FirebaseUtils.java @@ -0,0 +1,20 @@ +package com.mvrt.scout; + +import com.google.firebase.database.FirebaseDatabase; + +/** + * Created by akhil on 9/21/2016. + */ +public class FirebaseUtils { + + private static FirebaseDatabase mDatabase; + + public static FirebaseDatabase getDatabase() { + if (mDatabase == null) { + mDatabase = FirebaseDatabase.getInstance(); + mDatabase.setPersistenceEnabled(true); + } + return mDatabase; + } + +} diff --git a/scout/src/main/java/com/mvrt/scout/MainActivity.java b/scout/src/main/java/com/mvrt/scout/MainActivity.java index 220a781b..e3569a4f 100644 --- a/scout/src/main/java/com/mvrt/scout/MainActivity.java +++ b/scout/src/main/java/com/mvrt/scout/MainActivity.java @@ -118,9 +118,6 @@ public void initNFC(){ } private void initFirebase() { - FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance(); - firebaseDatabase.setPersistenceEnabled(true); - final FirebaseRemoteConfig mRemoteConfig = FirebaseRemoteConfig.getInstance(); mRemoteConfig.setDefaults(R.xml.remote_config_defaults); diff --git a/scout/src/main/java/com/mvrt/scout/StandScoutActivity.java b/scout/src/main/java/com/mvrt/scout/StandScoutActivity.java index d5ca92f1..7dc658c9 100644 --- a/scout/src/main/java/com/mvrt/scout/StandScoutActivity.java +++ b/scout/src/main/java/com/mvrt/scout/StandScoutActivity.java @@ -12,8 +12,8 @@ import android.util.Log; import android.widget.Toast; +import com.google.android.gms.tasks.OnSuccessListener; import com.google.firebase.database.DatabaseReference; -import com.google.firebase.database.FirebaseDatabase; import com.mvrt.mvrtlib.util.Constants; import com.mvrt.mvrtlib.util.FragmentPagerAdapter; import com.mvrt.mvrtlib.util.JSONUtils; @@ -49,8 +49,7 @@ protected void onCreate(Bundle savedInstanceState) { } private void initFirebase(){ - FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance(); - matchReference = firebaseDatabase.getReference("matches"); + matchReference = FirebaseUtils.getDatabase().getReference("matches"); } public void loadIntentData(){ @@ -132,6 +131,7 @@ else if(!standScoutPostgameFragment.validate()){ uploadData(matchInfo, scoutId, obj); String filename = writeToFile(obj, matchInfo, scoutId); + Intent i = new Intent(this, MatchScoutingDataActivity.class); i.putExtra(Constants.INTENT_EXTRA_FILENAME, filename); startActivity(i); @@ -162,12 +162,20 @@ public String writeToFile(JSONObject data, MatchInfo matchInfo, int scoutId) thr private void uploadData(MatchInfo info, int scoutId, JSONObject scoutData){ try{ - matchReference.child(info.toDbKey(scoutId)).updateChildren(JSONUtils.jsonToMap(new JSONObject(scoutData.toString()))); + matchReference.child(info.toDbKey(scoutId)).updateChildren(JSONUtils.jsonToMap(new JSONObject(scoutData.toString()))).addOnSuccessListener(syncCompleteListener); }catch(JSONException e){ Toast.makeText(this, "Upload JSONException", Toast.LENGTH_SHORT).show(); Log.e("MVRT", "Upload JSONException"); } } + OnSuccessListener syncCompleteListener = new OnSuccessListener() { + + @Override + public void onSuccess(Void aVoid) { + Toast.makeText(getApplicationContext(), "Synced Online!", Toast.LENGTH_LONG).show(); + } + + }; } diff --git a/superscout/src/main/java/com/mvrt/superscout/FirebaseUtils.java b/superscout/src/main/java/com/mvrt/superscout/FirebaseUtils.java new file mode 100644 index 00000000..3b9acca2 --- /dev/null +++ b/superscout/src/main/java/com/mvrt/superscout/FirebaseUtils.java @@ -0,0 +1,20 @@ +package com.mvrt.superscout; + +import com.google.firebase.database.FirebaseDatabase; + +/** + * Created by akhil on 9/21/2016. + */ +public class FirebaseUtils { + + private static FirebaseDatabase mDatabase; + + public static FirebaseDatabase getDatabase() { + if (mDatabase == null) { + mDatabase = FirebaseDatabase.getInstance(); + mDatabase.setPersistenceEnabled(true); + } + return mDatabase; + } + +} diff --git a/superscout/src/main/java/com/mvrt/superscout/SuperDataFragment.java b/superscout/src/main/java/com/mvrt/superscout/SuperDataFragment.java index 873eb290..e0c56053 100644 --- a/superscout/src/main/java/com/mvrt/superscout/SuperDataFragment.java +++ b/superscout/src/main/java/com/mvrt/superscout/SuperDataFragment.java @@ -50,10 +50,11 @@ public void onClick(View v) { } public void addData(int team, String code){ - Log.d("MVRT", "add data"); String toAppend = "T " + team + ", verif. code: " + code; - if(list != null)list.append(toAppend + System.getProperty("line.separator")); - else queue += toAppend; + queue += toAppend + System.getProperty("line.separator"); + if(list != null){ + list.setText(queue); + } } public void startQR(){ diff --git a/superscout/src/main/java/com/mvrt/superscout/SuperScoutActivity.java b/superscout/src/main/java/com/mvrt/superscout/SuperScoutActivity.java index e5051894..98235d0b 100644 --- a/superscout/src/main/java/com/mvrt/superscout/SuperScoutActivity.java +++ b/superscout/src/main/java/com/mvrt/superscout/SuperScoutActivity.java @@ -15,8 +15,11 @@ import android.util.Log; import android.widget.Toast; +import com.google.firebase.database.ChildEventListener; +import com.google.firebase.database.DataSnapshot; +import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; -import com.google.firebase.database.FirebaseDatabase; +import com.google.firebase.database.Query; import com.mvrt.mvrtlib.util.Constants; import com.mvrt.mvrtlib.util.FragmentPagerAdapter; import com.mvrt.mvrtlib.util.JSONUtils; @@ -30,7 +33,7 @@ /** * @author Bubby and Akhil */ -public class SuperScoutActivity extends AppCompatActivity { +public class SuperScoutActivity extends AppCompatActivity implements ChildEventListener { NfcAdapter nfcAdapter; PendingIntent pendingIntent; @@ -44,6 +47,7 @@ public class SuperScoutActivity extends AppCompatActivity { SuperDataFragment superDataFragment; DatabaseReference matchDataRef; + Query fbQuery; @Override protected void onCreate(Bundle savedInstanceState) { @@ -58,9 +62,9 @@ protected void onCreate(Bundle savedInstanceState) { } private void initFirebase(){ - FirebaseDatabase database = FirebaseDatabase.getInstance(); - database.setPersistenceEnabled(true); - matchDataRef = database.getReference("matches"); + matchDataRef = FirebaseUtils.getDatabase().getReference("matches"); + fbQuery = matchDataRef.orderByChild(Constants.JSON_DATA_MATCHINFO).equalTo(matchInfo.toString()); + fbQuery.addChildEventListener(this); } public void loadIntentData(){ @@ -181,6 +185,7 @@ public void sendSuperData(){ public void finishScouting(){ sendSuperData(); + if(fbQuery != null) fbQuery.removeEventListener(this); finish(); } @@ -191,5 +196,33 @@ public void addQRMatchData(String str){ }catch(JSONException e){ e.printStackTrace(); } } + @Override + public void onChildAdded(DataSnapshot dataSnapshot, String s) { + String matchInfo = (String)dataSnapshot.child(Constants.JSON_DATA_MATCHINFO).getValue(); + MatchInfo i = MatchInfo.parse(matchInfo); + int scoutId = ((Long)dataSnapshot.child(Constants.JSON_DATA_SCOUTID).getValue()).intValue(); + superDataFragment.addData(i.getTeams()[scoutId], "N/A, Synced Online"); + Log.d("MVRT", "DB Child Added: " + i.toString()); + } + + @Override + public void onChildChanged(DataSnapshot dataSnapshot, String s) { + + } + + @Override + public void onChildRemoved(DataSnapshot dataSnapshot) { + + } + + @Override + public void onChildMoved(DataSnapshot dataSnapshot, String s) { + + } + + @Override + public void onCancelled(DatabaseError databaseError) { + + } }