#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,31 +390,40 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
*/
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
if (direction == ItemTouchHelper.LEFT || direction == ItemTouchHelper.RIGHT) {
final DBNote dbNote = (DBNote) adapter.getItem(viewHolder.getAdapterPosition());
db.deleteNoteAndSync((dbNote).getId());
adapter.remove(dbNote);
refreshLists();
Log.v("Note", "Item deleted through swipe ----------------------------------------------");
Snackbar.make(swipeRefreshLayout, R.string.action_note_deleted, 7 * 1000)
.setAction(R.string.action_undo, new View.OnClickListener() {
@Override
public void onClick(View v) {
db.addNoteAndSync(dbNote);
refreshLists();
Snackbar.make(swipeRefreshLayout, R.string.action_note_restored, Snackbar.LENGTH_SHORT)
.show();
}
})
.show();
switch(direction) {
case ItemTouchHelper.LEFT: {
final DBNote dbNote = (DBNote) adapter.getItem(viewHolder.getAdapterPosition());
db.deleteNoteAndSync((dbNote).getId());
adapter.remove(dbNote);
refreshLists();
Log.v("Note", "Item deleted through swipe ----------------------------------------------");
Snackbar.make(swipeRefreshLayout, R.string.action_note_deleted, Snackbar.LENGTH_LONG)
.setAction(R.string.action_undo, new View.OnClickListener() {
@Override
public void onClick(View v) {
db.addNoteAndSync(dbNote);
refreshLists();
Snackbar.make(swipeRefreshLayout, R.string.action_note_restored, Snackbar.LENGTH_SHORT)
.show();
}
})
.show();
break;
}
case ItemTouchHelper.RIGHT: {
final DBNote dbNote = (DBNote) adapter.getItem(viewHolder.getAdapterPosition());
db.toggleFavorite(dbNote, null);
refreshLists();
break;
}
}
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
ItemAdapter.NoteViewHolder noteViewHolder = (ItemAdapter.NoteViewHolder) viewHolder;
// show delete icon on the right side
noteViewHolder.showSwipeDelete(dX > 0);
// show swipe icon on the side
noteViewHolder.showSwipe(dX>0);
// move only swipeable part of item (not leave-behind)
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 {
@BindView(R.id.noteSwipeable)
public View noteSwipeable;
@BindView(R.id.noteDeleteLeft)
ImageView noteDeleteLeft;
@BindView(R.id.noteDeleteRight)
ImageView noteDeleteRight;
@BindView(R.id.noteTitle)
View noteSwipeFrame;
ImageView noteFavoriteLeft, noteDeleteRight;
TextView noteTitle;
@BindView(R.id.noteCategory)
TextView noteCategory;
@ -187,11 +184,19 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@BindView(R.id.noteFavorite)
ImageView noteFavorite;
private NoteViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
view.setOnClickListener(this);
view.setOnLongClickListener(this);
private NoteViewHolder(View v) {
super(v);
this.noteSwipeFrame = v.findViewById(R.id.noteSwipeFrame);
this.noteSwipeable = v.findViewById(R.id.noteSwipeable);
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
@ -204,9 +209,10 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
return noteClickListener.onNoteLongClick(getAdapterPosition(), v);
}
public void showSwipeDelete(boolean left) {
noteDeleteLeft.setVisibility(left ? View.VISIBLE : View.INVISIBLE);
public void showSwipe(boolean left) {
noteFavoriteLeft.setVisibility(left ? View.VISIBLE : View.INVISIBLE);
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"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/noteSwipeFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_attention">
<ImageView
android:id="@+id/noteDeleteLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/noteFavoriteLeft"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/button_padding"
android:layout_marginStart="@dimen/button_padding"
android:contentDescription="@string/menu_delete"
android:src="@drawable/ic_delete_white_32dp" />
android:src="@drawable/ic_star_white_24dp" />
<ImageView
android:id="@+id/noteDeleteRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="right|end|center_vertical"
android:layout_marginEnd="@dimen/button_padding"
android:layout_marginRight="@dimen/button_padding"
android:contentDescription="@string/menu_delete"

View file

@ -10,7 +10,8 @@
<color name="bg_transparent">#aa000000</color>
<color name="bg_highlighted">#f3f3f3</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_selection">#333333</color>
<color name="fg_default_low">#666666</color>