mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Restore drag & drop and click; remove DragSortListView library
This commit is contained in:
@@ -69,8 +69,6 @@ dependencies {
|
||||
androidTestApt 'com.google.dagger:dagger-compiler:2.2'
|
||||
provided 'javax.annotation:jsr250-api:1.0'
|
||||
|
||||
compile project(':libs:drag-sort-listview:library')
|
||||
|
||||
testCompile 'junit:junit:4.12'
|
||||
testCompile 'org.hamcrest:hamcrest-library:1.3'
|
||||
testCompile 'org.mockito:mockito-core:1.10.19'
|
||||
|
||||
@@ -22,6 +22,7 @@ package org.isoron.uhabits.models.sqlite;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import com.activeandroid.query.*;
|
||||
import com.activeandroid.util.*;
|
||||
|
||||
import org.apache.commons.lang3.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
@@ -181,8 +182,16 @@ public class SQLiteHabitList extends HabitList
|
||||
{
|
||||
if (from == to) return;
|
||||
|
||||
Integer toPos = indexOf(to);
|
||||
Integer fromPos = indexOf(from);
|
||||
HabitRecord fromRecord = HabitRecord.get(from.getId());
|
||||
HabitRecord toRecord = HabitRecord.get(to.getId());
|
||||
|
||||
if (fromRecord == null) throw new RuntimeException("habit not in database");
|
||||
if (toRecord == null) throw new RuntimeException("habit not in database");
|
||||
|
||||
Integer fromPos = fromRecord.position;
|
||||
Integer toPos = toRecord.position;
|
||||
|
||||
Log.d("SQLiteHabitList", String.format("reorder: %d %d", fromPos, toPos));
|
||||
|
||||
if (toPos < fromPos)
|
||||
{
|
||||
@@ -199,10 +208,8 @@ public class SQLiteHabitList extends HabitList
|
||||
.execute();
|
||||
}
|
||||
|
||||
HabitRecord record = HabitRecord.get(from.getId());
|
||||
if (record == null) throw new RuntimeException("habit not in database");
|
||||
record.position = toPos;
|
||||
record.save();
|
||||
fromRecord.position = toPos;
|
||||
fromRecord.save();
|
||||
|
||||
update(from);
|
||||
|
||||
@@ -212,10 +219,6 @@ public class SQLiteHabitList extends HabitList
|
||||
@Override
|
||||
public int size()
|
||||
{
|
||||
// SQLiteDatabase db = Cache.openDatabase();
|
||||
// SQLiteStatement st = db.compileStatement(buildCountQuery());
|
||||
// return (int) st.simpleQueryForLong();
|
||||
|
||||
return toList().size();
|
||||
}
|
||||
|
||||
@@ -251,11 +254,6 @@ public class SQLiteHabitList extends HabitList
|
||||
return habits;
|
||||
}
|
||||
|
||||
private void appendCount(StringBuilder query)
|
||||
{
|
||||
query.append("select count (*) from habits ");
|
||||
}
|
||||
|
||||
private void appendOrderBy(StringBuilder query)
|
||||
{
|
||||
query.append("order by position ");
|
||||
@@ -278,14 +276,6 @@ public class SQLiteHabitList extends HabitList
|
||||
query.append(" ");
|
||||
}
|
||||
|
||||
private String buildCountQuery()
|
||||
{
|
||||
StringBuilder query = new StringBuilder();
|
||||
appendCount(query);
|
||||
appendWhere(query);
|
||||
return query.toString();
|
||||
}
|
||||
|
||||
private String buildSelectQuery()
|
||||
{
|
||||
StringBuilder query = new StringBuilder();
|
||||
|
||||
@@ -98,7 +98,7 @@ public class ListHabitsController
|
||||
@Override
|
||||
public void onHabitReorder(@NonNull Habit from, @NonNull Habit to)
|
||||
{
|
||||
habitList.reorder(from, to);
|
||||
new SimpleTask(() -> habitList.reorder(from, to)).execute();
|
||||
}
|
||||
|
||||
public void onImportData(@NonNull File file)
|
||||
|
||||
@@ -115,7 +115,7 @@ public class ListHabitsRootView extends BaseRootView
|
||||
@NonNull ListHabitsSelectionMenu menu)
|
||||
{
|
||||
HabitCardListController listController =
|
||||
new HabitCardListController(listAdapter, listView);
|
||||
new HabitCardListController(listAdapter);
|
||||
|
||||
listController.setHabitListener(controller);
|
||||
listController.setSelectionListener(menu);
|
||||
|
||||
@@ -21,8 +21,6 @@ package org.isoron.uhabits.ui.habits.list.controllers;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import com.mobeta.android.dslv.*;
|
||||
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.ui.habits.list.model.*;
|
||||
import org.isoron.uhabits.ui.habits.list.views.*;
|
||||
@@ -32,9 +30,7 @@ import org.isoron.uhabits.ui.habits.list.views.*;
|
||||
* HabitListView. These include selecting and reordering items, toggling
|
||||
* checkmarks and clicking habits.
|
||||
*/
|
||||
public class HabitCardListController implements DragSortListView.DropListener,
|
||||
DragSortListView.DragListener,
|
||||
HabitCardListView.Controller
|
||||
public class HabitCardListController implements HabitCardListView.Controller
|
||||
{
|
||||
private final Mode NORMAL_MODE = new NormalMode();
|
||||
|
||||
@@ -43,9 +39,6 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
||||
@NonNull
|
||||
private final HabitCardListAdapter adapter;
|
||||
|
||||
@NonNull
|
||||
private final HabitCardListView view;
|
||||
|
||||
@Nullable
|
||||
private HabitListener habitListener;
|
||||
|
||||
@@ -55,11 +48,9 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
||||
@NonNull
|
||||
private Mode activeMode;
|
||||
|
||||
public HabitCardListController(@NonNull HabitCardListAdapter adapter,
|
||||
@NonNull HabitCardListView view)
|
||||
public HabitCardListController(@NonNull HabitCardListAdapter adapter)
|
||||
{
|
||||
this.adapter = adapter;
|
||||
this.view = view;
|
||||
this.activeMode = new NormalMode();
|
||||
}
|
||||
|
||||
@@ -190,7 +181,6 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
||||
private void cancelSelection()
|
||||
{
|
||||
adapter.clearSelection();
|
||||
// view.setDragEnabled(true);
|
||||
activeMode = new NormalMode();
|
||||
|
||||
if (selectionListener != null) selectionListener.onSelectionFinish();
|
||||
|
||||
@@ -121,7 +121,8 @@ public class HabitCardListAdapter
|
||||
boolean selected = this.selected.contains(habit);
|
||||
|
||||
HabitCardView cardView = (HabitCardView) holder.itemView;
|
||||
listView.bindCardView(cardView, habit, score, checkmarks, selected);
|
||||
listView.bindCardView(cardView, habit, score, checkmarks, selected,
|
||||
position);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -22,11 +22,10 @@ package org.isoron.uhabits.ui.habits.list.views;
|
||||
import android.content.*;
|
||||
import android.support.annotation.*;
|
||||
import android.support.v7.widget.*;
|
||||
import android.support.v7.widget.helper.*;
|
||||
import android.util.*;
|
||||
import android.view.*;
|
||||
|
||||
import com.mobeta.android.dslv.*;
|
||||
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.ui.habits.list.controllers.*;
|
||||
import org.isoron.uhabits.ui.habits.list.model.*;
|
||||
@@ -42,11 +41,12 @@ public class HabitCardListView extends RecyclerView
|
||||
public HabitCardListView(Context context, AttributeSet attrs)
|
||||
{
|
||||
super(context, attrs);
|
||||
// setFloatViewManager(new ViewManager());
|
||||
// setDragEnabled(true);
|
||||
setLongClickable(true);
|
||||
setHasFixedSize(true);
|
||||
setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
|
||||
TouchHelperCallback callback = new TouchHelperCallback();
|
||||
new ItemTouchHelper(callback).attachToRecyclerView(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,13 +60,15 @@ public class HabitCardListView extends RecyclerView
|
||||
* @param checkmarks the list of checkmark values to be included in the
|
||||
* card
|
||||
* @param selected true if the card is selected, false otherwise
|
||||
* @param position
|
||||
* @return the HabitCardView generated
|
||||
*/
|
||||
public View bindCardView(@NonNull HabitCardView cardView,
|
||||
@NonNull Habit habit,
|
||||
int score,
|
||||
int[] checkmarks,
|
||||
boolean selected)
|
||||
boolean selected,
|
||||
int position)
|
||||
{
|
||||
cardView.setHabit(habit);
|
||||
cardView.setSelected(selected);
|
||||
@@ -79,6 +81,12 @@ public class HabitCardListView extends RecyclerView
|
||||
cardController.setListener(controller);
|
||||
cardView.setController(cardController);
|
||||
cardController.setView(cardView);
|
||||
|
||||
cardView.setOnClickListener(v -> controller.onItemClick(position));
|
||||
cardView.setOnLongClickListener(v -> {
|
||||
controller.onItemLongClick(position);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
return cardView;
|
||||
@@ -99,18 +107,6 @@ public class HabitCardListView extends RecyclerView
|
||||
public void setController(@Nullable Controller controller)
|
||||
{
|
||||
this.controller = controller;
|
||||
// setDropListener(controller);
|
||||
// setDragListener(controller);
|
||||
// setOnItemClickListener(null);
|
||||
setOnLongClickListener(null);
|
||||
|
||||
if (controller == null) return;
|
||||
|
||||
// setOnItemClickListener((p, v, pos, id) -> controller.onItemClick(pos));
|
||||
// setOnItemLongClickListener((p, v, pos, id) -> {
|
||||
// controller.onItemLongClick(pos);
|
||||
// return true;
|
||||
// });
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -128,33 +124,56 @@ public class HabitCardListView extends RecyclerView
|
||||
}
|
||||
|
||||
public interface Controller extends CheckmarkButtonController.Listener,
|
||||
HabitCardController.Listener,
|
||||
DragSortListView.DropListener,
|
||||
DragSortListView.DragListener
|
||||
HabitCardController.Listener
|
||||
{
|
||||
void drag(int from, int to);
|
||||
|
||||
void drop(int from, int to);
|
||||
|
||||
void onItemClick(int pos);
|
||||
|
||||
void onItemLongClick(int pos);
|
||||
|
||||
void startDrag(int position);
|
||||
}
|
||||
|
||||
// private class ViewManager extends DragSortController
|
||||
// {
|
||||
// public ViewManager()
|
||||
// {
|
||||
// super(HabitCardListView.this);
|
||||
// setRemoveEnabled(false);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public View onCreateFloatView(int position)
|
||||
// {
|
||||
// if (adapter == null) return null;
|
||||
// return adapter.getView(position, null, null);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onDestroyFloatView(View floatView)
|
||||
// {
|
||||
// }
|
||||
// }
|
||||
class TouchHelperCallback extends ItemTouchHelper.Callback
|
||||
{
|
||||
@Override
|
||||
public int getMovementFlags(RecyclerView recyclerView,
|
||||
ViewHolder viewHolder)
|
||||
{
|
||||
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
|
||||
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
|
||||
return makeMovementFlags(dragFlags, swipeFlags);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLongPressDragEnabled()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemViewSwipeEnabled()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMove(RecyclerView recyclerView,
|
||||
ViewHolder from,
|
||||
ViewHolder to)
|
||||
{
|
||||
if (controller == null) return false;
|
||||
controller.drop(from.getAdapterPosition(), to.getAdapterPosition());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwiped(ViewHolder viewHolder, int direction)
|
||||
{
|
||||
// NOP
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,9 +45,6 @@
|
||||
android:divider="?windowBackgroundColor"
|
||||
android:dividerHeight="1dp"
|
||||
android:listSelector="@android:color/transparent"
|
||||
app:drag_start_mode="onLongPress"
|
||||
app:sort_enabled="true"
|
||||
app:track_drag_sort="false"
|
||||
android:layout_below="@id/header"/>
|
||||
|
||||
<LinearLayout
|
||||
|
||||
@@ -66,7 +66,7 @@ public class HabitCardListControllerTest extends BaseUnitTest
|
||||
|
||||
resetMocks();
|
||||
|
||||
this.controller = new HabitCardListController(adapter, view);
|
||||
this.controller = new HabitCardListController(adapter);
|
||||
controller.setHabitListener(habitListener);
|
||||
controller.setSelectionListener(selectionListener);
|
||||
view.setController(controller);
|
||||
|
||||
Reference in New Issue
Block a user