From 99761094726533eebbad2a0feed07f04b26bb2d3 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sun, 22 Apr 2018 22:57:16 +0200 Subject: [PATCH] Add remove option on watchlist --- .../HomeActivityFragments/Watchlist.java | 100 +++++- .../DataManagers/DatabaseManager.java | 7 + .../res/drawable/ic_mode_edit_white_24dp.xml | 9 + .../main/res/layout/cardview_watchlist.xml | 288 ++++++++++-------- .../main/res/layout/custom_currency_row.xml | 4 +- .../fragment_watchlist_homeactivity.xml | 10 + app/src/main/res/values/strings.xml | 1 + 7 files changed, 286 insertions(+), 133 deletions(-) create mode 100644 app/src/main/res/drawable/ic_mode_edit_white_24dp.xml diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java index 65e3c51..469f41e 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java @@ -8,18 +8,17 @@ import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; import android.graphics.drawable.Drawable; import android.os.AsyncTask; -import android.support.design.widget.AppBarLayout; import android.support.v4.app.Fragment; import android.os.Bundle; -import android.support.v4.view.ViewPager; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.graphics.Palette; import android.support.v7.widget.CardView; +import android.util.DisplayMetrics; import android.util.Log; +import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.ViewParent; import android.view.animation.Animation; import android.view.animation.Transformation; import android.widget.Button; @@ -28,7 +27,6 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; -import android.support.v7.widget.Toolbar; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.Entry; @@ -43,6 +41,7 @@ import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyDataChart; import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyDetailsList; import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyTickerList; +import com.nauk.coinfolio.DataManagers.DatabaseManager; import com.nauk.coinfolio.DataManagers.PreferencesManager; import com.nauk.coinfolio.DataManagers.WatchlistManager; import com.nauk.coinfolio.PlaceholderManager; @@ -77,6 +76,7 @@ public class Watchlist extends Fragment { private CurrencyTickerList currencyTickerList; private boolean tickerUpdated; private boolean detailsUpdated; + private boolean editModeEnabled; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) @@ -99,6 +99,8 @@ public class Watchlist extends Fragment { } }); + editModeEnabled = false; + watchlistManager = new WatchlistManager(getContext()); updateWatchlist(true); @@ -121,7 +123,6 @@ public class Watchlist extends Fragment { }); ImageButton settingsButton = view.findViewById(R.id.settings_button); - settingsButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -130,6 +131,33 @@ public class Watchlist extends Fragment { } }); + ImageButton editButton = view.findViewById(R.id.edit_button); + editButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if(editModeEnabled) + { + editModeEnabled = false; + + for(int i = 0; i < ((LinearLayout) Watchlist.this.view.findViewById(R.id.linearLayoutWatchlist)).getChildCount(); i++) + { + ((LinearLayout) Watchlist.this.view.findViewById(R.id.linearLayoutWatchlist)).getChildAt(i).setClickable(true); + collapseW(((LinearLayout) Watchlist.this.view.findViewById(R.id.linearLayoutWatchlist)).getChildAt(i).findViewById(R.id.deleteCardWatchlist)); + } + } + else + { + editModeEnabled = true; + + for(int i = 0; i < ((LinearLayout) Watchlist.this.view.findViewById(R.id.linearLayoutWatchlist)).getChildCount(); i++) + { + ((LinearLayout) Watchlist.this.view.findViewById(R.id.linearLayoutWatchlist)).getChildAt(i).setClickable(false); + expandW(((LinearLayout) Watchlist.this.view.findViewById(R.id.linearLayoutWatchlist)).getChildAt(i).findViewById(R.id.deleteCardWatchlist)); + } + } + } + }); + return view; } @@ -172,6 +200,31 @@ public class Watchlist extends Fragment { v.startAnimation(a); } + private static void expandW(final View v) { + v.measure(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT); + final int targetWidth = v.getMeasuredWidth(); + + v.getLayoutParams().width = 1; + v.setVisibility(View.VISIBLE); + Animation a = new Animation() { + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + v.getLayoutParams().width = interpolatedTime == 1 + ? CardView.LayoutParams.WRAP_CONTENT + : (int)(targetWidth * interpolatedTime); + v.requestLayout(); + } + + @Override + public boolean willChangeBounds() { + return true; + } + }; + + a.setDuration((int)(targetWidth / v.getContext().getResources().getDisplayMetrics().density)); + v.startAnimation(a); + } + private static void collapse(final View v) { final int initialHeight = v.getMeasuredHeight(); @@ -198,6 +251,30 @@ public class Watchlist extends Fragment { v.startAnimation(a); } + private static void collapseW(final View v) { + final int initialWidth = v.getMeasuredWidth(); + + Animation a = new Animation() { + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + if(interpolatedTime == 1) { + v.setVisibility(View.GONE); + } else { + v.getLayoutParams().width = initialWidth - (int)(initialWidth * interpolatedTime); + v.requestLayout(); + } + } + + @Override + public boolean willChangeBounds() { + return true; + } + }; + + a.setDuration((int)(initialWidth / v.getContext().getResources().getDisplayMetrics().density)); + v.startAnimation(a); + } + @Override public void onResume() @@ -301,7 +378,9 @@ public class Watchlist extends Fragment { private View getCurrencyCardFor(final Currency currency) { - View card = LayoutInflater.from(getContext()).inflate(R.layout.cardview_watchlist, null); + final View card = LayoutInflater.from(getContext()).inflate(R.layout.cardview_watchlist, null); + + card.setTag(currency.getSymbol()); ((TextView) card.findViewById(R.id.currencyFluctuationTextView)) .setText(PlaceholderManager.getValueParenthesisString(numberConformer(currency.getDayFluctuation()), getActivity())); @@ -328,6 +407,15 @@ public class Watchlist extends Fragment { progressDrawable.setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN)); progressDrawable.invalidateSelf(); + card.findViewById(R.id.deleteCardWatchlist).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + collapse(card); + DatabaseManager databaseManager = new DatabaseManager(getActivity()); + databaseManager.deleteCurrencyFromWatchlist(currency.getSymbol()); + } + }); + updateColor(card, currency); card.findViewById(R.id.LineChartView).setOnClickListener(new View.OnClickListener() { diff --git a/app/src/main/java/com/nauk/coinfolio/DataManagers/DatabaseManager.java b/app/src/main/java/com/nauk/coinfolio/DataManagers/DatabaseManager.java index f8cb330..9b45445 100644 --- a/app/src/main/java/com/nauk/coinfolio/DataManagers/DatabaseManager.java +++ b/app/src/main/java/com/nauk/coinfolio/DataManagers/DatabaseManager.java @@ -103,6 +103,13 @@ public class DatabaseManager extends SQLiteOpenHelper{ db.close(); } + public int deleteCurrencyFromWatchlist(String symbol) + { + SQLiteDatabase db = this.getWritableDatabase(); + + return db.delete(TABLE_WATCHLIST, KEY_WATCHLIST_SYMBOL + " = '" + symbol + "'", null); + } + public List getAllCurrenciesFromWatchlist() { String searchQuerry = "SELECT * FROM " + TABLE_WATCHLIST; diff --git a/app/src/main/res/drawable/ic_mode_edit_white_24dp.xml b/app/src/main/res/drawable/ic_mode_edit_white_24dp.xml new file mode 100644 index 0000000..35a774a --- /dev/null +++ b/app/src/main/res/drawable/ic_mode_edit_white_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/cardview_watchlist.xml b/app/src/main/res/layout/cardview_watchlist.xml index e970242..b5e658a 100644 --- a/app/src/main/res/layout/cardview_watchlist.xml +++ b/app/src/main/res/layout/cardview_watchlist.xml @@ -5,7 +5,8 @@ xmlns:app="http://schemas.android.com/apk/res-auto" android:clickable="true" android:focusable="true" - android:paddingBottom="4dp"> + android:paddingBottom="4dp" + android:tag="ParentCon"> + app:layout_constraintBottom_toBottomOf="parent" + android:tag="Parent"> + android:orientation="horizontal"> + android:clickable="false" + android:orientation="vertical" + android:tag="mainLinear"> - - - - - - - - - - - - + android:baselineAligned="false"> + android:layout_margin="5dp" + android:orientation="vertical" + android:layout_weight="0.92"> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + android:background="@drawable/ic_delete_white_24dp" + android:clickable="false" + android:focusable="false"/> - - - - + android:visibility="gone" + android:orientation="vertical"> - - - - - - - - - + android:layout_margin="5dp"> - - - - - + android:text="Day history" + android:textSize="@dimen/cardViewSecondaryText" /> + + + + + + + + + + + + + + + + - + diff --git a/app/src/main/res/layout/custom_currency_row.xml b/app/src/main/res/layout/custom_currency_row.xml index 14cc9f3..85c1afd 100644 --- a/app/src/main/res/layout/custom_currency_row.xml +++ b/app/src/main/res/layout/custom_currency_row.xml @@ -6,8 +6,8 @@ + android:orientation="horizontal"> + + + Coinfolio Settings + Edit mode Fais un don wallah Coinfolio_debug Coinfolio_debugVolley