From 2de23b9a13b15a97bc4311c4dd49e17fb6b5d7cf Mon Sep 17 00:00:00 2001 From: Kala30 Date: Wed, 18 Jul 2018 15:07:14 -0700 Subject: [PATCH] Fixed reordering and disabled drag while refreshing --- .../cogentworks/overwidget/MainActivity.java | 57 ++++++++++++------- .../overwidget/UpdateListTask.java | 14 +++-- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/cogentworks/overwidget/MainActivity.java b/app/src/main/java/com/cogentworks/overwidget/MainActivity.java index cbf67bf..927e193 100644 --- a/app/src/main/java/com/cogentworks/overwidget/MainActivity.java +++ b/app/src/main/java/com/cogentworks/overwidget/MainActivity.java @@ -42,6 +42,7 @@ public class MainActivity extends AppCompatActivity { public DragListView mDragListView; ArrayList mItemArray; + OWSwipeRefreshLayout mRefreshLayout; FloatingActionButton fab; @@ -86,28 +87,11 @@ public void onRefresh() { dbHelper = new SQLHelper(this); mItemArray = dbHelper.getList(); - final OWSwipeRefreshLayout mRefreshLayout = mSwipeRefreshLayout; + mRefreshLayout = mSwipeRefreshLayout; mDragListView = (DragListView) findViewById(R.id.list); mDragListView.getRecyclerView().setVerticalScrollBarEnabled(true); - mDragListView.setDragListListener(new DragListView.DragListListenerAdapter() { - @Override - public void onItemDragStarted(int position) { - mRefreshLayout.setEnabled(false); - //Toast.makeText(mDragListView.getContext(), "Start - position: " + position, Toast.LENGTH_SHORT).show(); - } - - @Override - public void onItemDragEnded(int fromPosition, int toPosition) { - if (!isBusy) { - mRefreshLayout.setEnabled(true); - if (fromPosition != toPosition) { - dbHelper.setList(mItemArray); - //Toast.makeText(mDragListView.getContext(), "End - position: " + toPosition, Toast.LENGTH_SHORT).show(); - } - } - } - }); + setDragListener(); mRefreshLayout.setScrollingView(mDragListView.getRecyclerView()); if (useDarkTheme) @@ -157,11 +141,9 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) { setupListRecyclerView(); - isBusy = true; mSwipeRefreshLayout.setRefreshing(true); UpdateListTask updateListTask = new UpdateListTask(this, mItemArray); updateListTask.execute(); - } private void setupListRecyclerView() { @@ -192,6 +174,39 @@ private void setupListRecyclerView() { isBusy = false; }*/ + public void disableDrag(final boolean disable) { + mDragListView.setDragListCallback(new DragListView.DragListCallbackAdapter() { + @Override + public boolean canDragItemAtPosition(int dragPosition) { + return !disable; + } + + @Override + public boolean canDropItemAtPosition(int dropPosition) { + return !disable; + } + }); + } + + public void setDragListener() { + mDragListView.setDragListListener(new DragListView.DragListListenerAdapter() { + @Override + public void onItemDragStarted(int position) { + mRefreshLayout.setEnabled(false); + } + + @Override + public void onItemDragEnded(int fromPosition, int toPosition) { + if (!isBusy) { + mRefreshLayout.setEnabled(true); + if (fromPosition != toPosition) { + dbHelper.setList((ArrayList) mDragListView.getAdapter().getItemList()); + } + } + } + }); + } + public void onFabClick(View view) { final Context context = this; final View dialogView = this.getLayoutInflater().inflate(R.layout.configure_dialog, null); diff --git a/app/src/main/java/com/cogentworks/overwidget/UpdateListTask.java b/app/src/main/java/com/cogentworks/overwidget/UpdateListTask.java index 3fc2bea..f0092bc 100644 --- a/app/src/main/java/com/cogentworks/overwidget/UpdateListTask.java +++ b/app/src/main/java/com/cogentworks/overwidget/UpdateListTask.java @@ -24,6 +24,7 @@ public UpdateListTask(Context context, ArrayList profiles) { @Override protected ArrayList doInBackground(String... params) { ((MainActivity)context).isBusy = true; + ((MainActivity)context).disableDrag(true); if (profiles.size() == 0) { error = false; @@ -54,18 +55,21 @@ protected ArrayList doInBackground(String... params) { @Override protected void onPostExecute(ArrayList result) { - if (result != null) { - MainActivity activity = (MainActivity) context; + MainActivity activity = (MainActivity) context; + if (result != null) { activity.dbHelper.setList(result); activity.mDragListView.getAdapter().setItemList(result); + } else { if (error) - Snackbar.make(((Activity) context).findViewById(R.id.layout_main), "An update error occurred", Snackbar.LENGTH_LONG).show(); + Snackbar.make(activity.findViewById(R.id.layout_main), "An update error occurred", Snackbar.LENGTH_LONG).show(); } - ((MainActivity)context).isBusy = false; - ((SwipeRefreshLayout)((MainActivity)context).findViewById(R.id.swiperefresh)).setRefreshing(false); + activity.isBusy = false; + ((SwipeRefreshLayout)activity.findViewById(R.id.swiperefresh)).setRefreshing(false); + activity.disableDrag(false); + activity.setDragListener(); } }