Skip to content

Commit

Permalink
Fixed reordering and disabled drag while refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-yun committed Jul 18, 2018
1 parent 229a8af commit 2de23b9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
57 changes: 36 additions & 21 deletions app/src/main/java/com/cogentworks/overwidget/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class MainActivity extends AppCompatActivity {

public DragListView mDragListView;
ArrayList<Profile> mItemArray;
OWSwipeRefreshLayout mRefreshLayout;

FloatingActionButton fab;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<Profile>) mDragListView.getAdapter().getItemList());
}
}
}
});
}

public void onFabClick(View view) {
final Context context = this;
final View dialogView = this.getLayoutInflater().inflate(R.layout.configure_dialog, null);
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/java/com/cogentworks/overwidget/UpdateListTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public UpdateListTask(Context context, ArrayList<Profile> profiles) {
@Override
protected ArrayList<Profile> doInBackground(String... params) {
((MainActivity)context).isBusy = true;
((MainActivity)context).disableDrag(true);

if (profiles.size() == 0) {
error = false;
Expand Down Expand Up @@ -54,18 +55,21 @@ protected ArrayList<Profile> doInBackground(String... params) {

@Override
protected void onPostExecute(ArrayList<Profile> 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();
}
}

0 comments on commit 2de23b9

Please sign in to comment.