Skip to content

Commit

Permalink
Overall status now updates when new reports are received
Browse files Browse the repository at this point in the history
  • Loading branch information
rkalz committed Jul 21, 2017
1 parent be7ff6d commit e43fa14
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public View getView(final int position, View view, ViewGroup parent) {

// Recover the time of the newest report
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child(lotList.get(position).toString()).limitToLast(1).addValueEventListener(new ValueEventListener() {
mDatabase.child("reports").child(lotList.get(position).toString()).limitToLast(1).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot reportSnapshot : dataSnapshot.getChildren()) {
Expand All @@ -73,18 +73,39 @@ public void onDataChange(DataSnapshot dataSnapshot) {
Report rep = new Report(lotList.get(position), reportStat, reportTime);

lastReport.setText("Last report: " + rep.readableLastReportTime());
switch (rep.getStatus())
{
case 0:
status.setImageResource(R.drawable.low);
break;
case 1:
status.setImageResource(R.drawable.med);
break;
case 2:
status.setImageResource(R.drawable.high);
break;
}

}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

mDatabase.child("overall").child(lotList.get(position).toString()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int overallStatus = -1;
if (dataSnapshot.getValue() != null)
{
long getRaw = (long) dataSnapshot.getValue();
overallStatus = Integer.parseInt(Long.toString(getRaw));
}
switch (overallStatus)
{
case -1:
status.setImageResource(R.drawable.unk);
break;
case 0:
status.setImageResource(R.drawable.low);
break;
case 1:
status.setImageResource(R.drawable.med);
break;
case 2:
status.setImageResource(R.drawable.high);
break;
}
}

Expand Down
46 changes: 38 additions & 8 deletions app/src/main/java/net/rofael/uabparkingfinder/ParkingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,45 @@ protected void onCreate(Bundle savedInstanceState) {
// Receives name of parking lot from selection in main menu
lot = (Parking) getIntent().getParcelableExtra("Parking");
TextView setParkingName = (TextView) findViewById(R.id.parking_name);
TextView setParkingStatus = (TextView) findViewById(R.id.parking_status);
final TextView setParkingStatus = (TextView) findViewById(R.id.parking_status);

// Initializes connection to Google Firebase and checks for reports from server
mDatabase = FirebaseDatabase.getInstance().getReference();
checkFirebase();

// Sets text based on the selection
setParkingName.setText(lot.toString());
setParkingStatus.setText(R.string.parking_status);
mDatabase.child("overall").child(lot.toString()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int overallStatus = -1;
if (dataSnapshot.getValue() != null)
{
long getRaw = (long) dataSnapshot.getValue();
overallStatus = Integer.parseInt(Long.toString(getRaw));
}
switch (overallStatus)
{
case -1:
setParkingStatus.setText(R.string.no_reports);
break;
case 0:
setParkingStatus.setText(R.string.light_parking);
break;
case 1:
setParkingStatus.setText(R.string.medium_parking);
break;
case 2:
setParkingStatus.setText(R.string.heavy_parking);
break;
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

// Sets up the drop down box for report selection
final Spinner dropDownBox = (Spinner) findViewById(R.id.status_selection);
Expand All @@ -72,10 +106,6 @@ protected void onCreate(Bundle savedInstanceState) {
dropDownBox.setOnItemSelectedListener(this);
drop = dropDownBox;

// Initializes connection to Google Firebase and checks for reports from server
mDatabase = FirebaseDatabase.getInstance().getReference();
checkFirebase();

// Initializes the Send button
Button confirmClick = (Button) findViewById(R.id.send_staus);
confirmClick.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -183,7 +213,7 @@ public void addToList() {
reportType = drop.getSelectedItemPosition() - 1;
Report rep = new Report(lot,reportType);
if (reportType > -1 && reportType < 3) {
mDatabase.child(lot.toString()).push().setValue(rep);
mDatabase.child("reports").child(lot.toString()).push().setValue(rep);
checkFirebase();
}

Expand All @@ -195,7 +225,7 @@ public void addToList() {
private void checkFirebase()
{

mDatabase.child(lot.toString()).limitToLast(10).addValueEventListener(new ValueEventListener() {
mDatabase.child("reports").child(lot.toString()).limitToLast(10).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot reportSnapshot: dataSnapshot.getChildren())
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/net/rofael/uabparkingfinder/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public long getReportTime()

public int getStatus() { return status; }

public String getLot() { return parking.toString(); }

String viewStatus()
{
if (status == -1)
Expand Down

0 comments on commit e43fa14

Please sign in to comment.