mirror of
				https://github.com/SimpleMobileTools/Simple-App-Launcher.git
				synced 2025-06-05 21:49:21 +02:00 
			
		
		
		
	allow selecting multiple items by dragging
This commit is contained in:
		| @@ -18,6 +18,7 @@ import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN | |||||||
| import com.simplemobiletools.commons.helpers.LICENSE_MULTISELECT | import com.simplemobiletools.commons.helpers.LICENSE_MULTISELECT | ||||||
| import com.simplemobiletools.commons.helpers.LICENSE_STETHO | import com.simplemobiletools.commons.helpers.LICENSE_STETHO | ||||||
| import com.simplemobiletools.commons.models.Release | import com.simplemobiletools.commons.models.Release | ||||||
|  | import com.simplemobiletools.commons.views.MyScalableRecyclerView | ||||||
| import kotlinx.android.synthetic.main.activity_main.* | import kotlinx.android.synthetic.main.activity_main.* | ||||||
| import java.util.* | import java.util.* | ||||||
|  |  | ||||||
| @@ -29,6 +30,7 @@ class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener { | |||||||
|         setContentView(R.layout.activity_main) |         setContentView(R.layout.activity_main) | ||||||
|         setupLaunchers() |         setupLaunchers() | ||||||
|         checkWhatsNewDialog() |         checkWhatsNewDialog() | ||||||
|  |         setupGridLayoutManager() | ||||||
|  |  | ||||||
|         fab.setOnClickListener { |         fab.setOnClickListener { | ||||||
|             AddAppLauncherDialog(this, launchers) { |             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) |         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() { |     private fun setupLaunchers() { | ||||||
|         launchers = dbHelper.getLaunchers() |         launchers = dbHelper.getLaunchers() | ||||||
|         checkInvalidApps() |         checkInvalidApps() | ||||||
|         launchers_holder.adapter = RecyclerAdapter(this, launchers, this) { |         launchers_grid.adapter = RecyclerAdapter(this, launchers, this) { | ||||||
|             val launchIntent = packageManager.getLaunchIntentForPackage(it.packageName) |             val launchIntent = packageManager.getLaunchIntentForPackage(it.packageName) | ||||||
|             if (launchIntent != null) { |             if (launchIntent != null) { | ||||||
|                 startActivity(launchIntent) |                 startActivity(launchIntent) | ||||||
| @@ -97,6 +118,10 @@ class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener { | |||||||
|         setupLaunchers() |         setupLaunchers() | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     override fun itemLongClicked(position: Int) { | ||||||
|  |         launchers_grid.setDragSelectActive(position) | ||||||
|  |     } | ||||||
|  |  | ||||||
|     private fun checkWhatsNewDialog() { |     private fun checkWhatsNewDialog() { | ||||||
|         arrayListOf<Release>().apply { |         arrayListOf<Release>().apply { | ||||||
|             checkWhatsNew(this, BuildConfig.VERSION_CODE) |             checkWhatsNew(this, BuildConfig.VERSION_CODE) | ||||||
|   | |||||||
| @@ -147,6 +147,45 @@ class RecyclerAdapter(val activity: SimpleActivity, val launchers: MutableList<A | |||||||
|         actMode?.finish() |         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, |     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()) { |                      val multiSelector: MultiSelector, val listener: AppLaunchersListener?, val itemClick: (AppLauncher) -> (Unit)) : SwappingHolder(view, MultiSelector()) { | ||||||
|         fun bindView(launcher: AppLauncher, textColor: Int): View { |         fun bindView(launcher: AppLauncher, textColor: Int): View { | ||||||
| @@ -176,11 +215,15 @@ class RecyclerAdapter(val activity: SimpleActivity, val launchers: MutableList<A | |||||||
|                     activity.startSupportActionMode(multiSelectorCallback) |                     activity.startSupportActionMode(multiSelectorCallback) | ||||||
|                     adapterListener.toggleItemSelectionAdapter(true, adapterPosition) |                     adapterListener.toggleItemSelectionAdapter(true, adapterPosition) | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|  |                 listener.itemLongClicked(adapterPosition) | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     interface AppLaunchersListener { |     interface AppLaunchersListener { | ||||||
|         fun refreshLaunchers() |         fun refreshLaunchers() | ||||||
|  |  | ||||||
|  |         fun itemLongClicked(position: Int) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,8 +6,8 @@ | |||||||
|     android:layout_width="match_parent" |     android:layout_width="match_parent" | ||||||
|     android:layout_height="match_parent"> |     android:layout_height="match_parent"> | ||||||
|  |  | ||||||
|     <android.support.v7.widget.RecyclerView |     <com.simplemobiletools.commons.views.MyScalableRecyclerView | ||||||
|         android:id="@+id/launchers_holder" |         android:id="@+id/launchers_grid" | ||||||
|         android:layout_width="match_parent" |         android:layout_width="match_parent" | ||||||
|         android:layout_height="match_parent" |         android:layout_height="match_parent" | ||||||
|         android:clipToPadding="false" |         android:clipToPadding="false" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user