Add reorder option for Watchlist currencies | New edit buttons

This commit is contained in:
Tanguy Herbron 2018-06-04 01:48:53 +02:00
parent 856bce59ac
commit 142b00aff5
9 changed files with 137 additions and 26 deletions

View File

@ -46,6 +46,7 @@ dependencies {
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3' implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
implementation 'com.mattprecious.swirl:swirl:1.1.0' implementation 'com.mattprecious.swirl:swirl:1.1.0'
implementation 'com.wdullaer:materialdatetimepicker:3.5.2' implementation 'com.wdullaer:materialdatetimepicker:3.5.2'
implementation 'com.jmedeisis:draglinearlayout:1.1.0'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

View File

@ -17,6 +17,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import com.herbron.moodl.Activities.CurrencySelectionActivity; 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.CurrencyCardview;
import com.herbron.moodl.DataManagers.CurrencyData.CurrencyDetailsList; import com.herbron.moodl.DataManagers.CurrencyData.CurrencyDetailsList;
import com.herbron.moodl.DataManagers.CurrencyData.CurrencyTickerList; import com.herbron.moodl.DataManagers.CurrencyData.CurrencyTickerList;
import com.herbron.moodl.DataManagers.DatabaseManager;
import com.herbron.moodl.DataManagers.PreferencesManager; import com.herbron.moodl.DataManagers.PreferencesManager;
import com.herbron.moodl.DataManagers.WatchlistManager; import com.herbron.moodl.DataManagers.WatchlistManager;
import com.herbron.moodl.MoodlBox; import com.herbron.moodl.MoodlBox;
import com.herbron.moodl.R; import com.herbron.moodl.R;
import com.jmedeisis.draglinearlayout.DragLinearLayout;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -49,6 +52,7 @@ public class Watchlist extends Fragment {
private int watchlistCounter; private int watchlistCounter;
private CurrencyDetailsList currencyDetailsList; private CurrencyDetailsList currencyDetailsList;
private SwipeRefreshLayout refreshLayout; private SwipeRefreshLayout refreshLayout;
private DragLinearLayout dragLinearLayout;
private long lastTimestamp; private long lastTimestamp;
private PreferencesManager preferencesManager; private PreferencesManager preferencesManager;
private String defaultCurrency; private String defaultCurrency;
@ -56,6 +60,7 @@ public class Watchlist extends Fragment {
private boolean tickerUpdated; private boolean tickerUpdated;
private boolean detailsUpdated; private boolean detailsUpdated;
private boolean editModeEnabled; private boolean editModeEnabled;
private DatabaseManager databaseManager;
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 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); view = inflater.inflate(R.layout.fragment_watchlist_homeactivity, container, false);
refreshLayout = view.findViewById(R.id.swiperefreshwatchlist); refreshLayout = view.findViewById(R.id.swiperefreshwatchlist);
dragLinearLayout = view.findViewById(R.id.linearLayoutWatchlist);
currencyDetailsList = CurrencyDetailsList.getInstance(getContext()); currencyDetailsList = CurrencyDetailsList.getInstance(getContext());
preferencesManager = new PreferencesManager(getContext()); preferencesManager = new PreferencesManager(getContext());
databaseManager = new DatabaseManager(getContext());
lastTimestamp = 0; lastTimestamp = 0;
defaultCurrency = preferencesManager.getDefaultCurrency(); defaultCurrency = preferencesManager.getDefaultCurrency();
@ -72,6 +79,17 @@ public class Watchlist extends Fragment {
tickerUpdated = false; tickerUpdated = false;
updateTickerList(); 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; editModeEnabled = false;
watchlistManager = new WatchlistManager(getContext()); watchlistManager = new WatchlistManager(getContext());
@ -128,35 +146,40 @@ public class Watchlist extends Fragment {
editButton.setOnClickListener(new View.OnClickListener() { editButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
LinearLayout watchlistLayout = Watchlist.this.view.findViewById(R.id.linearLayoutWatchlist);
if(editModeEnabled) if(editModeEnabled)
{ {
((ImageView) view.findViewById(R.id.edit_button)).setBackground(MoodlBox.getDrawable(R.drawable.ic_mode_edit_white_24dp, getContext()));
editModeEnabled = false; 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) if(watchlistElement instanceof CurrencyCardview)
{ {
watchlistElement.setClickable(true); watchlistElement.setClickable(true);
collapseW(watchlistElement.findViewById(R.id.deleteCardWatchlist)); collapseW(watchlistElement.findViewById(R.id.deleteCardWatchlist));
collapseW(watchlistElement.findViewById(R.id.dragCardWatchlist));
} }
} }
} }
else else
{ {
((ImageView) view.findViewById(R.id.edit_button)).setBackground(MoodlBox.getDrawable(R.drawable.ic_check_white_24dp, getContext()));
editModeEnabled = true; 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) if(watchlistElement instanceof CurrencyCardview)
{ {
watchlistElement.setClickable(false); watchlistElement.setClickable(false);
expandW(watchlistElement.findViewById(R.id.deleteCardWatchlist)); 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() { getActivity().runOnUiThread(new Runnable() {
@Override @Override
public void run() { 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()) 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));
} }
} }
}); });

