Fix icon download delay

This commit is contained in:
Tanguy Herbron 2018-02-02 00:28:58 +01:00
parent f17e3a0dc8
commit ba4655f64a

View File

@ -4,6 +4,7 @@ import android.app.Dialog;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.CollapsingToolbarLayout;
@ -53,6 +54,7 @@ public class HomeActivity extends AppCompatActivity {
private BalanceManager balanceManager; private BalanceManager balanceManager;
private int coinCounter; private int coinCounter;
private int iconCounter;
private HomeLayoutGenerator layoutGenerator; private HomeLayoutGenerator layoutGenerator;
private LinearLayout currencyLayout; private LinearLayout currencyLayout;
private Toolbar toolbar; private Toolbar toolbar;
@ -256,10 +258,13 @@ public class HomeActivity extends AppCompatActivity {
private void resetCounter() private void resetCounter()
{ {
coinCounter = 0; coinCounter = 0;
iconCounter = 0;
iconChecker = false; iconChecker = false;
} }
private Bitmap getBitmapFromURL(String src) { private void getBitmapFromURL(String src, IconCallBack callBack) {
Bitmap result;
try { try {
java.net.URL url = new java.net.URL(src); java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url HttpURLConnection connection = (HttpURLConnection) url
@ -267,33 +272,24 @@ public class HomeActivity extends AppCompatActivity {
connection.setDoInput(true); connection.setDoInput(true);
connection.connect(); connection.connect();
InputStream input = connection.getInputStream(); InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input); result = BitmapFactory.decodeStream(input);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} result = null;
return null;
} }
private void countCoins(boolean isCoin, boolean isDetails) callBack.onSuccess(result);
}
private void countIcons()
{ {
float totalValue = 0; float totalValue = 0;
float totalFluctuation = 0; float totalFluctuation = 0;
if(isCoin) iconCounter++;
{
coinCounter++;
}
if(isDetails) if(iconCounter == balanceManager.getTotalBalance().size())
{ {
iconChecker = true;
}
for(int i = 0; i < coinCounter; i++)
{
balanceManager.getTotalBalance().get(i).setIcon(getBitmapFromURL(balanceManager.getIconUrl(balanceManager.getTotalBalance().get(i).getSymbol())));
}
if(balanceManager.getTotalBalance() != null) if(balanceManager.getTotalBalance() != null)
{ {
@ -391,6 +387,36 @@ public class HomeActivity extends AppCompatActivity {
} }
} }
} }
}
private void countCoins(boolean isCoin, boolean isDetails)
{
if(isCoin)
{
coinCounter++;
}
if(isDetails)
{
iconChecker = true;
}
if(coinCounter == balanceManager.getTotalBalance().size()-1)
{
for (int i = 0; i < balanceManager.getTotalBalance().size(); i++)
{
final int index = i;
getBitmapFromURL(balanceManager.getIconUrl(balanceManager.getTotalBalance().get(i).getSymbol()), new IconCallBack() {
@Override
public void onSuccess(Bitmap bitmapIcon) {
balanceManager.getTotalBalance().get(index).setIcon(bitmapIcon);
countIcons();
}
});
}
}
}
private void updateViewButtonIcon() private void updateViewButtonIcon()
{ {
@ -515,4 +541,9 @@ public class HomeActivity extends AppCompatActivity {
} }
} }
public interface IconCallBack
{
void onSuccess(Bitmap bitmap);
}
} }