Fix & improvements

- Added coin list in navigation drawer
- Rework the coin list interface to load dynamicly
- Fix dust not being counted in balance
This commit is contained in:
Tanguy Herbron 2018-05-28 01:34:44 +02:00
parent d0069f4ad5
commit aeb9c9cd18
8 changed files with 369 additions and 41 deletions

View File

@ -10,7 +10,6 @@ import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
@ -19,6 +18,7 @@ import android.widget.CompoundButton;
import android.widget.Switch;
import com.nauk.moodl.Activities.HomeActivityFragments.MarketCapitalization;
import com.nauk.moodl.Activities.HomeActivityFragments.Overview;
import com.nauk.moodl.Activities.HomeActivityFragments.Summary;
import com.nauk.moodl.Activities.HomeActivityFragments.Watchlist;
import com.nauk.moodl.HideBalanceSwitch;
@ -38,6 +38,7 @@ public class HomeActivity extends AppCompatActivity {
private Fragment watchlistFragment;
private Fragment holdingsFragment;
private Fragment marketFragment;
private Fragment overviewFragment;
private Fragment currentFragment;
@ -57,6 +58,7 @@ public class HomeActivity extends AppCompatActivity {
watchlistFragment = new Watchlist();
holdingsFragment = new Summary();
marketFragment = new MarketCapitalization();
overviewFragment = new Overview();
drawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
@ -65,7 +67,7 @@ public class HomeActivity extends AppCompatActivity {
showFragment(holdingsFragment);
navigationView.setCheckedItem(R.id.navigation_currencies_list);
navigationView.setCheckedItem(R.id.navigation_holdings);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
@ -78,12 +80,15 @@ public class HomeActivity extends AppCompatActivity {
case R.id.navigation_watchlist:
showFragment(watchlistFragment);
break;
case R.id.navigation_currencies_list:
case R.id.navigation_holdings:
showFragment(holdingsFragment);
break;
case R.id.navigation_market_cap:
showFragment(marketFragment);
break;
case R.id.navigation_overview:
showFragment(overviewFragment);
break;
case R.id.navigation_settings:
Intent settingIntent = new Intent(getApplicationContext(), SettingsActivity.class);
startActivity(settingIntent);
@ -158,22 +163,6 @@ public class HomeActivity extends AppCompatActivity {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id)
{
case R.id.navigation_settings:
break;
}
return super.onOptionsItemSelected(item);
}
public interface IconCallBack
{
void onSuccess(Bitmap bitmap);

View File

@ -0,0 +1,158 @@
package com.nauk.moodl.Activities.HomeActivityFragments;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.graphics.Palette;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.nauk.moodl.Activities.HomeActivity;
import com.nauk.moodl.DataManagers.CurrencyData.Currency;
import com.nauk.moodl.DataManagers.CurrencyData.CurrencyCardview;
import com.nauk.moodl.DataManagers.CurrencyData.CurrencyDetailsList;
import com.nauk.moodl.DataManagers.CurrencyData.CurrencyTickerList;
import com.nauk.moodl.DataManagers.PreferencesManager;
import com.nauk.moodl.LayoutManagers.OverviewListAdapter;
import com.nauk.moodl.MoodlBox;
import com.nauk.moodl.R;
import java.util.List;
/**
* Created by Administrator on 27/05/2018.
*/
public class Overview extends Fragment {
private CurrencyTickerList currencyTickerList;
private CurrencyDetailsList currencyDetailsList;
private PreferencesManager preferenceManager;
private OverviewListAdapter overviewListAdapter;
boolean flag_loading;
private ListView listLayout;
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View fragmentView = inflater.inflate(R.layout.fragment_overview_homeactivity, container, false);
currencyTickerList = CurrencyTickerList.getInstance(getContext());
currencyDetailsList = CurrencyDetailsList.getInstance(getContext());
preferenceManager = new PreferencesManager(getContext());
listLayout = fragmentView.findViewById(R.id.linearLayoutOverview);
listLayout.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0)
{
if(!flag_loading)
{
flag_loading = true;
updateList();
}
}
}
});
updateList();
return fragmentView;
}
private void updateList()
{
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
currencyTickerList.getCurrenciesFrom(listLayout.getCount(), preferenceManager.getDefaultCurrency(), new UpdateCallBack() {
@Override
public void onSuccess(List<Currency> currencyList)
{
for(Currency currency : currencyList)
{
String iconUrl = MoodlBox.getIconUrl(currency.getSymbol(), currencyDetailsList);
if(iconUrl != null)
{
MoodlBox.getBitmapFromURL(iconUrl, currency.getSymbol(), getResources(), getContext(), new HomeActivity.IconCallBack() {
@Override
public void onSuccess(Bitmap bitmap) {
currency.setIcon(bitmap);
updateChartColor(currency);
}
});
}
else
{
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_moodl);
icon = Bitmap.createScaledBitmap(icon, 50, 50, false);
currency.setIcon(icon);
updateChartColor(currency);
}
}
if(overviewListAdapter == null)
{
overviewListAdapter = new OverviewListAdapter(getContext(), currencyList);
listLayout.setAdapter(overviewListAdapter);
listLayout.setTextFilterEnabled(false);
}
else
{
overviewListAdapter.addAll(currencyList);
overviewListAdapter.notifyDataSetChanged();
}
flag_loading = false;
}
});
return null;
}
}.execute();
}
private void updateChartColor(Currency currency)
{
if(currency.getIcon() != null)
{
Palette.Builder builder = Palette.from(currency.getIcon());
currency.setChartColor(builder.generate().getDominantColor(0));
}
else
{
currency.setChartColor(12369084);
}
}
public interface UpdateCallBack
{
void onSuccess(List<Currency> currencyList);
}
}

