mirror of https://github.com/readrops/Readrops.git
Add custom item touch callback (first kotlin class !)
This commit is contained in:
parent
530632f0f2
commit
a2cc40a7e8
|
@ -13,7 +13,6 @@ import android.widget.ProgressBar;
|
|||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
|
@ -44,10 +43,13 @@ import com.readrops.app.database.pojo.ItemWithFeed;
|
|||
import com.readrops.app.fragments.settings.AccountSettingsFragment;
|
||||
import com.readrops.app.utils.DrawerManager;
|
||||
import com.readrops.app.utils.GlideApp;
|
||||
import com.readrops.app.utils.ReadropsItemTouchCallback;
|
||||
import com.readrops.app.utils.SharedPreferencesManager;
|
||||
import com.readrops.app.utils.Utils;
|
||||
import com.readrops.app.viewmodels.MainViewModel;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -59,7 +61,7 @@ import io.reactivex.disposables.Disposable;
|
|||
import io.reactivex.observers.DisposableSingleObserver;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
|
||||
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener, ReadropsItemTouchCallback.SwipeCallback {
|
||||
|
||||
public static final String TAG = MainActivity.class.getSimpleName();
|
||||
|
||||
|
@ -370,22 +372,36 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
|||
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
|
||||
@Override
|
||||
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
|
||||
int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
|
||||
new ItemTouchHelper(new ReadropsItemTouchCallback(
|
||||
new ReadropsItemTouchCallback.Config.Builder()
|
||||
.swipeDirs(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT)
|
||||
.swipeCallback(this)
|
||||
.build()))
|
||||
.attachToRecyclerView(recyclerView);
|
||||
|
||||
return makeMovementFlags(0, swipeFlags);
|
||||
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
|
||||
@Override
|
||||
public void onItemRangeInserted(int positionStart, int itemCount) {
|
||||
if (scrollToTop) {
|
||||
recyclerView.scrollToPosition(0);
|
||||
scrollToTop = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
|
||||
return false;
|
||||
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
|
||||
if (scrollToTop) {
|
||||
recyclerView.scrollToPosition(0);
|
||||
scrollToTop = false;
|
||||
} else
|
||||
super.onItemRangeMoved(fromPosition, toPosition, itemCount);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
|
||||
if (i == ItemTouchHelper.LEFT) { // set item read state
|
||||
public void onSwipe(@NotNull RecyclerView.ViewHolder viewHolder, int direction) {
|
||||
if (direction == ItemTouchHelper.LEFT) { // set item read state
|
||||
ItemWithFeed itemWithFeed = adapter.getItemWithFeed(viewHolder.getAdapterPosition());
|
||||
|
||||
viewModel.setItemReadState(itemWithFeed, !itemWithFeed.getItem().isRead())
|
||||
|
@ -409,32 +425,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemViewSwipeEnabled() {
|
||||
return true;
|
||||
}
|
||||
}).attachToRecyclerView(recyclerView);
|
||||
|
||||
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
|
||||
@Override
|
||||
public void onItemRangeInserted(int positionStart, int itemCount) {
|
||||
if (scrollToTop) {
|
||||
recyclerView.scrollToPosition(0);
|
||||
scrollToTop = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
|
||||
if (scrollToTop) {
|
||||
recyclerView.scrollToPosition(0);
|
||||
scrollToTop = false;
|
||||
} else
|
||||
super.onItemRangeMoved(fromPosition, toPosition, itemCount);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setReadState(boolean read) {
|
||||
if (allItemsSelected) {
|
||||
viewModel.setAllItemsReadState(read)
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
package com.readrops.app.utils
|
||||
|
||||
import androidx.recyclerview.widget.ItemTouchHelper
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
class ReadropsItemTouchCallback(private val config: Config) : ItemTouchHelper.SimpleCallback(config.dragDirs, config.swipeDirs) {
|
||||
|
||||
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
|
||||
config.moveCallback?.onMove()
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
|
||||
config.swipeCallback?.onSwipe(viewHolder, direction)
|
||||
}
|
||||
|
||||
override fun isItemViewSwipeEnabled(): Boolean {
|
||||
return config.swipeCallback != null
|
||||
}
|
||||
|
||||
override fun isLongPressDragEnabled(): Boolean {
|
||||
return config.moveCallback != null
|
||||
}
|
||||
|
||||
interface MoveCallback {
|
||||
fun onMove()
|
||||
}
|
||||
|
||||
interface SwipeCallback {
|
||||
fun onSwipe(viewHolder: RecyclerView.ViewHolder, direction: Int)
|
||||
}
|
||||
|
||||
class Config(val dragDirs: Int = 0, val swipeDirs: Int = 0, val moveCallback: MoveCallback? = null,
|
||||
val swipeCallback: SwipeCallback? = null) {
|
||||
|
||||
private constructor(builder: Builder) : this(builder.dragDirs, builder.swipeDirs, builder.moveCallback, builder.swipeCallback)
|
||||
|
||||
class Builder {
|
||||
var dragDirs: Int = 0
|
||||
private set
|
||||
|
||||
var swipeDirs: Int = 0
|
||||
private set
|
||||
|
||||
var moveCallback: MoveCallback? = null
|
||||
private set
|
||||
|
||||
var swipeCallback: SwipeCallback? = null
|
||||
private set
|
||||
|
||||
fun dragDirs(dragDirs: Int) = apply { this.dragDirs = dragDirs }
|
||||
|
||||
fun swipeDirs(swipeDirs: Int) = apply { this.swipeDirs = swipeDirs }
|
||||
|
||||
fun moveCallback(moveCallback: MoveCallback) = apply { this.moveCallback = moveCallback }
|
||||
|
||||
fun swipeCallback(swipeCallback: SwipeCallback) = apply { this.swipeCallback = swipeCallback }
|
||||
|
||||
fun build(): Config {
|
||||
return Config(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue