- Now the receive fragment generate the QR code in another thread and don't stop the UI anymore

This commit is contained in:
Javier Varona 2018-10-14 21:55:00 -04:00
parent 319321c94b
commit 8f2fc92945

View file

@ -9,6 +9,7 @@ import android.content.ContextWrapper;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -95,6 +96,8 @@ public class ReceiveTransactionFragment extends DialogFragment implements UIVali
private FloatingActionButton fabReceive; private FloatingActionButton fabReceive;
private AsyncTask qrCodeTask;
public static ReceiveTransactionFragment newInstance(long cryptoNetAccountId) { public static ReceiveTransactionFragment newInstance(long cryptoNetAccountId) {
ReceiveTransactionFragment f = new ReceiveTransactionFragment(); ReceiveTransactionFragment f = new ReceiveTransactionFragment();
@ -323,12 +326,33 @@ public class ReceiveTransactionFragment extends DialogFragment implements UIVali
this.invoice.setTo(grapheneAccountSelected.getName()); this.invoice.setTo(grapheneAccountSelected.getName());
this.invoice.setCurrency(this.cryptoCurrency.getName()); this.invoice.setCurrency(this.cryptoCurrency.getName());
try { if (this.qrCodeTask != null){
Bitmap bitmap = textToImageEncode(Invoice.toQrCode(invoice)); this.qrCodeTask.cancel(true);
ivQrCode.setImageBitmap(bitmap);
} catch (WriterException e) {
Log.e("ReceiveFragment", "Error creating QrCode");
} }
this.qrCodeTask = new AsyncTask<Object, Void, Void>(){
@Override
protected Void doInBackground(Object... voids) {
try {
final Bitmap bitmap = textToImageEncode(Invoice.toQrCode(invoice));
if (!this.isCancelled()) {
ReceiveTransactionFragment.this.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ivQrCode.setImageBitmap(bitmap);
}
});
}
} catch (WriterException e) {
Log.e("ReceiveFragment", "Error creating QrCode");
}
return null;
}
};
this.qrCodeTask.execute(null,null,null);
} }
Bitmap textToImageEncode(String Value) throws WriterException { Bitmap textToImageEncode(String Value) throws WriterException {