Adds swipe to delete monsters on the library screen.
This commit is contained in:
		| @@ -110,4 +110,10 @@ public class MonsterListRecyclerViewAdapter extends RecyclerView.Adapter<Monster | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public void deleteItem(int position) { | ||||||
|  |         if (mOnDelete != null) { | ||||||
|  |             Monster monster = mValues.get(position); | ||||||
|  |             mOnDelete.onItem(monster); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,13 +1,22 @@ | |||||||
| package com.majinnaibu.monstercards.ui.library; | package com.majinnaibu.monstercards.ui.library; | ||||||
|  |  | ||||||
|  | import android.graphics.Canvas; | ||||||
|  | import android.graphics.Color; | ||||||
|  | import android.graphics.Paint; | ||||||
|  | import android.graphics.PorterDuff; | ||||||
|  | import android.graphics.PorterDuffXfermode; | ||||||
|  | import android.graphics.drawable.ColorDrawable; | ||||||
|  | import android.graphics.drawable.Drawable; | ||||||
| import android.os.Bundle; | import android.os.Bundle; | ||||||
| import android.view.LayoutInflater; | import android.view.LayoutInflater; | ||||||
| import android.view.View; | import android.view.View; | ||||||
| import android.view.ViewGroup; | import android.view.ViewGroup; | ||||||
|  |  | ||||||
| import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||||
|  | import androidx.core.content.ContextCompat; | ||||||
| import androidx.navigation.NavDirections; | import androidx.navigation.NavDirections; | ||||||
| import androidx.navigation.Navigation; | import androidx.navigation.Navigation; | ||||||
|  | import androidx.recyclerview.widget.ItemTouchHelper; | ||||||
| import androidx.recyclerview.widget.LinearLayoutManager; | import androidx.recyclerview.widget.LinearLayoutManager; | ||||||
| import androidx.recyclerview.widget.RecyclerView; | import androidx.recyclerview.widget.RecyclerView; | ||||||
|  |  | ||||||
| @@ -71,10 +80,19 @@ public class LibraryFragment extends MCFragment { | |||||||
|                 this, |                 this, | ||||||
|                 repository.getMonsters(), |                 repository.getMonsters(), | ||||||
|                 (monster) -> { |                 (monster) -> { | ||||||
|  |                     repository | ||||||
|  |                             .deleteMonster(monster) | ||||||
|  |                             .subscribeOn(Schedulers.io()) | ||||||
|  |                             .observeOn(AndroidSchedulers.mainThread()) | ||||||
|  |                             .subscribe(() -> { | ||||||
|  |                                 Logger.logDebug("deleted"); | ||||||
|  |                             }, Logger::logError); | ||||||
|                 }, |                 }, | ||||||
|                 mTwoPane); |                 mTwoPane); | ||||||
|         recyclerView.setAdapter(adapter); |         recyclerView.setAdapter(adapter); | ||||||
|         recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); |         recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); | ||||||
|  |         ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteMonsterCallback(adapter)); | ||||||
|  |         itemTouchHelper.attachToRecyclerView(recyclerView); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     protected void navigateToMonsterDetail(UUID monsterId) { |     protected void navigateToMonsterDetail(UUID monsterId) { | ||||||
| @@ -82,5 +100,60 @@ public class LibraryFragment extends MCFragment { | |||||||
|         Navigation.findNavController(getView()).navigate(action); |         Navigation.findNavController(getView()).navigate(action); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public static class SwipeToDeleteMonsterCallback extends ItemTouchHelper.SimpleCallback { | ||||||
|  |         private final MonsterListRecyclerViewAdapter mAdapter; | ||||||
|  |         private final Drawable icon; | ||||||
|  |         private final ColorDrawable background; | ||||||
|  |         private final Paint clearPaint; | ||||||
|  |  | ||||||
|  |         public SwipeToDeleteMonsterCallback(MonsterListRecyclerViewAdapter adapter) { | ||||||
|  |             super(0, ItemTouchHelper.LEFT); | ||||||
|  |             mAdapter = adapter; | ||||||
|  |             icon = ContextCompat.getDrawable(mAdapter.getContext(), R.drawable.ic_delete_white_36); | ||||||
|  |             background = new ColorDrawable(Color.RED); | ||||||
|  |             clearPaint = new Paint(); | ||||||
|  |             clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { | ||||||
|  |             int position = viewHolder.getAdapterPosition(); | ||||||
|  |             mAdapter.deleteItem(position); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { | ||||||
|  |             View itemView = viewHolder.itemView; | ||||||
|  |             int itemHeight = itemView.getBottom() - itemView.getTop(); | ||||||
|  |             boolean isCancelled = dX == 0 && !isCurrentlyActive; | ||||||
|  |  | ||||||
|  |             if (isCancelled) { | ||||||
|  |                 c.drawRect(itemView.getRight() + dX, itemView.getTop(), itemView.getRight(), itemView.getBottom(), clearPaint); | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|  |             // Draw the red delete background | ||||||
|  |             background.setBounds(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom()); | ||||||
|  |             background.draw(c); | ||||||
|  |  | ||||||
|  |             // Calculate position of delete icon | ||||||
|  |             int iconHeight = icon.getIntrinsicHeight(); | ||||||
|  |             int iconWidth = icon.getIntrinsicWidth(); | ||||||
|  |             int iconTop = itemView.getTop() + (itemHeight - iconHeight) / 2; | ||||||
|  |             int iconMargin = (itemHeight - iconHeight) / 2; | ||||||
|  |             int iconLeft = itemView.getRight() - iconMargin - iconWidth; | ||||||
|  |             int iconRight = itemView.getRight() - iconMargin; | ||||||
|  |             int iconBottom = iconTop + iconHeight; | ||||||
|  |  | ||||||
|  |             // Draw the icon | ||||||
|  |             icon.setBounds(iconLeft, iconTop, iconRight, iconBottom); | ||||||
|  |             icon.draw(c); | ||||||
|  |  | ||||||
|  |             super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										5
									
								
								app/src/main/res/drawable/ic_delete_white_36.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								app/src/main/res/drawable/ic_delete_white_36.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | <vector android:height="24dp" android:tint="#FFFFFF" | ||||||
|  |     android:viewportHeight="24" android:viewportWidth="24" | ||||||
|  |     android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||||||
|  |     <path android:fillColor="@android:color/white" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/> | ||||||
|  | </vector> | ||||||
							
								
								
									
										20
									
								
								app/src/main/res/layout/monster_list_content.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								app/src/main/res/layout/monster_list_content.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | |||||||
|  | <?xml version="1.0" encoding="utf-8"?> | ||||||
|  | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||||||
|  |     android:layout_width="wrap_content" | ||||||
|  |     android:layout_height="wrap_content" | ||||||
|  |     android:orientation="horizontal"> | ||||||
|  |  | ||||||
|  |     <TextView | ||||||
|  |         android:id="@+id/id_text" | ||||||
|  |         android:layout_width="wrap_content" | ||||||
|  |         android:layout_height="wrap_content" | ||||||
|  |         android:layout_margin="@dimen/text_margin" | ||||||
|  |         android:textAppearance="?attr/textAppearanceListItem" /> | ||||||
|  |  | ||||||
|  |     <TextView | ||||||
|  |         android:id="@+id/content" | ||||||
|  |         android:layout_width="wrap_content" | ||||||
|  |         android:layout_height="wrap_content" | ||||||
|  |         android:layout_margin="@dimen/text_margin" | ||||||
|  |         android:textAppearance="?attr/textAppearanceListItem" /> | ||||||
|  | </LinearLayout> | ||||||
| @@ -2,5 +2,6 @@ | |||||||
| <resources> | <resources> | ||||||
|     <color name="colorPrimary">#9B2818</color> |     <color name="colorPrimary">#9B2818</color> | ||||||
|     <color name="colorPrimaryDark">#661A10</color> |     <color name="colorPrimaryDark">#661A10</color> | ||||||
|     <color name="colorAccent">#188B9B</color> |     <!--    <color name="colorAccent">#188B9B</color>--> | ||||||
|  |     <color name="colorAccent">#995500</color> | ||||||
| </resources> | </resources> | ||||||
| @@ -2,4 +2,7 @@ | |||||||
|     <!-- Default screen margins, per the Android Design guidelines. --> |     <!-- Default screen margins, per the Android Design guidelines. --> | ||||||
|     <dimen name="activity_horizontal_margin">16dp</dimen> |     <dimen name="activity_horizontal_margin">16dp</dimen> | ||||||
|     <dimen name="activity_vertical_margin">16dp</dimen> |     <dimen name="activity_vertical_margin">16dp</dimen> | ||||||
|  |     <dimen name="text_margin">16dp</dimen> | ||||||
|  |     <dimen name="fab_margin">16dp</dimen> | ||||||
|  |  | ||||||
| </resources> | </resources> | ||||||
		Reference in New Issue
	
	Block a user