Skip to content

Commit

Permalink
Sample QR Codes + Button clickable when valid QR scanned
Browse files Browse the repository at this point in the history
  • Loading branch information
nb2998 committed Oct 11, 2018
1 parent 837b2d8 commit 792c1e9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 52 deletions.
Binary file added SampleQRCodes/non_student.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SampleQRCodes/student.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -61,53 +61,6 @@ protected void onCreate(Bundle savedInstanceState) {

id = getIntent().getIntExtra(getString(R.string.loginId), 24);

btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference ref = firebaseDatabase.getReference();

String unique = "";
if(id ==24) unique = "-LO-JpcKGtMtWZuqnXOD";
else unique = "-LO-JpcJWPJ2CjM4tNu9";

final DatabaseReference curr = ref.child("Student").child(String.valueOf(id)).child(unique);
Log.d("TAG", "onClick: "+curr);

curr.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot!=null) {
if(checkBox.isChecked()){
amountToBeDeducted = 5;
}

long currBal = (Long) dataSnapshot.child("currentBalance").getValue();
Log.d("TAG", "onDataChange: " + currBal);
currBal -= amountToBeDeducted;

ref.child("Student").child(String.valueOf(id)).child("-LO-JpcKGtMtWZuqnXOD").child("currentBalance").setValue(currBal);

} else {
Log.d("TAG", "onDataChange: " + null);
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

Intent successfulIntent = new Intent(ScannedBarcodeActivity.this, ConfirmationActivity.class);
successfulIntent.putExtra(getString(R.string.loginId), id);
successfulIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(successfulIntent);


}
});

}

private void initialiseDetectorsAndSources() {
Expand Down Expand Up @@ -163,6 +116,13 @@ public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0) {

btnAction.setClickable(true);
btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takeAction();
}
});

txtBarcodeValue.post(new Runnable() {

Expand All @@ -174,13 +134,10 @@ public void run() {
intentData = barcodes.valueAt(0).email.address;
txtBarcodeValue.setText(intentData);
isEmail = true;
btnAction.setText("ADD CONTENT TO THE MAIL");
} else {
isEmail = false;
btnAction.setText("LAUNCH URL");
intentData = barcodes.valueAt(0).displayValue;
txtBarcodeValue.setText(intentData);

}
}
});
Expand All @@ -190,6 +147,50 @@ public void run() {
});
}

private void takeAction() {
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference ref = firebaseDatabase.getReference();

String unique = "";
if(id ==24) unique = "-LO-JpcKGtMtWZuqnXOD";
else unique = "-LO-JpcJWPJ2CjM4tNu9";

final DatabaseReference curr = ref.child("Student").child(String.valueOf(id)).child(unique);
Log.d("TAG", "onClick: "+curr);

curr.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot!=null) {
if(checkBox.isChecked()){
amountToBeDeducted = 5;
}

long currBal = (Long) dataSnapshot.child("currentBalance").getValue();
Log.d("TAG", "onDataChange: " + currBal);
currBal -= amountToBeDeducted;

ref.child("Student").child(String.valueOf(id)).child("-LO-JpcKGtMtWZuqnXOD").child("currentBalance").setValue(currBal);

} else {
Log.d("TAG", "onDataChange: " + null);
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

Intent successfulIntent = new Intent(ScannedBarcodeActivity.this, ConfirmationActivity.class);
successfulIntent.putExtra(getString(R.string.loginId), id);
successfulIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(successfulIntent);


}


@Override
protected void onPause() {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_scanned_barcode.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Proceed" />
android:text="Proceed"
android:clickable="false"/>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"

android:text="Amount:"
android:textSize="18sp" />

Expand Down

0 comments on commit 792c1e9

Please # to comment.