#387 swipe to favorite

This commit is contained in:
Niedermann IT-Dienstleistungen 2018-06-17 21:01:46 +02:00 committed by GitHub
commit 7ddda65dd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 39 deletions

View file

@ -390,13 +390,14 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
*/ */
@Override @Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
if (direction == ItemTouchHelper.LEFT || direction == ItemTouchHelper.RIGHT) { switch(direction) {
case ItemTouchHelper.LEFT: {
final DBNote dbNote = (DBNote) adapter.getItem(viewHolder.getAdapterPosition()); final DBNote dbNote = (DBNote) adapter.getItem(viewHolder.getAdapterPosition());
db.deleteNoteAndSync((dbNote).getId()); db.deleteNoteAndSync((dbNote).getId());
adapter.remove(dbNote); adapter.remove(dbNote);
refreshLists(); refreshLists();
Log.v("Note", "Item deleted through swipe ----------------------------------------------"); Log.v("Note", "Item deleted through swipe ----------------------------------------------");
Snackbar.make(swipeRefreshLayout, R.string.action_note_deleted, 7 * 1000) Snackbar.make(swipeRefreshLayout, R.string.action_note_deleted, Snackbar.LENGTH_LONG)
.setAction(R.string.action_undo, new View.OnClickListener() { .setAction(R.string.action_undo, new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -407,14 +408,22 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
} }
}) })
.show(); .show();
break;
}
case ItemTouchHelper.RIGHT: {
final DBNote dbNote = (DBNote) adapter.getItem(viewHolder.getAdapterPosition());
db.toggleFavorite(dbNote, null);
refreshLists();
break;
}
} }
} }
@Override @Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
ItemAdapter.NoteViewHolder noteViewHolder = (ItemAdapter.NoteViewHolder) viewHolder; ItemAdapter.NoteViewHolder noteViewHolder = (ItemAdapter.NoteViewHolder) viewHolder;
// show delete icon on the right side // show swipe icon on the side
noteViewHolder.showSwipeDelete(dX > 0); noteViewHolder.showSwipe(dX>0);
// move only swipeable part of item (not leave-behind) // move only swipeable part of item (not leave-behind)
getDefaultUIUtil().onDraw(c, recyclerView, noteViewHolder.noteSwipeable, dX, dY, actionState, isCurrentlyActive); getDefaultUIUtil().onDraw(c, recyclerView, noteViewHolder.noteSwipeable, dX, dY, actionState, isCurrentlyActive);
} }

View file

@ -172,11 +172,8 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener { public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener {
@BindView(R.id.noteSwipeable) @BindView(R.id.noteSwipeable)
public View noteSwipeable; public View noteSwipeable;
@BindView(R.id.noteDeleteLeft) View noteSwipeFrame;
ImageView noteDeleteLeft; ImageView noteFavoriteLeft, noteDeleteRight;
@BindView(R.id.noteDeleteRight)
ImageView noteDeleteRight;
@BindView(R.id.noteTitle)
TextView noteTitle; TextView noteTitle;
@BindView(R.id.noteCategory) @BindView(R.id.noteCategory)
TextView noteCategory; TextView noteCategory;
@ -187,11 +184,19 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@BindView(R.id.noteFavorite) @BindView(R.id.noteFavorite)
ImageView noteFavorite; ImageView noteFavorite;
private NoteViewHolder(View view) { private NoteViewHolder(View v) {
super(view); super(v);
ButterKnife.bind(this, view); this.noteSwipeFrame = v.findViewById(R.id.noteSwipeFrame);
view.setOnClickListener(this); this.noteSwipeable = v.findViewById(R.id.noteSwipeable);
view.setOnLongClickListener(this); this.noteFavoriteLeft = v.findViewById(R.id.noteFavoriteLeft);
this.noteDeleteRight = v.findViewById(R.id.noteDeleteRight);
this.noteTitle = v.findViewById(R.id.noteTitle);
this.noteCategory = v.findViewById(R.id.noteCategory);
this.noteExcerpt = v.findViewById(R.id.noteExcerpt);
this.noteStatus = v.findViewById(R.id.noteStatus);
this.noteFavorite = v.findViewById(R.id.noteFavorite);
v.setOnClickListener(this);
v.setOnLongClickListener(this);
} }
@Override @Override
@ -204,9 +209,10 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
return noteClickListener.onNoteLongClick(getAdapterPosition(), v); return noteClickListener.onNoteLongClick(getAdapterPosition(), v);
} }
public void showSwipeDelete(boolean left) { public void showSwipe(boolean left) {
noteDeleteLeft.setVisibility(left ? View.VISIBLE : View.INVISIBLE); noteFavoriteLeft.setVisibility(left ? View.VISIBLE : View.INVISIBLE);
noteDeleteRight.setVisibility(left ? View.INVISIBLE : View.VISIBLE); noteDeleteRight.setVisibility(left ? View.INVISIBLE : View.VISIBLE);
noteSwipeFrame.setBackgroundResource(left ? R.color.bg_warning : R.color.bg_attention);
} }
} }

View file

@ -1,24 +1,25 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/noteSwipeFrame"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@color/bg_attention"> android:background="@color/bg_attention">
<ImageView <ImageView
android:id="@+id/noteDeleteLeft" android:id="@+id/noteFavoriteLeft"
android:layout_width="wrap_content" android:layout_width="32dp"
android:layout_height="wrap_content" android:layout_height="32dp"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/button_padding" android:layout_marginLeft="@dimen/button_padding"
android:layout_marginStart="@dimen/button_padding" android:layout_marginStart="@dimen/button_padding"
android:contentDescription="@string/menu_delete" android:contentDescription="@string/menu_delete"
android:src="@drawable/ic_delete_white_32dp" /> android:src="@drawable/ic_star_white_24dp" />
<ImageView <ImageView
android:id="@+id/noteDeleteRight" android:id="@+id/noteDeleteRight"
android:layout_width="wrap_content" android:layout_width="32dp"
android:layout_height="wrap_content" android:layout_height="32dp"
android:layout_gravity="end|center_vertical" android:layout_gravity="right|end|center_vertical"
android:layout_marginEnd="@dimen/button_padding" android:layout_marginEnd="@dimen/button_padding"
android:layout_marginRight="@dimen/button_padding" android:layout_marginRight="@dimen/button_padding"
android:contentDescription="@string/menu_delete" android:contentDescription="@string/menu_delete"

View file

@ -10,7 +10,8 @@
<color name="bg_transparent">#aa000000</color> <color name="bg_transparent">#aa000000</color>
<color name="bg_highlighted">#f3f3f3</color> <color name="bg_highlighted">#f3f3f3</color>
<color name="bg_normal">#ffffff</color> <color name="bg_normal">#ffffff</color>
<color name="bg_attention">#662020</color> <color name="bg_attention">#d40000</color>
<color name="bg_warning">#ffcc00</color>
<color name="fg_default">#000000</color> <color name="fg_default">#000000</color>
<color name="fg_default_selection">#333333</color> <color name="fg_default_selection">#333333</color>
<color name="fg_default_low">#666666</color> <color name="fg_default_low">#666666</color>