View File

@ -43,6 +43,8 @@ import static com.herbron.moodl.MoodlBox.numberConformer;
public class CurrencyCardview extends CardView { public class CurrencyCardview extends CardView {
private Currency currency;
public CurrencyCardview(@NonNull Context context) { public CurrencyCardview(@NonNull Context context) {
super(context); super(context);
} }
@ -51,6 +53,8 @@ public class CurrencyCardview extends CardView {
{ {
super (context); super (context);
this.currency = currency;
LayoutInflater.from(context).inflate(R.layout.cardview_watchlist, this, true); LayoutInflater.from(context).inflate(R.layout.cardview_watchlist, this, true);
((LineChart) findViewById(R.id.LineChartView)).setNoDataTextColor(currency.getChartColor()); ((LineChart) findViewById(R.id.LineChartView)).setNoDataTextColor(currency.getChartColor());
@ -136,6 +140,8 @@ public class CurrencyCardview extends CardView {
{ {
super(context); super(context);
this.currency = currency;
LayoutInflater.from(context).inflate(R.layout.cardview_currency, this, true); LayoutInflater.from(context).inflate(R.layout.cardview_currency, this, true);
((LineChart) findViewById(R.id.LineChartView)).setNoDataTextColor(currency.getChartColor()); ((LineChart) findViewById(R.id.LineChartView)).setNoDataTextColor(currency.getChartColor());
@ -198,6 +204,11 @@ public class CurrencyCardview extends CardView {
updateColor(currency); updateColor(currency);
} }
public Currency getCurrency()
{
return currency;
}
private void setupCardView() private void setupCardView()
{ {
ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.MATCH_PARENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT); ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.MATCH_PARENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT);

View File

@ -99,11 +99,33 @@ public class DatabaseManager extends SQLiteOpenHelper{
values.put(KEY_WATCHLIST_SYMBOL, currency.getSymbol()); values.put(KEY_WATCHLIST_SYMBOL, currency.getSymbol());
values.put(KEY_WATCHLIST_NAME, currency.getName()); values.put(KEY_WATCHLIST_NAME, currency.getName());
values.put(KEY_WATCHLIST_POSITION, getWatchlistRowCount(db));
db.insert(TABLE_WATCHLIST, null, values); db.insert(TABLE_WATCHLIST, null, values);
db.close(); 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) public int deleteCurrencyFromWatchlist(String symbol)
{ {
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
@ -113,15 +135,15 @@ public class DatabaseManager extends SQLiteOpenHelper{
public List<Currency> getAllCurrenciesFromWatchlist() public List<Currency> getAllCurrenciesFromWatchlist()
{ {
String searchQuerry = "SELECT * FROM " + TABLE_WATCHLIST; String searchQuerry = "SELECT * FROM " + TABLE_WATCHLIST + " ORDER BY " + KEY_WATCHLIST_POSITION + " ASC";
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
Cursor resultatList = db.rawQuery(searchQuerry, null); Cursor resultList = db.rawQuery(searchQuerry, null);
List<Currency> currencyList = new ArrayList<>(); List<Currency> 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; return currencyList;

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
import android.support.v7.widget.CardView; import android.support.v7.widget.CardView;
import android.util.Log; import android.util.Log;
@ -257,6 +258,22 @@ public class MoodlBox {
return color; 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) public static String getIconUrl(String symbol, CurrencyDetailsList currencyDetailsList)
{ {
return getIconUrl(symbol, 50, currencyDetailsList); return getIconUrl(symbol, 50, currencyDetailsList);

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#66999999"
android:pathData="M20,9H4v2h16V9zM4,15h16v-2H4v2z"/>
</vector>

View File

@ -13,6 +13,25 @@
android:orientation="horizontal" android:orientation="horizontal"
android:baselineAligned="false"> android:baselineAligned="false">
<LinearLayout
android:id="@+id/dragCardWatchlist"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="start"
android:visibility="gone"
android:layout_weight="0.08"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_drag_handle_24dp"
android:clickable="false"
android:focusable="false"/>
</LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/currencyInfoLayout" android:id="@+id/currencyInfoLayout"
android:layout_width="0dp" android:layout_width="0dp"

View File

@ -78,11 +78,10 @@
android:orientation="vertical" android:orientation="vertical"
android:paddingTop="25dp"> android:paddingTop="25dp">
<LinearLayout android:id="@+id/linearLayoutWatchlist" <com.jmedeisis.draglinearlayout.DragLinearLayout android:id="@+id/linearLayoutWatchlist"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"/>
xmlns:android="http://schemas.android.com/apk/res/android">
<ProgressBar <ProgressBar
android:id="@+id/progressBarWatchlist" android:id="@+id/progressBarWatchlist"
@ -93,8 +92,6 @@
android:background="@drawable/circular_progress_bar" android:background="@drawable/circular_progress_bar"
android:visibility="visible"/> android:visibility="visible"/>
</LinearLayout>
<Button <Button
android:id="@+id/buttonAddWatchlist" android:id="@+id/buttonAddWatchlist"
android:text="@string/add_watchlist" android:text="@string/add_watchlist"