From 142b00aff5548631aedcdbc74dbb04e5e27a1904 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Mon, 4 Jun 2018 01:48:53 +0200 Subject: [PATCH] Add reorder option for Watchlist currencies | New edit buttons --- app/build.gradle | 1 + .../HomeActivityFragments/Watchlist.java | 40 +++++++++++++++---- .../CurrencyData/CurrencyCardview.java | 11 +++++ .../moodl/DataManagers/DatabaseManager.java | 30 ++++++++++++-- .../main/java/com/herbron/moodl/MoodlBox.java | 17 ++++++++ .../main/res/drawable/ic_check_white_24dp.xml | 9 +++++ .../main/res/drawable/ic_drag_handle_24dp.xml | 9 +++++ .../main/res/layout/cardview_watchlist.xml | 19 +++++++++ .../fragment_watchlist_homeactivity.xml | 27 ++++++------- 9 files changed, 137 insertions(+), 26 deletions(-) create mode 100644 app/src/main/res/drawable/ic_check_white_24dp.xml create mode 100644 app/src/main/res/drawable/ic_drag_handle_24dp.xml diff --git a/app/build.gradle b/app/build.gradle index ccdb374..382ed82 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -46,6 +46,7 @@ dependencies { implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3' implementation 'com.mattprecious.swirl:swirl:1.1.0' implementation 'com.wdullaer:materialdatetimepicker:3.5.2' + implementation 'com.jmedeisis:draglinearlayout:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' diff --git a/app/src/main/java/com/herbron/moodl/Activities/HomeActivityFragments/Watchlist.java b/app/src/main/java/com/herbron/moodl/Activities/HomeActivityFragments/Watchlist.java index 309f304..62b3032 100644 --- a/app/src/main/java/com/herbron/moodl/Activities/HomeActivityFragments/Watchlist.java +++ b/app/src/main/java/com/herbron/moodl/Activities/HomeActivityFragments/Watchlist.java @@ -17,6 +17,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageButton; +import android.widget.ImageView; import android.widget.LinearLayout; import com.herbron.moodl.Activities.CurrencySelectionActivity; @@ -26,10 +27,12 @@ import com.herbron.moodl.DataManagers.CurrencyData.Currency; import com.herbron.moodl.DataManagers.CurrencyData.CurrencyCardview; import com.herbron.moodl.DataManagers.CurrencyData.CurrencyDetailsList; import com.herbron.moodl.DataManagers.CurrencyData.CurrencyTickerList; +import com.herbron.moodl.DataManagers.DatabaseManager; import com.herbron.moodl.DataManagers.PreferencesManager; import com.herbron.moodl.DataManagers.WatchlistManager; import com.herbron.moodl.MoodlBox; import com.herbron.moodl.R; +import com.jmedeisis.draglinearlayout.DragLinearLayout; import org.json.JSONException; import org.json.JSONObject; @@ -49,6 +52,7 @@ public class Watchlist extends Fragment { private int watchlistCounter; private CurrencyDetailsList currencyDetailsList; private SwipeRefreshLayout refreshLayout; + private DragLinearLayout dragLinearLayout; private long lastTimestamp; private PreferencesManager preferencesManager; private String defaultCurrency; @@ -56,6 +60,7 @@ public class Watchlist extends Fragment { private boolean tickerUpdated; private boolean detailsUpdated; private boolean editModeEnabled; + private DatabaseManager databaseManager; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) @@ -63,8 +68,10 @@ public class Watchlist extends Fragment { view = inflater.inflate(R.layout.fragment_watchlist_homeactivity, container, false); refreshLayout = view.findViewById(R.id.swiperefreshwatchlist); + dragLinearLayout = view.findViewById(R.id.linearLayoutWatchlist); currencyDetailsList = CurrencyDetailsList.getInstance(getContext()); preferencesManager = new PreferencesManager(getContext()); + databaseManager = new DatabaseManager(getContext()); lastTimestamp = 0; defaultCurrency = preferencesManager.getDefaultCurrency(); @@ -72,6 +79,17 @@ public class Watchlist extends Fragment { tickerUpdated = false; updateTickerList(); + dragLinearLayout.setOnViewSwapListener(new DragLinearLayout.OnViewSwapListener() { + @Override + public void onSwap(View firstView, int firstPosition, View secondView, int secondPosition) { + CurrencyCardview currencyCardviewMoved = (CurrencyCardview) firstView; + CurrencyCardview currencyCardviewSwaped = (CurrencyCardview) secondView; + + databaseManager.updateWatchlistPosition(currencyCardviewMoved.getCurrency().getSymbol(), secondPosition); + databaseManager.updateWatchlistPosition(currencyCardviewSwaped.getCurrency().getSymbol(), firstPosition); + } + }); + editModeEnabled = false; watchlistManager = new WatchlistManager(getContext()); @@ -128,35 +146,40 @@ public class Watchlist extends Fragment { editButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - LinearLayout watchlistLayout = Watchlist.this.view.findViewById(R.id.linearLayoutWatchlist); if(editModeEnabled) { + ((ImageView) view.findViewById(R.id.edit_button)).setBackground(MoodlBox.getDrawable(R.drawable.ic_mode_edit_white_24dp, getContext())); + editModeEnabled = false; - for(int i = 0; i < watchlistLayout.getChildCount(); i++) + for(int i = 0; i < dragLinearLayout.getChildCount(); i++) { - View watchlistElement = watchlistLayout.getChildAt(i); + View watchlistElement = dragLinearLayout.getChildAt(i); if(watchlistElement instanceof CurrencyCardview) { watchlistElement.setClickable(true); collapseW(watchlistElement.findViewById(R.id.deleteCardWatchlist)); + collapseW(watchlistElement.findViewById(R.id.dragCardWatchlist)); } } } else { + ((ImageView) view.findViewById(R.id.edit_button)).setBackground(MoodlBox.getDrawable(R.drawable.ic_check_white_24dp, getContext())); + editModeEnabled = true; - for(int i = 0; i < watchlistLayout.getChildCount(); i++) + for(int i = 0; i < dragLinearLayout.getChildCount(); i++) { - View watchlistElement = watchlistLayout.getChildAt(i); + View watchlistElement = dragLinearLayout.getChildAt(i); if(watchlistElement instanceof CurrencyCardview) { watchlistElement.setClickable(false); expandW(watchlistElement.findViewById(R.id.deleteCardWatchlist)); + expandW(watchlistElement.findViewById(R.id.dragCardWatchlist)); } } } @@ -279,11 +302,14 @@ public class Watchlist extends Fragment { getActivity().runOnUiThread(new Runnable() { @Override public void run() { - ((LinearLayout) view.findViewById(R.id.linearLayoutWatchlist)).removeAllViews(); + dragLinearLayout.removeAllViews(); + view.findViewById(R.id.progressBarWatchlist).setVisibility(View.GONE); for(Currency currency : watchlistManager.getWatchlist()) { - ((LinearLayout) view.findViewById(R.id.linearLayoutWatchlist)).addView(new CurrencyCardview(getContext(), currency, getActivity())); + View addedView = new CurrencyCardview(getContext(), currency, getActivity()); + + dragLinearLayout.addDragView(addedView, addedView.findViewById(R.id.dragCardWatchlist)); } } }); diff --git a/app/src/main/java/com/herbron/moodl/DataManagers/CurrencyData/CurrencyCardview.java b/app/src/main/java/com/herbron/moodl/DataManagers/CurrencyData/CurrencyCardview.java index 01c96b8..71d52d2 100644 --- a/app/src/main/java/com/herbron/moodl/DataManagers/CurrencyData/CurrencyCardview.java +++ b/app/src/main/java/com/herbron/moodl/DataManagers/CurrencyData/CurrencyCardview.java @@ -43,6 +43,8 @@ import static com.herbron.moodl.MoodlBox.numberConformer; public class CurrencyCardview extends CardView { + private Currency currency; + public CurrencyCardview(@NonNull Context context) { super(context); } @@ -51,6 +53,8 @@ public class CurrencyCardview extends CardView { { super (context); + this.currency = currency; + LayoutInflater.from(context).inflate(R.layout.cardview_watchlist, this, true); ((LineChart) findViewById(R.id.LineChartView)).setNoDataTextColor(currency.getChartColor()); @@ -136,6 +140,8 @@ public class CurrencyCardview extends CardView { { super(context); + this.currency = currency; + LayoutInflater.from(context).inflate(R.layout.cardview_currency, this, true); ((LineChart) findViewById(R.id.LineChartView)).setNoDataTextColor(currency.getChartColor()); @@ -198,6 +204,11 @@ public class CurrencyCardview extends CardView { updateColor(currency); } + public Currency getCurrency() + { + return currency; + } + private void setupCardView() { ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.MATCH_PARENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT); diff --git a/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java b/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java index 8c98055..08cbf7e 100644 --- a/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java +++ b/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java @@ -99,11 +99,33 @@ public class DatabaseManager extends SQLiteOpenHelper{ values.put(KEY_WATCHLIST_SYMBOL, currency.getSymbol()); values.put(KEY_WATCHLIST_NAME, currency.getName()); + values.put(KEY_WATCHLIST_POSITION, getWatchlistRowCount(db)); db.insert(TABLE_WATCHLIST, null, values); db.close(); } + public void updateWatchlistPosition(String symbol, int position) + { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues cv = new ContentValues(); + + cv.put(KEY_WATCHLIST_POSITION, position); + + db.update(TABLE_WATCHLIST, cv, KEY_WATCHLIST_SYMBOL + "='" + symbol + "'", null); + + } + + private int getWatchlistRowCount(SQLiteDatabase db) + { + String countQuerry = "SELECT COUNT() FROM " + TABLE_WATCHLIST; + Cursor result = db.rawQuery(countQuerry, null); + + result.moveToFirst(); + + return result.getInt(0); + } + public int deleteCurrencyFromWatchlist(String symbol) { SQLiteDatabase db = this.getWritableDatabase(); @@ -113,15 +135,15 @@ public class DatabaseManager extends SQLiteOpenHelper{ public List getAllCurrenciesFromWatchlist() { - String searchQuerry = "SELECT * FROM " + TABLE_WATCHLIST; + String searchQuerry = "SELECT * FROM " + TABLE_WATCHLIST + " ORDER BY " + KEY_WATCHLIST_POSITION + " ASC"; SQLiteDatabase db = this.getWritableDatabase(); - Cursor resultatList = db.rawQuery(searchQuerry, null); + Cursor resultList = db.rawQuery(searchQuerry, null); List currencyList = new ArrayList<>(); - while(resultatList.moveToNext()) + while(resultList.moveToNext()) { - currencyList.add(new Currency(resultatList.getString(2), resultatList.getString(1))); + currencyList.add(new Currency(resultList.getString(2), resultList.getString(1))); } return currencyList; diff --git a/app/src/main/java/com/herbron/moodl/MoodlBox.java b/app/src/main/java/com/herbron/moodl/MoodlBox.java index f197405..ea553d4 100644 --- a/app/src/main/java/com/herbron/moodl/MoodlBox.java +++ b/app/src/main/java/com/herbron/moodl/MoodlBox.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.drawable.Drawable; import android.os.Build; import android.support.v7.widget.CardView; import android.util.Log; @@ -257,6 +258,22 @@ public class MoodlBox { return color; } + public static Drawable getDrawable(int id, Context context) + { + Drawable drawable; + + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) + { + drawable = context.getDrawable(id); + } + else + { + drawable = context.getResources().getDrawable(id); + } + + return drawable; + } + public static String getIconUrl(String symbol, CurrencyDetailsList currencyDetailsList) { return getIconUrl(symbol, 50, currencyDetailsList); diff --git a/app/src/main/res/drawable/ic_check_white_24dp.xml b/app/src/main/res/drawable/ic_check_white_24dp.xml new file mode 100644 index 0000000..6541ee3 --- /dev/null +++ b/app/src/main/res/drawable/ic_check_white_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_drag_handle_24dp.xml b/app/src/main/res/drawable/ic_drag_handle_24dp.xml new file mode 100644 index 0000000..53afdca --- /dev/null +++ b/app/src/main/res/drawable/ic_drag_handle_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 03bbb50..f32435a 100644 --- a/app/src/main/res/layout/cardview_watchlist.xml +++ b/app/src/main/res/layout/cardview_watchlist.xml @@ -13,6 +13,25 @@ android:orientation="horizontal" android:baselineAligned="false"> + + + + + + - + - - - +