allow selecting multiple items by dragging

This commit is contained in:
tibbi 2017-11-12 17:33:49 +01:00
parent 3896bd8943
commit eb34554a01
3 changed files with 71 additions and 3 deletions

View File

@ -18,6 +18,7 @@ import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN
import com.simplemobiletools.commons.helpers.LICENSE_MULTISELECT
import com.simplemobiletools.commons.helpers.LICENSE_STETHO
import com.simplemobiletools.commons.models.Release
import com.simplemobiletools.commons.views.MyScalableRecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
@ -29,6 +30,7 @@ class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener {
setContentView(R.layout.activity_main)
setupLaunchers()
checkWhatsNewDialog()
setupGridLayoutManager()
fab.setOnClickListener {
AddAppLauncherDialog(this, launchers) {
@ -65,10 +67,29 @@ class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener {
startAboutActivity(R.string.app_name, LICENSE_KOTLIN or LICENSE_MULTISELECT or LICENSE_STETHO, BuildConfig.VERSION_NAME)
}
private fun getGridAdapter() = (launchers_grid.adapter as RecyclerAdapter)
private fun setupGridLayoutManager() {
launchers_grid.isDragSelectionEnabled = true
launchers_grid.listener = object : MyScalableRecyclerView.MyScalableRecyclerViewListener {
override fun zoomIn() {}
override fun zoomOut() {}
override fun selectItem(position: Int) {
getGridAdapter().selectItem(position)
}
override fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int) {
getGridAdapter().selectRange(initialSelection, lastDraggedIndex, minReached, maxReached)
}
}
}
private fun setupLaunchers() {
launchers = dbHelper.getLaunchers()
checkInvalidApps()
launchers_holder.adapter = RecyclerAdapter(this, launchers, this) {
launchers_grid.adapter = RecyclerAdapter(this, launchers, this) {
val launchIntent = packageManager.getLaunchIntentForPackage(it.packageName)
if (launchIntent != null) {
startActivity(launchIntent)
@ -97,6 +118,10 @@ class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener {
setupLaunchers()
}
override fun itemLongClicked(position: Int) {
launchers_grid.setDragSelectActive(position)
}
private fun checkWhatsNewDialog() {
arrayListOf<Release>().apply {
checkWhatsNew(this, BuildConfig.VERSION_CODE)

View File

@ -147,6 +147,45 @@ class RecyclerAdapter(val activity: SimpleActivity, val launchers: MutableList<A
actMode?.finish()
}
fun selectItem(pos: Int) {
toggleItemSelection(true, pos)
}
fun selectRange(from: Int, to: Int, min: Int, max: Int) {
if (from == to) {
(min..max).filter { it != from }
.forEach { toggleItemSelection(false, it) }
return
}
if (to < from) {
for (i in to..from)
toggleItemSelection(true, i)
if (min > -1 && min < to) {
(min until to).filter { it != from }
.forEach { toggleItemSelection(false, it) }
}
if (max > -1) {
for (i in from + 1..max)
toggleItemSelection(false, i)
}
} else {
for (i in from..to)
toggleItemSelection(true, i)
if (max > -1 && max > to) {
(to + 1..max).filter { it != from }
.forEach { toggleItemSelection(false, it) }
}
if (min > -1) {
for (i in min until from)
toggleItemSelection(false, i)
}
}
}
class ViewHolder(view: View, val adapterListener: MyAdapterListener, val activity: SimpleActivity, val multiSelectorCallback: ModalMultiSelectorCallback,
val multiSelector: MultiSelector, val listener: AppLaunchersListener?, val itemClick: (AppLauncher) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
fun bindView(launcher: AppLauncher, textColor: Int): View {
@ -176,11 +215,15 @@ class RecyclerAdapter(val activity: SimpleActivity, val launchers: MutableList<A
activity.startSupportActionMode(multiSelectorCallback)
adapterListener.toggleItemSelectionAdapter(true, adapterPosition)
}
listener.itemLongClicked(adapterPosition)
}
}
}
interface AppLaunchersListener {
fun refreshLaunchers()
fun itemLongClicked(position: Int)
}
}

View File

@ -6,8 +6,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/launchers_holder"
<com.simplemobiletools.commons.views.MyScalableRecyclerView
android:id="@+id/launchers_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"