diff --git a/app/src/main/java/com/nauk/moodl/Activities/HomeActivity.java b/app/src/main/java/com/nauk/moodl/Activities/HomeActivity.java index 09e1a21..ee34ddb 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/HomeActivity.java +++ b/app/src/main/java/com/nauk/moodl/Activities/HomeActivity.java @@ -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); diff --git a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Overview.java b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Overview.java new file mode 100644 index 0000000..c92c6a5 --- /dev/null +++ b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Overview.java @@ -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() { + + @Override + protected Void doInBackground(Void... voids) { + + currencyTickerList.getCurrenciesFrom(listLayout.getCount(), preferenceManager.getDefaultCurrency(), new UpdateCallBack() { + @Override + public void onSuccess(List 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 currencyList); + } +} diff --git a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java index 0b86c0b..10727b7 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java +++ b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java @@ -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); diff --git a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Watchlist.java b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Watchlist.java index 47ead6b..161ecf0 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Watchlist.java +++ b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Watchlist.java @@ -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; diff --git a/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyTickerList.java b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyTickerList.java index 8e8d099..30c01eb 100644 --- a/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyTickerList.java +++ b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyTickerList.java @@ -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 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() { + @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() { @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 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 { diff --git a/app/src/main/java/com/nauk/moodl/LayoutManagers/OverviewListAdapter.java b/app/src/main/java/com/nauk/moodl/LayoutManagers/OverviewListAdapter.java new file mode 100644 index 0000000..f0244e1 --- /dev/null +++ b/app/src/main/java/com/nauk/moodl/LayoutManagers/OverviewListAdapter.java @@ -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 { + + private Context context; + + public OverviewListAdapter(Context context, List 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; + } +} diff --git a/app/src/main/res/layout/fragment_overview_homeactivity.xml b/app/src/main/res/layout/fragment_overview_homeactivity.xml new file mode 100644 index 0000000..e35ed17 --- /dev/null +++ b/app/src/main/res/layout/fragment_overview_homeactivity.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/summary_navigation_drawer.xml b/app/src/main/res/menu/summary_navigation_drawer.xml index c07ba87..e4f3e73 100644 --- a/app/src/main/res/menu/summary_navigation_drawer.xml +++ b/app/src/main/res/menu/summary_navigation_drawer.xml @@ -3,8 +3,8 @@ + +