Merge branch 'feature/recyclerview' into dev

pull/145/head
Alinson S. Xavier 9 years ago
commit 94a48133ec

@ -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);
@ -141,6 +141,6 @@ public class ListHabitsRootView extends BaseRootView
private void updateEmptyView()
{
llEmpty.setVisibility(
listAdapter.getCount() > 0 ? View.GONE : View.VISIBLE);
listAdapter.getItemCount() > 0 ? View.GONE : View.VISIBLE);
}
}

@ -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();

@ -20,8 +20,8 @@
package org.isoron.uhabits.ui.habits.list.model;
import android.support.annotation.*;
import android.support.v7.widget.*;
import android.view.*;
import android.widget.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
@ -35,7 +35,8 @@ import java.util.*;
* The data if fetched and cached by a {@link HabitCardListCache}. This adapter
* also holds a list of items that have been selected.
*/
public class HabitCardListAdapter extends BaseAdapter
public class HabitCardListAdapter
extends RecyclerView.Adapter<HabitCardViewHolder>
implements HabitCardListCache.Listener
{
@NonNull
@ -61,6 +62,8 @@ public class HabitCardListAdapter extends BaseAdapter
cache = new HabitCardListCache(allHabits);
cache.setListener(this);
cache.setCheckmarkCount(checkmarkCount);
setHasStableIds(true);
}
public void cancelRefresh()
@ -78,7 +81,7 @@ public class HabitCardListAdapter extends BaseAdapter
}
@Override
public int getCount()
public int getItemCount()
{
return cache.getHabitCount();
}
@ -90,13 +93,38 @@ public class HabitCardListAdapter extends BaseAdapter
* @return the item at given position
* @throws IndexOutOfBoundsException if position is not valid
*/
@Override
@Deprecated
@NonNull
public Habit getItem(int position)
{
return cache.getHabitByPosition(position);
}
@Override
public HabitCardViewHolder onCreateViewHolder(ViewGroup parent,
int viewType)
{
if(listView == null) return null;
View view = listView.createCardView();
return new HabitCardViewHolder(view);
}
@Override
public void onBindViewHolder(@Nullable HabitCardViewHolder holder, int position)
{
if(holder == null) return;
if(listView == null) return;
Habit habit = cache.getHabitByPosition(position);
int score = cache.getScore(habit.getId());
int checkmarks[] = cache.getCheckmarks(habit.getId());
boolean selected = this.selected.contains(habit);
HabitCardView cardView = (HabitCardView) holder.itemView;
listView.bindCardView(cardView, habit, score, checkmarks, selected,
position);
}
@Override
public long getItemId(int position)
{
@ -115,21 +143,21 @@ public class HabitCardListAdapter extends BaseAdapter
return new LinkedList<>(selected);
}
@Override
public View getView(int position,
@Nullable View view,
@Nullable ViewGroup parent)
{
if (listView == null) return null;
Habit habit = cache.getHabitByPosition(position);
int score = cache.getScore(habit.getId());
int checkmarks[] = cache.getCheckmarks(habit.getId());
boolean selected = this.selected.contains(habit);
return listView.buildCardView((HabitCardView) view, habit, score,
checkmarks, selected);
}
// @Override
// public View getView(int position,
// @Nullable View view,
// @Nullable ViewGroup parent)
// {
// if (listView == null) return null;
//
// Habit habit = cache.getHabitByPosition(position);
// int score = cache.getScore(habit.getId());
// int checkmarks[] = cache.getCheckmarks(habit.getId());
// boolean selected = this.selected.contains(habit);
//
// return listView.buildCardView((HabitCardView) view, habit, score,
// checkmarks, selected);
// }
/**
* Returns whether list of selected items is empty.
@ -203,12 +231,6 @@ public class HabitCardListAdapter extends BaseAdapter
this.listView = listView;
}
public void setShowArchived(boolean showArchived)
{
cache.setIncludeArchived(showArchived);
cache.refreshAllHabits(true);
}
/**
* Selects or deselects the item at a given position.
*

@ -102,16 +102,6 @@ public class HabitCardListCache implements CommandRunner.Listener
return data.habits.size();
}
public boolean getIncludeArchived()
{
return includeArchived;
}
public void setIncludeArchived(boolean includeArchived)
{
this.includeArchived = includeArchived;
}
@Nullable
public Long getLastLoadTimestamp()
{

@ -0,0 +1,31 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.ui.habits.list.model;
import android.support.v7.widget.*;
import android.view.*;
public class HabitCardViewHolder extends RecyclerView.ViewHolder
{
public HabitCardViewHolder(View itemView)
{
super(itemView);
}
}

@ -19,22 +19,18 @@
package org.isoron.uhabits.ui.habits.list.views;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListAdapter;
import com.mobeta.android.dslv.DragSortController;
import com.mobeta.android.dslv.DragSortListView;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.ui.habits.list.controllers.CheckmarkButtonController;
import org.isoron.uhabits.ui.habits.list.controllers.HabitCardController;
import org.isoron.uhabits.ui.habits.list.model.HabitCardListAdapter;
public class HabitCardListView extends DragSortListView
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 org.isoron.uhabits.models.*;
import org.isoron.uhabits.ui.habits.list.controllers.*;
import org.isoron.uhabits.ui.habits.list.model.*;
public class HabitCardListView extends RecyclerView
{
@Nullable
private HabitCardListAdapter adapter;
@ -45,9 +41,12 @@ public class HabitCardListView extends DragSortListView
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);
}
/**
@ -61,16 +60,16 @@ public class HabitCardListView extends DragSortListView
* @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 buildCardView(@Nullable HabitCardView cardView,
public View bindCardView(@NonNull HabitCardView cardView,
@NonNull Habit habit,
int score,
int[] checkmarks,
boolean selected)
boolean selected,
int position)
{
if (cardView == null) cardView = new HabitCardView(getContext());
cardView.setHabit(habit);
cardView.setSelected(selected);
cardView.setCheckmarkValues(checkmarks);
@ -82,13 +81,24 @@ public class HabitCardListView extends DragSortListView
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;
}
public View createCardView()
{
return new HabitCardView(getContext());
}
@Override
public void setAdapter(ListAdapter adapter)
public void setAdapter(RecyclerView.Adapter adapter)
{
this.adapter = (HabitCardListAdapter) adapter;
super.setAdapter(adapter);
@ -97,18 +107,6 @@ public class HabitCardListView extends DragSortListView
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
@ -126,33 +124,56 @@ public class HabitCardListView extends DragSortListView
}
public interface Controller extends CheckmarkButtonController.Listener,
HabitCardController.Listener,
DropListener,
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);
}
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);
}
private class ViewManager extends DragSortController
@Override
public boolean isLongPressDragEnabled()
{
public ViewManager()
return true;
}
@Override
public boolean isItemViewSwipeEnabled()
{
super(HabitCardListView.this);
setRemoveEnabled(false);
return false;
}
@Override
public View onCreateFloatView(int position)
public boolean onMove(RecyclerView recyclerView,
ViewHolder from,
ViewHolder to)
{
if (adapter == null) return null;
return adapter.getView(position, null, null);
if (controller == null) return false;
controller.drop(from.getAdapterPosition(), to.getAdapterPosition());
return true;
}
@Override
public void onDestroyFloatView(View floatView)
public void onSwiped(ViewHolder viewHolder, int direction)
{
// NOP
}
}
}

@ -88,6 +88,10 @@ public class HabitCardView extends FrameLayout
checkmarkPanel.setController(null);
if (controller == null) return;
setOnClickListener(v -> {
});
checkmarkPanel.setController(controller);
}

@ -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);

@ -1 +0,0 @@
Subproject commit 54ca667d4cfb0e38d0c9df816360059ac0675afe

@ -1,2 +1 @@
include ':app'
include ':libs:drag-sort-listview:library'

Loading…
Cancel
Save