Skip to content

Commit

Permalink
Fix firebase bugs and add online sync feature in scout/super
Browse files Browse the repository at this point in the history
  • Loading branch information
akhil99 committed Sep 21, 2016
1 parent 3ef17e8 commit 11144c8
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 15 deletions.
20 changes: 20 additions & 0 deletions scout/src/main/java/com/mvrt/scout/FirebaseUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
3 changes: 0 additions & 3 deletions scout/src/main/java/com/mvrt/scout/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 12 additions & 4 deletions scout/src/main/java/com/mvrt/scout/StandScoutActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Void> syncCompleteListener = new OnSuccessListener<Void>() {

@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "Synced Online!", Toast.LENGTH_LONG).show();
}

};

}
20 changes: 20 additions & 0 deletions superscout/src/main/java/com/mvrt/superscout/FirebaseUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +33,7 @@
/**
* @author Bubby and Akhil
*/
public class SuperScoutActivity extends AppCompatActivity {
public class SuperScoutActivity extends AppCompatActivity implements ChildEventListener {

NfcAdapter nfcAdapter;
PendingIntent pendingIntent;
Expand All @@ -44,6 +47,7 @@ public class SuperScoutActivity extends AppCompatActivity {
SuperDataFragment superDataFragment;

DatabaseReference matchDataRef;
Query fbQuery;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -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(){
Expand Down Expand Up @@ -181,6 +185,7 @@ public void sendSuperData(){

public void finishScouting(){
sendSuperData();
if(fbQuery != null) fbQuery.removeEventListener(this);
finish();
}

Expand All @@ -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) {

}
}

0 comments on commit 11144c8

Please sign in to comment.