Skip to content

Commit

Permalink
feat: Added Manual Slide Control Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumman04 committed Dec 1, 2023
1 parent 0e7b8cd commit f57b3a1
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 113 deletions.
Binary file modified J-Slider-release.aar
Binary file not shown.
2 changes: 1 addition & 1 deletion J-Slider/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ afterEvaluate {
register<MavenPublication>("release") {
groupId = "com.github.Jumman04"
artifactId = "Jummania-Slider"
version = "4.0"
version = "4.2"

afterEvaluate {
from(components["release"])
Expand Down
41 changes: 39 additions & 2 deletions J-Slider/src/main/java/com/jummania/JSlider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.graphics.Color
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -241,6 +240,12 @@ class JSlider @JvmOverloads constructor(
)]
)


// Sets the manual sliding behavior based on the value of the custom attribute "manualSlidable" defined
// in the XML layout file using a TypedArray.

setManualSlidable(typedArray.getBoolean(R.styleable.JSlider_manualSlidable, true))

} finally {
// Recycle the TypedArray to avoid memory leaks
typedArray.recycle()
Expand All @@ -252,7 +257,6 @@ class JSlider @JvmOverloads constructor(
addView(selectedIndicatorLayout)

} catch (e: Exception) {
Log.e("JSlider Initialization", "An unexpected error occurred: ${e.message}")
e.printStackTrace()
}

Expand Down Expand Up @@ -853,6 +857,24 @@ class JSlider @JvmOverloads constructor(
this@JSlider.shapeTypes = shapeTypes
}

/**
* Sets the flag indicating whether manual sliding is enabled.
*
* @param isEnabled If true, manual sliding is enabled; if false, it follows default behavior.
*/
fun setManualSlidable(isEnabled: Boolean) {
manualSlidable = isEnabled
}

/**
* Checks whether manual sliding is currently enabled.
*
* @return True if manual sliding is enabled, false otherwise.
*/
fun isManualSlidableEnabled(): Boolean {
return manualSlidable
}


/**
* Check if the Slider has been set, and throw an exception if any modification is attempted
Expand Down Expand Up @@ -968,8 +990,23 @@ class JSlider @JvmOverloads constructor(
this@JSlider.listener = listener
}


/**
* This internal companion object is used to hold shared properties related to measuring and sliding.
*/
internal companion object {
/**
* Represents the measure specification to be used for sizing views.
* Default value is set to 0.
*/
internal var measureSpec: Int = 0

/**
* A flag indicating whether manual sliding is enabled.
* If true, the sliding behavior is controlled manually; otherwise, it follows default behavior.
*/
internal var manualSlidable = true
}


}
24 changes: 24 additions & 0 deletions J-Slider/src/main/java/com/jummania/widgets/Slider.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.jummania.widgets

import android.content.Context
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager
import com.jummania.JSlider.Companion.manualSlidable
import com.jummania.JSlider.Companion.measureSpec

/**
Expand Down Expand Up @@ -50,4 +52,26 @@ internal abstract class Slider(context: Context) : ViewPager(context) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)

}


/**
* Overrides the onTouchEvent method to control touch events.
*
* @param event The MotionEvent representing the touch event.
* @return True if manual sliding is enabled and the super implementation returns true; false otherwise.
*/
override fun onTouchEvent(event: MotionEvent?): Boolean {
return manualSlidable && super.onTouchEvent(event)
}

/**
* Overrides the onInterceptTouchEvent method to intercept touch events before they are dispatched to child views.
*
* @param ev The MotionEvent representing the touch event.
* @return True if manual sliding is enabled and the super implementation returns true; false otherwise.
*/
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
return manualSlidable && super.onInterceptTouchEvent(ev)
}

}
3 changes: 3 additions & 0 deletions J-Slider/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<!-- Enable or disable auto-sliding functionality -->
<attr name="enableAutoSliding" format="boolean" />

<!-- If set to true, the view allows manual sliding; if set to false, it follows default behavior.-->
<attr name="manualSlidable" format="boolean" />

<!-- Default color for the indicator shape -->
<attr name="defaultIndicatorColor" format="color|reference" />

Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
minSdk = 17
targetSdk = 34
versionCode = 4
versionName = "4.0"
versionName = "4.2"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


val jSlider: JSlider = findViewById(R.id.jSlider) //find
// val jSlider:JSlider = JSlider(this) // or bind the slider

Expand Down
2 changes: 1 addition & 1 deletion jitpack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ before_install:
- ./scripts/prepareJitpackEnvironment.sh
install:
- FILE="-Dfile=J-Slider-release.aar"
- mvn install:install-file $FILE -DgroupId=com.github.Jumman04 -DartifactId=Jummania-Slider -Dversion=4.0 -Dpackaging=aar -DgeneratePom=true -DpomFile=pom.xml
- mvn install:install-file $FILE -DgroupId=com.github.Jumman04 -DartifactId=Jummania-Slider -Dversion=4.2 -Dpackaging=aar -DgeneratePom=true -DpomFile=pom.xml
107 changes: 0 additions & 107 deletions module.json

This file was deleted.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.Jumman04</groupId>
<artifactId>Jummania-Slider</artifactId>
<version>4.0</version>
<version>4.2</version>
<packaging>aar</packaging>

<properties>
Expand Down

0 comments on commit f57b3a1

Please sign in to comment.