View File

@ -16,7 +16,6 @@ import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.graphics.Palette;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
@ -39,11 +38,6 @@ import com.nauk.moodl.MoodlBox;
import com.nauk.moodl.PlaceholderManager;
import com.nauk.moodl.R;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
@ -514,7 +508,7 @@ public class Summary extends Fragment implements HideBalanceSwitch {
private void loadCurrency(Currency currency)
{
if(!currency.getSymbol().equals("USD") && (currency.getBalance() * currency.getValue()) > preferencesManager.getMinimumAmount())
if(!currency.getSymbol().equals("USD"))
{
currency.setName(balanceManager.getCurrencyName(currency.getSymbol()));
currency.setId(balanceManager.getCurrencyId(currency.getSymbol()));
@ -693,7 +687,7 @@ public class Summary extends Fragment implements HideBalanceSwitch {
{
if(!currencyTickerList.isUpToDate())
{
currencyTickerList.update(new BalanceManager.IconCallBack() {
currencyTickerList.updateListing(new BalanceManager.IconCallBack() {
@Override
public void onSuccess() {
countCoins(false, false, true);

View File

@ -11,7 +11,6 @@ import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -22,7 +21,6 @@ import android.widget.LinearLayout;
import com.nauk.moodl.Activities.CurrencySelectionActivity;
import com.nauk.moodl.Activities.HomeActivity;
import com.nauk.moodl.Activities.SettingsActivity;
import com.nauk.moodl.DataManagers.BalanceManager;
import com.nauk.moodl.DataManagers.CurrencyData.Currency;
import com.nauk.moodl.DataManagers.CurrencyData.CurrencyCardview;
@ -36,10 +34,6 @@ import com.nauk.moodl.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import static com.nauk.moodl.MoodlBox.collapseW;
import static com.nauk.moodl.MoodlBox.expandW;
import static java.lang.Math.abs;
@ -107,7 +101,7 @@ public class Watchlist extends Fragment {
protected Void doInBackground(Void... voids) {
if(!currencyTickerList.isUpToDate())
{
currencyTickerList.update(new BalanceManager.IconCallBack() {
currencyTickerList.updateListing(new BalanceManager.IconCallBack() {
@Override
public void onSuccess() {
tickerUpdated = true;

View File

@ -9,6 +9,7 @@ import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.nauk.moodl.Activities.HomeActivityFragments.Overview;
import com.nauk.moodl.DataManagers.BalanceManager;
import org.json.JSONArray;
@ -16,9 +17,8 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
/**
* Created by Guitoune on 22/04/2018.
@ -26,7 +26,8 @@ import java.util.regex.Pattern;
public class CurrencyTickerList {
final private static String TICKERLISTURL = "https://api.coinmarketcap.com/v2/listings/";
final private static String LISTINGURL = "https://api.coinmarketcap.com/v2/listings/";
final private static String TICKERLISTURL1 = "https://api.coinmarketcap.com/v2/ticker/?start=";
private RequestQueue requestQueue;
private List<Currency> currencyTickerList;
private static CurrencyTickerList INSTANCE;
@ -52,10 +53,34 @@ public class CurrencyTickerList {
return upToDate;
}
public void update(final BalanceManager.IconCallBack callBack)
public void getCurrenciesFrom(int indexFrom, final String toSymbol, Overview.UpdateCallBack callBack)
{
String requetsString = TICKERLISTURL1 + indexFrom + "&limit=50&convert=" + toSymbol;
StringRequest strRequest = new StringRequest(Request.Method.GET, requetsString,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.length() > 0)
{
processTickersResult(response, toSymbol, callBack);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(strRequest);
}
public void updateListing(final BalanceManager.IconCallBack callBack)
{
currencyTickerList = new ArrayList<>();
StringRequest strRequest = new StringRequest(Request.Method.GET, TICKERLISTURL,
StringRequest strRequest = new StringRequest(Request.Method.GET, LISTINGURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
@ -93,6 +118,40 @@ public class CurrencyTickerList {
return tickerId;
}
private void processTickersResult(String response, String toSymbol, Overview.UpdateCallBack callBack)
{
List<Currency> currencyList = new ArrayList<>();
try {
JSONObject masterJsonObject = new JSONObject(response);
if(masterJsonObject.keys().hasNext())
{
JSONObject currencyJsonObject = masterJsonObject.getJSONObject(masterJsonObject.keys().next());
Iterator<?> keys = currencyJsonObject.keys();
while(keys.hasNext())
{
String key = keys.next().toString();
JSONObject subCurrencyJsonObject = currencyJsonObject.getJSONObject(key);
Currency newCurrency = new Currency(subCurrencyJsonObject.getString("name"), subCurrencyJsonObject.getString("symbol"), subCurrencyJsonObject.getInt("id"));
newCurrency.setRank(subCurrencyJsonObject.getInt("rank"));
JSONObject quoteJsonObject = subCurrencyJsonObject.getJSONObject("quotes");
JSONObject symJsonObject = quoteJsonObject.getJSONObject(toSymbol);
newCurrency.setValue(symJsonObject.getDouble("price"));
newCurrency.setDayFluctuationPercentage((float) symJsonObject.getDouble("percent_change_24h"));
newCurrency.setDayFluctuation(newCurrency.getDayFluctuationPercentage() * newCurrency.getValue() / 100);
currencyList.add(newCurrency);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
callBack.onSuccess(currencyList);
}
public void processTickerListResult(String response, BalanceManager.IconCallBack callBack)
{
try {

View File

@ -0,0 +1,62 @@
package com.nauk.moodl.LayoutManagers;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nauk.moodl.DataManagers.CurrencyData.Currency;
import com.nauk.moodl.PlaceholderManager;
import com.nauk.moodl.R;
import java.util.List;
import static com.nauk.moodl.MoodlBox.numberConformer;
/**
* Created by Administrator on 28/05/2018.
*/
public class OverviewListAdapter extends ArrayAdapter<Currency> {
private Context context;
public OverviewListAdapter(Context context, List<Currency> currencies)
{
super(context, android.R.layout.simple_list_item_1, currencies);
this.context = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
{
Currency currency = getItem(position);
if(convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.cardview_watchlist, parent, false);
}
TextView symbolTxtView = convertView.findViewById(R.id.currencySymbolTextView);
TextView nameTxtView = convertView.findViewById(R.id.currencyNameTextView);
TextView valueTxtView = convertView.findViewById(R.id.currencyValueTextView);
TextView fluctuationTxtView = convertView.findViewById(R.id.currencyFluctuationTextView);
TextView percentageTxtView = convertView.findViewById(R.id.currencyFluctuationPercentageTextView);
ImageView iconImageView = convertView.findViewById(R.id.currencyIcon);
symbolTxtView.setText(currency.getSymbol());
nameTxtView.setText(currency.getName());
valueTxtView.setText(PlaceholderManager.getValueString(numberConformer(currency.getValue()), getContext()));
fluctuationTxtView.setText(PlaceholderManager.getValueParenthesisString(numberConformer(currency.getDayFluctuation()), getContext()));
percentageTxtView.setText(PlaceholderManager.getPercentageString(numberConformer(currency.getDayFluctuationPercentage()), getContext()));
iconImageView.setImageBitmap(currency.getIcon());
return convertView;
}
}

View File

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/fragment_padding"
android:background="@drawable/gradient_background">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="pin"
android:layout_margin="10dp">
<ImageButton
android:id="@+id/drawer_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/action_settings"
android:background="@drawable/ic_drawer_white_24dp"
android:visibility="visible"
android:layout_gravity="start|center_vertical"
android:contentDescription="Drawer"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Coin list"
android:textSize="18sp"
android:textColor="@color/white"
android:textStyle="bold"/>
</FrameLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize"
android:background="@color/summary_background"
android:paddingTop="15dp">
<ListView android:id="@+id/linearLayoutOverview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

View File

@ -3,8 +3,8 @@
<group android:checkableBehavior="single">
<item
android:id="@+id/navigation_currencies_list"
android:icon="@drawable/ic_view_list_black_24dp"
android:id="@+id/navigation_holdings"
android:icon="@drawable/ic_account_balance_wallet_black_24dp"
android:title="@string/title_home" />
<item
@ -17,6 +17,11 @@
android:icon="@drawable/ic_pie_chart_black_24dp"
android:title="@string/title_market_cap" />
<item
android:id="@+id/navigation_overview"
android:icon="@drawable/ic_view_list_black_24dp"
android:title="Coin list"/>
</group>
<item android:title="Other">