Commit c70a7133 authored by Buwaneka 's avatar Buwaneka

add ChartShow.java

parent 23dbda51
package com.example.count;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class ChartShow extends AppCompatActivity {
FirebaseStorage storage;
StorageReference storageReference;
public String urilist="";
public String resuilt="";
public int count=0;
FirebaseAuth mAuth = FirebaseAuth.getInstance();
private DatabaseReference mDatabase= FirebaseDatabase.getInstance().getReference("StudentData");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chart_layout);
GlobleVariable.wait=false;
urilist="";
resuilt="";
count=0;
storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();
BarChart chart = (BarChart) findViewById(R.id.chart);
ArrayList NoOfresult= new ArrayList();
for(int i=0;i<GlobleVariable.resultList.size();i++){
NoOfresult.add(new BarEntry(GlobleVariable.resultList.get(i), i));
}
ArrayList tryNo = new ArrayList();
for(int i=0;i<GlobleVariable.resultList.size();i++){
tryNo.add(Integer.toString(i+1)+"Attempt");
}
BarDataSet bardataset = new BarDataSet(NoOfresult, "Student Progress");
chart.animateY(3000);
BarData data = new BarData(tryNo, bardataset);
bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
chart.setData(data);
Button btn = findViewById(R.id.upload);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressBar uploadPB=(ProgressBar) findViewById(R.id.uploadbar);
uploadPB.setVisibility(View.VISIBLE);
background newbackGP = new background();
newbackGP.start();
}
});
}
class background extends Thread{
@Override
public void run(){
for (int i=0;i<GlobleVariable.bitmapNo.size();i++){
uploadImage(GlobleVariable.bitmapNo.get(i),i);
while(true){
if(GlobleVariable.wait){
GlobleVariable.wait=false;
break;
}
}
}
for(int y=0;y<GlobleVariable.resultList.size();y++){
String result= GlobleVariable.resultList.get(y).toString();
resuilt= resuilt + result + " ";
}
uploaddata();
startActivity(new Intent(ChartShow.this, Done.class));
}
public String Geanrate_Name() {
String DATA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghtjklmnopqrstunvzxyz";
Random RANDOM = new Random();
StringBuilder sb = new StringBuilder(10);
for (int i = 0; i < 10; i++) {
sb.append(DATA.charAt(RANDOM.nextInt(DATA.length())));
}
return sb.toString();
}
public void uploaddata(){
UserDetails data = new UserDetails(resuilt,urilist);
mDatabase.child("students").child(GlobleVariable.StudentID).setValue(data);
}
private void uploadImage(Bitmap converetdImage, final int i){
if(converetdImage != null)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
converetdImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
converetdImage.recycle();
final String randomname=Geanrate_Name();
GlobleVariable.nameurl=randomname;
StorageReference ref = storageReference.child("uploaded/"+randomname+".jpeg");
UploadTask uploadTask=ref.putBytes(byteArray);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//progressDialog.dismiss();
Geturl(GlobleVariable.nameurl);
try {
// code runs in a thread
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ChartShow.this, "Uploaded Image "+String.valueOf(i+1), Toast.LENGTH_SHORT).show();
}
});
} catch (final Exception ex) {
Log.i("---","Exception in thread");
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
final Exception xe=e;
//progressDialog.dismiss();
try {
// code runs in a thread
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ChartShow.this, "Failed "+xe.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} catch (final Exception ex) {
Log.i("---","Exception in thread");
}
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
/*double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploading Data Please Wait! "+(int)progress+"%");*/
}
});
}
//deleteImage();
}
public void Geturl(String name){
storageReference.child("uploaded/"+name+".jpeg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url= uri.toString();
urilist= urilist + url + " ";
GlobleVariable.wait=true;
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
}
});
}
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
// do your stuff
} else {
signInAnonymously();
}
}
private void signInAnonymously() {
mAuth.signInAnonymously().addOnSuccessListener(this, new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
// do your stuff
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
}
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment