Skip to content

Instantly share code, notes, and snippets.

@DavidInnocent
Created April 30, 2020 08:59
Show Gist options
  • Save DavidInnocent/8e4fafc573e74e2869e9f37bf6f723f6 to your computer and use it in GitHub Desktop.
Save DavidInnocent/8e4fafc573e74e2869e9f37bf6f723f6 to your computer and use it in GitHub Desktop.
package com.co.ximmoz.FireFy.ui.views.Activities;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.PointF;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.co.ximmoz.FireFy.ui.views.auth.LoginActivity;
import com.co.ximmoz.FireFy.utils.RedeemToken;
import com.co.ximmoz.FireFy.data.models.User;
import com.co.ximmoz.adfy.R;
import com.dlazaro66.qrcodereaderview.QRCodeReaderView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.ValueEventListener;
public class ScanQrActivity extends AppCompatActivity implements QRCodeReaderView.OnQRCodeReadListener {
private static final int PERMISSION_REQUEST_CODE = 200;
private QRCodeReaderView qrCodeReaderView;
private DatabaseReference databaseReference;
private RedeemToken token_data;
private Button Dashboardd;
private double final_amount_of_token=0;
@Override
protected void onStart() {
super.onStart();
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
if(user==null){
startActivity(new Intent(ScanQrActivity.this, LoginActivity.class));
finish();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_qr);
databaseReference= FirebaseDatabase.getInstance().getReference();
qrCodeReaderView = findViewById(R.id.qrdecoderview);
if (checkPermission()) {
qrCodeReaderView.setOnQRCodeReadListener(this);
// Use this function to enable/disable decoding
qrCodeReaderView.setQRDecodingEnabled(true);
//Use this to set the image to display in the background_green.
//qrCodeReaderView.setBackgroundResource(R.drawable.qr_background);
// Use this function to change the autofocus interval (default is 5 secs)
qrCodeReaderView.setAutofocusInterval(5000L);
// Use this function to enable/disable Torch
qrCodeReaderView.setTorchEnabled(true);
// Use this function to set front camera preview
qrCodeReaderView.setPreviewCameraId(0);
qrCodeReaderView.setFrontCamera();
// Use this function to set back camera preview
qrCodeReaderView.setBackCamera();
} else {
requestPermission();
}
}
private boolean checkPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
return false;
}
return true;
}
private void requestPermission() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
PERMISSION_REQUEST_CODE);
}
@Override
public void onQRCodeRead(final String token_text, PointF[] points) {
final String user_id=FirebaseAuth.getInstance().getCurrentUser().getUid();
if(token_text.toLowerCase().endsWith("bat"))
{
databaseReference.child("Redeem").child(token_text).child(user_id).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
Toast.makeText(ScanQrActivity.this, "You have already redeemed" , Toast.LENGTH_SHORT).show();
startActivity(new Intent(ScanQrActivity.this, UserDashboardActivity.class));
finish();
}
else
{
databaseReference.child("Redeem").child(token_text).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists())
{
Toast.makeText(ScanQrActivity.this, "None Redeemable Code Detected", Toast.LENGTH_SHORT).show();
return;
}
token_data=dataSnapshot.getValue(RedeemToken.class);
databaseReference.child("USERS").child(user_id).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user=dataSnapshot.getValue(User.class);
double amount_to_award_user=user.getAccountBalance()+token_data.getAmount_to_award();
final double final_amount_of_token1=token_data.getAccount_balance()-token_data.getAmount_to_award();
databaseReference.child("USERS").child(user_id).child("accountBalance").setValue(amount_to_award_user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
{
databaseReference.child("Redeem").child(token_text).child(user_id).setValue(true).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
{
databaseReference.child("Redeem").child(token_text).child("account_balance").setValue(final_amount_of_token1).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
{
Toast.makeText(ScanQrActivity.this, "DONEEEEE", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(ScanQrActivity.this, "failed for some reason", Toast.LENGTH_SHORT).show();
}
}
});
}
else
{
Toast.makeText(ScanQrActivity.this, "FAILEDDDDD", Toast.LENGTH_SHORT).show();
}
}
});
}
else
{
Toast.makeText(ScanQrActivity.this, "Something Went Wrong", Toast.LENGTH_SHORT).show();
return;
}
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ScanQrActivity.this, "Something Went Wrong", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ScanQrActivity.this, "Something Went Wrong", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ScanQrActivity.this, "Something Went Wrong", Toast.LENGTH_SHORT).show();
}
});
}
else {
databaseReference.child("SHOPS").child(token_text).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
Intent intent =new Intent(ScanQrActivity.this, shopOwnerRedeemActivity.class);
intent.putExtra("ShopID",token_text);
startActivity(intent);
finish();
}
else
{
Toast.makeText(ScanQrActivity.this, "Business Not Registered", Toast.LENGTH_SHORT).show();
startActivity(new Intent(ScanQrActivity.this, UserDashboardActivity.class));
finish();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
startActivity(new Intent(ScanQrActivity.this, UserDashboardActivity.class));
finish();
}
});
}
}
@Override
public void onBackPressed() {
startActivity(new Intent(ScanQrActivity.this, UserDashboardActivity.class));
finish();
}
@Override
protected void onResume() {
super.onResume();
qrCodeReaderView.startCamera();
}
@Override
protected void onPause() {
super.onPause();
qrCodeReaderView.stopCamera();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
return;
} else {
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
showMessageOKCancel("Camera access needed to capture QR Codes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermission();
}
}
});
}
}
}
break;
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(ScanQrActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment