Skip to content

Commit

Permalink
Feature: Added the ability to change swipeThreshold.
Browse files Browse the repository at this point in the history
  • Loading branch information
CreativeCodeCat committed Nov 29, 2024
1 parent 8c5b17a commit 8bcec48
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
get() = prefs.getInt(Constants.FILTER_STRENGTH, 25)
set(value) = prefs.edit().putInt(Constants.FILTER_STRENGTH, value).apply()

var swipeThreshold: Int
get() = prefs.getInt(Constants.SWIPE_THRESHOLD, 100)
set(value) = prefs.edit().putInt(Constants.SWIPE_THRESHOLD, value).apply()

var homeAppAlignment: Int
get() = prefs.getInt(Constants.HOME_APP_ALIGNMENT, Gravity.START)
set(value) = prefs.edit().putInt(Constants.HOME_APP_ALIGNMENT, value).apply()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import com.github.droidworksstudio.launcher.helper.PreferenceHelper
import com.github.droidworksstudio.launcher.utils.Constants
import java.util.*
import kotlin.concurrent.schedule
Expand All @@ -20,7 +21,7 @@ import kotlin.math.abs
* The original code has been modified to fit the specific requirements of this project.
*/

internal open class OnSwipeTouchListener(c: Context?) : OnTouchListener {
internal open class OnSwipeTouchListener(context: Context?, private val preferenceHelper: PreferenceHelper) : OnTouchListener {
private var longPressOn = false
private var doubleTapOn = false
private val gestureDetector: GestureDetector
Expand All @@ -33,47 +34,47 @@ internal open class OnSwipeTouchListener(c: Context?) : OnTouchListener {
}

private inner class GestureListener : SimpleOnGestureListener() {
private val swipeThreshold: Int = 100
private val swipeVelocityThreshold: Int = 100
private val swipeScrollThreshold: Int = 100
private val swipeThreshold: Int = preferenceHelper.swipeThreshold
private val swipeVelocityThreshold: Int = (preferenceHelper.swipeThreshold / 2)
private val swipeScrollThreshold: Int = (preferenceHelper.swipeThreshold / 2)

override fun onDown(e: MotionEvent): Boolean {
override fun onDown(event: MotionEvent): Boolean {
return true
}

override fun onSingleTapUp(e: MotionEvent): Boolean {
override fun onSingleTapUp(event: MotionEvent): Boolean {
if (doubleTapOn) {
doubleTapOn = false
onTripleClick()
}
return super.onSingleTapUp(e)
return super.onSingleTapUp(event)
}

override fun onDoubleTap(e: MotionEvent): Boolean {
override fun onDoubleTap(event: MotionEvent): Boolean {
doubleTapOn = true
Timer().schedule(Constants.TRIPLE_TAP_DELAY_MS.toLong()) {
if (doubleTapOn) {
doubleTapOn = false
onDoubleClick()
}
}
return super.onDoubleTap(e)
return super.onDoubleTap(event)
}

override fun onLongPress(e: MotionEvent) {
override fun onLongPress(event: MotionEvent) {
longPressOn = true
Timer().schedule(Constants.LONG_PRESS_DELAY_MS.toLong()) {
if (longPressOn) onLongClick()
}
super.onLongPress(e)
super.onLongPress(event)
}

// Detecting swipe or drag movement
override fun onScroll(
event1: MotionEvent?,
event2: MotionEvent,
distanceX: Float,
distanceY: Float
distanceY: Float,
): Boolean {
try {
if (event1 == null) return false
Expand Down Expand Up @@ -103,7 +104,7 @@ internal open class OnSwipeTouchListener(c: Context?) : OnTouchListener {
event1: MotionEvent?,
event2: MotionEvent,
velocityX: Float,
velocityY: Float
velocityY: Float,
): Boolean {
try {
val diffY = event2.y - event1!!.y
Expand Down Expand Up @@ -133,6 +134,6 @@ internal open class OnSwipeTouchListener(c: Context?) : OnTouchListener {
open fun onTripleClick() {}

init {
gestureDetector = GestureDetector(c, GestureListener())
gestureDetector = GestureDetector(context, GestureListener())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class DrawFragment : Fragment(),
private lateinit var context: Context
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
savedInstanceState: Bundle?,
): View {

_binding = FragmentDrawBinding.inflate(inflater, container, false)
Expand Down Expand Up @@ -235,7 +235,7 @@ class DrawFragment : Fragment(),


private fun getSwipeGestureListener(context: Context): View.OnTouchListener {
return object : OnSwipeTouchListener(context) {
return object : OnSwipeTouchListener(context, preferenceHelper) {
override fun onSwipeLeft() {
super.onSwipeLeft()
val actionTypeNavOptions: NavOptions? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class FavoriteFragment : Fragment(),

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
savedInstanceState: Bundle?,
): View {

_binding = FragmentFavoriteBinding.inflate(inflater, container, false)
Expand Down Expand Up @@ -136,7 +136,7 @@ class FavoriteFragment : Fragment(),
override fun onChildDraw(
canvas: Canvas, recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder, dX: Float,
dY: Float, actionState: Int, isCurrentlyActive: Boolean
dY: Float, actionState: Int, isCurrentlyActive: Boolean,
) {
if (isCurrentlyActive) {
viewHolder.itemView.alpha = 0.5f
Expand All @@ -152,7 +152,7 @@ class FavoriteFragment : Fragment(),

override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder
viewHolder: RecyclerView.ViewHolder,
): Int {
val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
val swipeFlags = 0
Expand All @@ -161,7 +161,7 @@ class FavoriteFragment : Fragment(),

override fun onMove(
recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
target: RecyclerView.ViewHolder,
): Boolean {

val oldPosition = viewHolder.bindingAdapterPosition
Expand Down Expand Up @@ -199,7 +199,7 @@ class FavoriteFragment : Fragment(),
}

private fun getSwipeGestureListener(context: Context): View.OnTouchListener {
return object : OnSwipeTouchListener(context) {
return object : OnSwipeTouchListener(context, preferenceHelper) {
override fun onSwipeLeft() {
super.onSwipeLeft()
findNavController().navigateUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class HiddenFragment : Fragment(),

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
savedInstanceState: Bundle?,
): View {

_binding = FragmentHiddenBinding.inflate(inflater, container, false)
Expand Down Expand Up @@ -116,7 +116,7 @@ class HiddenFragment : Fragment(),
}

private fun getSwipeGestureListener(context: Context): View.OnTouchListener {
return object : OnSwipeTouchListener(context) {
return object : OnSwipeTouchListener(context, preferenceHelper) {
override fun onSwipeLeft() {
super.onSwipeLeft()
findNavController().navigateUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class HomeFragment : Fragment(),

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
savedInstanceState: Bundle?,
): View {

_binding = FragmentHomeBinding.inflate(inflater, container, false)
Expand Down Expand Up @@ -344,7 +344,7 @@ class HomeFragment : Fragment(),
}

private fun getSwipeGestureListener(context: Context): View.OnTouchListener {
return object : OnSwipeTouchListener(context) {
return object : OnSwipeTouchListener(context, preferenceHelper) {
override fun onLongClick() {
super.onLongClick()
trySettings()
Expand Down Expand Up @@ -403,7 +403,8 @@ class HomeFragment : Fragment(),
Constants.Swipe.Up,
Constants.Swipe.Down,
Constants.Swipe.Left,
Constants.Swipe.Right -> {
Constants.Swipe.Right,
-> {
val packageName = when (actionType) {
Constants.Swipe.DoubleTap -> preferenceHelper.doubleTapApp
Constants.Swipe.Up -> preferenceHelper.swipeUpApp
Expand Down Expand Up @@ -526,7 +527,7 @@ class HomeFragment : Fragment(),
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(
errorCode: Int,
errString: CharSequence
errString: CharSequence,
) {
when (errorCode) {
BiometricPrompt.ERROR_USER_CANCELED -> requireContext().showLongToast(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SettingsFeaturesFragment : Fragment(),

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
savedInstanceState: Bundle?,
): View {
// Inflate the layout for this fragment
_binding = FragmentSettingsFeaturesBinding.inflate(inflater, container, false)
Expand All @@ -82,6 +82,7 @@ class SettingsFeaturesFragment : Fragment(),
miscellaneousSearchEngineControl.text = preferenceHelper.searchEngines.getString(context)
miscellaneousAppLanguageControl.text = preferenceHelper.appLanguage.name
miscellaneousFilterStrengthControl.text = "${preferenceHelper.filterStrength}"
miscellaneousSwipeThresholdControl.text = "${preferenceHelper.swipeThreshold}"
}

val actions = listOf(
Expand All @@ -102,7 +103,7 @@ class SettingsFeaturesFragment : Fragment(),
context: Context,
action: Constants.Action,
appPackageName: String?,
textView: TextView
textView: TextView,
) {
val actionText = if (action == Constants.Action.OpenApp) {
val appName = appPackageName?.let { context.getAppNameFromPackageName(it) }
Expand Down Expand Up @@ -161,12 +162,16 @@ class SettingsFeaturesFragment : Fragment(),
showSearchEngineDialog()
}

miscellaneousAppLanguageControl.setOnClickListener {
showAppLanguageDialog()
}

miscellaneousFilterStrengthControl.setOnClickListener {
showFilterStrengthDialog()
}

miscellaneousAppLanguageControl.setOnClickListener {
showAppLanguageDialog()
miscellaneousSwipeThresholdControl.setOnClickListener {
showSwipeThresholdDialog()
}
}
}
Expand Down Expand Up @@ -282,22 +287,90 @@ class SettingsFeaturesFragment : Fragment(),
val dialogBuilder = MaterialAlertDialogBuilder(context).apply {
setTitle(getString(R.string.settings_select_filter_strength))
setView(seekBarLayout) // Add the slider directly to the dialog
setPositiveButton("ok") { _, _ ->
setPositiveButton(getString(R.string.settings_ok)) { _, _ ->
// Save the slider value when OK is pressed
preferenceViewModel.setFilterStrength(currentValue)
binding.miscellaneousFilterStrengthControl.text = "$currentValue"

val feedbackType = "select"
appHelper.triggerHapticFeedback(context, feedbackType)
}
setNegativeButton("cancel", null)
setNegativeButton(getString(R.string.settings_cancel), null)
}

// Assign the created dialog to launcherFontDialog
filterStrengthDialog = dialogBuilder.create()
filterStrengthDialog?.show()
}

private var swipeThresholdDialog: AlertDialog? = null

@RequiresApi(Build.VERSION_CODES.Q)
private fun showSwipeThresholdDialog() {
// Dismiss any existing dialog to prevent multiple dialogs open simultaneously
swipeThresholdDialog?.dismiss()

var currentValue = preferenceHelper.swipeThreshold

// Create a layout to hold the SeekBar and the value display
val seekBarLayout = LinearLayout(context).apply {
orientation = LinearLayout.VERTICAL
gravity = Gravity.CENTER
setPadding(16, 16, 16, 16)

// TextView to display the current value
val valueText = TextView(context).apply {
text = "$currentValue"
textSize = 16f
gravity = Gravity.CENTER
}

// SeekBar for horizontal number selection
val seekBar = SeekBar(context).apply {
min = Constants.SWIPE_THRESHOLD_MIN // Maximum value
max = Constants.SWIPE_THRESHOLD_MAX // Maximum value
progress = currentValue // Default value
setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
currentValue = progress
valueText.text = "$currentValue"
}

override fun onStartTrackingTouch(seekBar: SeekBar) {
// Not used
}

override fun onStopTrackingTouch(seekBar: SeekBar) {
// Not used
}
})
}

// Add TextView and SeekBar to the layout
addView(valueText)
addView(seekBar)
}

// Create the dialog
val dialogBuilder = MaterialAlertDialogBuilder(context).apply {
setTitle(getString(R.string.settings_select_swipe_threshold))
setView(seekBarLayout) // Add the slider directly to the dialog
setPositiveButton(getString(R.string.settings_ok)) { _, _ ->
// Save the slider value when OK is pressed
preferenceViewModel.setSwipeThreshold(currentValue)
binding.miscellaneousSwipeThresholdControl.text = "$currentValue"

val feedbackType = "select"
appHelper.triggerHapticFeedback(context, feedbackType)
}
setNegativeButton(getString(R.string.settings_cancel), null)
}

// Assign the created dialog to launcherFontDialog
swipeThresholdDialog = dialogBuilder.create()
swipeThresholdDialog?.show()
}

private var appSelectionDialog: AlertDialog? = null

@RequiresApi(Build.VERSION_CODES.Q)
Expand All @@ -323,7 +396,8 @@ class SettingsFeaturesFragment : Fragment(),
Constants.Swipe.Up,
Constants.Swipe.Down,
Constants.Swipe.Left,
Constants.Swipe.Right -> handleSwipeAction(swipeType, selectedPackageName)
Constants.Swipe.Right,
-> handleSwipeAction(swipeType, selectedPackageName)
}
val feedbackType = "select"
appHelper.triggerHapticFeedback(context, feedbackType)
Expand Down Expand Up @@ -497,6 +571,7 @@ class SettingsFeaturesFragment : Fragment(),
swipeActionDialog?.dismiss()
searchEngineDialog?.dismiss()
filterStrengthDialog?.dismiss()
swipeThresholdDialog?.dismiss()
appSelectionDialog?.dismiss()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class WidgetFragment : Fragment(),

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
savedInstanceState: Bundle?,
): View {
// Inflate the layout for this fragment
_binding = FragmentWidgetsBinding.inflate(inflater, container, false)
Expand Down Expand Up @@ -386,7 +386,7 @@ class WidgetFragment : Fragment(),
}

private fun getSwipeGestureListener(context: Context): View.OnTouchListener {
return object : OnSwipeTouchListener(context) {
return object : OnSwipeTouchListener(context, preferenceHelper) {
override fun onLongClick() {
super.onLongClick()
val actionTypeNavOptions: NavOptions? =
Expand Down
Loading

0 comments on commit 8bcec48

Please sign in to comment.