From 9cd1068f04a9ec2fc1c69dd69519a5017ef646d3 Mon Sep 17 00:00:00 2001 From: m7mdra Date: Wed, 28 Sep 2022 12:09:01 +0300 Subject: [PATCH 1/2] migrate to androidx --- app/build.gradle | 17 ++++++++--------- .../stepbarviewdemo/ExampleInstrumentedTest.kt | 4 ++-- .../java/ir/neo/stepbarviewdemo/MainActivity.kt | 2 +- app/src/main/res/layout/activity_main.xml | 4 ++-- build.gradle | 9 ++++----- gradle.properties | 2 ++ gradle/wrapper/gradle-wrapper.properties | 6 +++--- stepbarview/build.gradle | 8 ++++---- .../stepbarview/ExampleInstrumentedTest.java | 4 ++-- .../main/java/ir/neo/stepbarview/StepBarView.kt | 2 +- 10 files changed, 29 insertions(+), 29 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5804fab..64a9af4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,14 +5,14 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 26 + compileSdkVersion 33 defaultConfig { applicationId "ir.neo.stepbarviewdemo" minSdkVersion 16 - targetSdkVersion 26 + targetSdkVersion 33 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } buildTypes { release { @@ -24,11 +24,10 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" - implementation 'com.android.support:appcompat-v7:26.1.0' - implementation 'com.android.support.constraint:constraint-layout:1.0.2' - testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.1' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation project(':stepbarview') } diff --git a/app/src/androidTest/java/ir/neo/stepbarviewdemo/ExampleInstrumentedTest.kt b/app/src/androidTest/java/ir/neo/stepbarviewdemo/ExampleInstrumentedTest.kt index 24e4a63..944f75f 100644 --- a/app/src/androidTest/java/ir/neo/stepbarviewdemo/ExampleInstrumentedTest.kt +++ b/app/src/androidTest/java/ir/neo/stepbarviewdemo/ExampleInstrumentedTest.kt @@ -1,7 +1,7 @@ package ir.neo.stepbarviewdemo -import android.support.test.InstrumentationRegistry -import android.support.test.runner.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 import org.junit.Test import org.junit.runner.RunWith diff --git a/app/src/main/java/ir/neo/stepbarviewdemo/MainActivity.kt b/app/src/main/java/ir/neo/stepbarviewdemo/MainActivity.kt index 103684b..6289021 100644 --- a/app/src/main/java/ir/neo/stepbarviewdemo/MainActivity.kt +++ b/app/src/main/java/ir/neo/stepbarviewdemo/MainActivity.kt @@ -1,7 +1,7 @@ package ir.neo.stepbarviewdemo import android.os.Bundle -import android.support.v7.app.AppCompatActivity +import androidx.appcompat.app.AppCompatActivity import android.view.View import android.widget.* import ir.neo.stepbarview.DpHandler diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f8de7ab..1625e6b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,5 +1,5 @@ - - + diff --git a/build.gradle b/build.gradle index 7d19f4e..f93382d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.2.31' - ext.kotlin_version = '1.2.21' + ext.kotlin_version = '1.7.10' repositories { google() - jcenter() + mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.1' + classpath "com.android.tools.build:gradle:4.1.3" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong @@ -19,7 +18,7 @@ buildscript { allprojects { repositories { google() - jcenter() + mavenCentral() } } diff --git a/gradle.properties b/gradle.properties index aac7c9b..9e6fce1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,6 +9,8 @@ # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. +android.enableJetifier=true +android.useAndroidX=true org.gradle.jvmargs=-Xmx1536m # When configured, Gradle will run in incubating parallel mode. diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 22500ac..8b57d0f 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Apr 10 02:02:21 IRDT 2018 +#Wed Sep 28 12:00:48 AST 2022 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip +zipStoreBase=GRADLE_USER_HOME +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip diff --git a/stepbarview/build.gradle b/stepbarview/build.gradle index 8f9b2f9..53f51f9 100644 --- a/stepbarview/build.gradle +++ b/stepbarview/build.gradle @@ -12,7 +12,7 @@ android { versionCode 3 versionName "1.0.4" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } @@ -28,10 +28,10 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'com.android.support:appcompat-v7:26.1.0' + implementation 'androidx.appcompat:appcompat:1.0.0' testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.1' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" } repositories { diff --git a/stepbarview/src/androidTest/java/ir/neo/stepbarview/ExampleInstrumentedTest.java b/stepbarview/src/androidTest/java/ir/neo/stepbarview/ExampleInstrumentedTest.java index 2a30065..3dbc307 100644 --- a/stepbarview/src/androidTest/java/ir/neo/stepbarview/ExampleInstrumentedTest.java +++ b/stepbarview/src/androidTest/java/ir/neo/stepbarview/ExampleInstrumentedTest.java @@ -1,8 +1,8 @@ package ir.neo.stepbarview; import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/stepbarview/src/main/java/ir/neo/stepbarview/StepBarView.kt b/stepbarview/src/main/java/ir/neo/stepbarview/StepBarView.kt index dbbc7ba..a57b064 100644 --- a/stepbarview/src/main/java/ir/neo/stepbarview/StepBarView.kt +++ b/stepbarview/src/main/java/ir/neo/stepbarview/StepBarView.kt @@ -8,7 +8,7 @@ import android.graphics.Rect import android.graphics.RectF import android.os.Parcel import android.os.Parcelable -import android.support.v4.content.ContextCompat +import androidx.core.content.ContextCompat import android.util.AttributeSet import android.util.Log import android.view.MotionEvent From 4445f6e17767f18056045e8996a1dbbc50fa9689 Mon Sep 17 00:00:00 2001 From: m7mdra Date: Wed, 28 Sep 2022 12:22:12 +0300 Subject: [PATCH 2/2] more fixes found while attempting to run sample app --- app/build.gradle | 9 +- app/src/main/AndroidManifest.xml | 4 +- .../java/ir/neo/stepbarview/StepBarView.kt | 373 ++++++++++-------- 3 files changed, 220 insertions(+), 166 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 64a9af4..3e9b168 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,11 +5,11 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 33 + compileSdkVersion 32 defaultConfig { applicationId "ir.neo.stepbarviewdemo" minSdkVersion 16 - targetSdkVersion 33 + targetSdkVersion 32 versionCode 1 versionName "1.0" testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' @@ -20,6 +20,11 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + compileOptions { + sourceCompatibility 1.8 + targetCompatibility 1.8 + } + } dependencies { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e8e07db..24290ee 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,7 +9,9 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - + diff --git a/stepbarview/src/main/java/ir/neo/stepbarview/StepBarView.kt b/stepbarview/src/main/java/ir/neo/stepbarview/StepBarView.kt index a57b064..6f9d581 100644 --- a/stepbarview/src/main/java/ir/neo/stepbarview/StepBarView.kt +++ b/stepbarview/src/main/java/ir/neo/stepbarview/StepBarView.kt @@ -19,8 +19,8 @@ import android.view.View * iman.neofight@gmail.com */ class StepBarView @JvmOverloads -constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : - View(mContext,attrs, defStyleAttr) { +constructor(mContext: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : + View(mContext, attrs, defStyleAttr) { companion object { private val IS_DEBUG = false @@ -29,48 +29,48 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = private val NAME_STEP_SEPARATION_PX = 10 } - private var stepsPaint : Paint - private var stepsStrokePaint : Paint - private var stepsLinePaint : Paint - private var stepsTextPaint : Paint + private var stepsPaint: Paint + private var stepsStrokePaint: Paint + private var stepsLinePaint: Paint + private var stepsTextPaint: Paint private var tmpRect = Rect() - var onStepChangeListener : OnStepChangeListener? = null + var onStepChangeListener: OnStepChangeListener? = null private val rawHeiht - get() = Math.max(stepsSize,stepsLineHeight) + get() = Math.max(stepsSize, stepsLineHeight) private val rawWidth get() = (linesWidth * (maxCount - 1)) + // Lines Width - ((stepsLineMarginLeft + stepsLineMarginRight) * (maxCount - 1)) + // Lines Margins - ((stepsSize + stepsStrokeSize) * maxCount) + ((stepsLineMarginLeft + stepsLineMarginRight) * (maxCount - 1)) + // Lines Margins + ((stepsSize + stepsStrokeSize) * maxCount) //Lines between steps will drawn based on this variable //Note that now width is match_parent - private val linesWidth : Float + private val linesWidth: Float get() { return if (isFixedStepsLineWidth) { stepsLineWidth } else { val allStepsSize = (maxCount * stepsSize) - val allMarginsSize = ((maxCount-1) * (stepsLineMarginLeft + stepsLineMarginRight)) + val allMarginsSize = ((maxCount - 1) * (stepsLineMarginLeft + stepsLineMarginRight)) val width = width - paddingLeft - paddingRight val available = (width - allStepsSize - allMarginsSize) - available / (maxCount-1) + available / (maxCount - 1) } } //This property used in drawing stuff private val yPos - get() = ((rawHeiht/2) + paddingTop) + get() = ((rawHeiht / 2) + paddingTop) - var maxCount : Int = 0 + var maxCount: Int = 0 set(value) { - if(allowTouchStepTo == field) + if (allowTouchStepTo == field) allowTouchStepTo = value field = value invalidate() @@ -83,55 +83,55 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = invalidate() } - var stepsReachedColor : Int = 0 + var stepsReachedColor: Int = 0 set(value) { field = value invalidate() } - var stepsUnreachedColor : Int = 0 + var stepsUnreachedColor: Int = 0 set(value) { field = value invalidate() } - var stepsLineReachedColor : Int = 0 + var stepsLineReachedColor: Int = 0 set(value) { field = value invalidate() } - var stepsLineUnreachedColor : Int = 0 + var stepsLineUnreachedColor: Int = 0 set(value) { field = value invalidate() } - var stepsLineHeight : Float = 0f + var stepsLineHeight: Float = 0f set(value) { field = value requestLayout() } - var stepsSize : Float = 0f + var stepsSize: Float = 0f set(value) { field = value requestLayout() } - var stepsTextColor : Int = 0 + var stepsTextColor: Int = 0 set(value) { field = value invalidate() } - var stepsTextSize : Float = 0f + var stepsTextSize: Float = 0f set(value) { field = value invalidate() } - var stepsLineMarginLeft : Float = 0f + var stepsLineMarginLeft: Float = 0f set(value) { field = value invalidate() @@ -143,7 +143,7 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = invalidate() } - var allowTouchStepTo : Int = 0 + var allowTouchStepTo: Int = 0 var showStepIndex: Boolean = true set(value) { @@ -157,56 +157,56 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = invalidate() } - var stepsStrokeSize : Float = 0f + var stepsStrokeSize: Float = 0f set(value) { field = value invalidate() } - var stepsStrokeReachedColor : Int = 0 + var stepsStrokeReachedColor: Int = 0 set(value) { field = value invalidate() } - var stepsStrokeUnReachedColor : Int = 0 + var stepsStrokeUnReachedColor: Int = 0 set(value) { field = value invalidate() } - var stepsStrokeCurrentColor : Int = 0 + var stepsStrokeCurrentColor: Int = 0 set(value) { field = value invalidate() } - var showStepStroke : Boolean = true + var showStepStroke: Boolean = true set(value) { field = value invalidate() } - var isRtl : Boolean = false + var isRtl: Boolean = false set(value) { field = value invalidate() } - var isFixedStepsLineWidth : Boolean = false + var isFixedStepsLineWidth: Boolean = false set(value) { field = value invalidate() } - var stepsLineWidth : Float = 0f + var stepsLineWidth: Float = 0f set(value) { field = value invalidate() } - var allowSelectStep = object : AllowSelectStep{ + var allowSelectStep = object : AllowSelectStep { override fun allowSelectStep(step: Int) = true } @@ -220,31 +220,35 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = maxCount = 8 reachedStep = 1 - stepsReachedColor = ContextCompat.getColor(context,R.color.sbv_step_reached_color) - stepsUnreachedColor = ContextCompat.getColor(context,R.color.sbv_step_unreached_color) + stepsReachedColor = ContextCompat.getColor(context, R.color.sbv_step_reached_color) + stepsUnreachedColor = ContextCompat.getColor(context, R.color.sbv_step_unreached_color) - stepsLineReachedColor = ContextCompat.getColor(context,R.color.sbv_step_line_reached_color) - stepsLineUnreachedColor = ContextCompat.getColor(context,R.color.sbv_step_line_unreached_color) + stepsLineReachedColor = ContextCompat.getColor(context, R.color.sbv_step_line_reached_color) + stepsLineUnreachedColor = + ContextCompat.getColor(context, R.color.sbv_step_line_unreached_color) - stepsLineHeight = DpHandler.dpToPx(context,4).toFloat() + stepsLineHeight = DpHandler.dpToPx(context, 4).toFloat() - stepsSize = DpHandler.dpToPx(context,16).toFloat() - stepsTextColor = ContextCompat.getColor(context,R.color.sbv_step_text_color) - stepsTextSize = DpHandler.spToPx(context,14f) + stepsSize = DpHandler.dpToPx(context, 16).toFloat() + stepsTextColor = ContextCompat.getColor(context, R.color.sbv_step_text_color) + stepsTextSize = DpHandler.spToPx(context, 14f) - stepsLineMarginLeft = DpHandler.dpToPx(context,2).toFloat() - stepsLineMarginRight = DpHandler.dpToPx(context,2).toFloat() + stepsLineMarginLeft = DpHandler.dpToPx(context, 2).toFloat() + stepsLineMarginRight = DpHandler.dpToPx(context, 2).toFloat() allowTouchStepTo = maxCount showStepIndex = true showStepName = false - stepsStrokeSize = DpHandler.dpToPx(context,2).toFloat() + stepsStrokeSize = DpHandler.dpToPx(context, 2).toFloat() - stepsStrokeReachedColor = ContextCompat.getColor(context,R.color.sbv_step_stroke_reached_color) - stepsStrokeUnReachedColor = ContextCompat.getColor(context,R.color.sbv_step_stroke_unreached_color) - stepsStrokeCurrentColor = ContextCompat.getColor(context,R.color.sbv_step_stroke_current_color) + stepsStrokeReachedColor = + ContextCompat.getColor(context, R.color.sbv_step_stroke_reached_color) + stepsStrokeUnReachedColor = + ContextCompat.getColor(context, R.color.sbv_step_stroke_unreached_color) + stepsStrokeCurrentColor = + ContextCompat.getColor(context, R.color.sbv_step_stroke_current_color) showStepStroke = false @@ -257,42 +261,76 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = attrs.let { val a = mContext.obtainStyledAttributes(attrs, R.styleable.StepBarView) - maxCount = a.getInt(R.styleable.StepBarView_sbv_max_count,maxCount) + maxCount = a.getInt(R.styleable.StepBarView_sbv_max_count, maxCount) - stepsReachedColor = a.getColor(R.styleable.StepBarView_sbv_steps_reached_colors,stepsReachedColor) - stepsUnreachedColor = a.getColor(R.styleable.StepBarView_sbv_steps_unreached_colors,stepsUnreachedColor) + stepsReachedColor = + a.getColor(R.styleable.StepBarView_sbv_steps_reached_colors, stepsReachedColor) + stepsUnreachedColor = + a.getColor(R.styleable.StepBarView_sbv_steps_unreached_colors, stepsUnreachedColor) - stepsLineReachedColor = a.getColor(R.styleable.StepBarView_sbv_steps_line_reached_colors,stepsLineReachedColor) - stepsLineUnreachedColor = a.getColor(R.styleable.StepBarView_sbv_steps_line_unreached_colors,stepsLineUnreachedColor) + stepsLineReachedColor = a.getColor( + R.styleable.StepBarView_sbv_steps_line_reached_colors, + stepsLineReachedColor + ) + stepsLineUnreachedColor = a.getColor( + R.styleable.StepBarView_sbv_steps_line_unreached_colors, + stepsLineUnreachedColor + ) - stepsLineHeight = a.getDimension(R.styleable.StepBarView_sbv_steps_line_height,stepsLineHeight) + stepsLineHeight = + a.getDimension(R.styleable.StepBarView_sbv_steps_line_height, stepsLineHeight) - stepsSize = a.getDimension(R.styleable.StepBarView_sbv_steps_size,stepsSize) - stepsTextColor = a.getColor(R.styleable.StepBarView_sbv_steps_text_color,stepsTextColor) - stepsTextSize = a.getDimensionPixelOffset(R.styleable.StepBarView_sbv_steps_text_size, stepsTextSize.toInt()).toFloat() + stepsSize = a.getDimension(R.styleable.StepBarView_sbv_steps_size, stepsSize) + stepsTextColor = + a.getColor(R.styleable.StepBarView_sbv_steps_text_color, stepsTextColor) + stepsTextSize = a.getDimensionPixelOffset( + R.styleable.StepBarView_sbv_steps_text_size, + stepsTextSize.toInt() + ).toFloat() - stepsLineMarginLeft = a.getDimension(R.styleable.StepBarView_sbv_steps_line_margin_left,stepsLineMarginLeft) - stepsLineMarginRight = a.getDimension(R.styleable.StepBarView_sbv_steps_line_margin_right,stepsLineMarginRight) + stepsLineMarginLeft = a.getDimension( + R.styleable.StepBarView_sbv_steps_line_margin_left, + stepsLineMarginLeft + ) + stepsLineMarginRight = a.getDimension( + R.styleable.StepBarView_sbv_steps_line_margin_right, + stepsLineMarginRight + ) - allowTouchStepTo = a.getInt(R.styleable.StepBarView_sbv_allow_touch_step_to,maxCount) + allowTouchStepTo = a.getInt(R.styleable.StepBarView_sbv_allow_touch_step_to, maxCount) showStepIndex = a.getBoolean(R.styleable.StepBarView_sbv_show_step_index, showStepIndex) showStepName = a.getBoolean(R.styleable.StepBarView_sbv_show_step_name, showStepName) - stepsStrokeSize = a.getDimension(R.styleable.StepBarView_sbv_steps_stroke_size,stepsStrokeSize) + stepsStrokeSize = + a.getDimension(R.styleable.StepBarView_sbv_steps_stroke_size, stepsStrokeSize) - stepsStrokeReachedColor = a.getColor(R.styleable.StepBarView_sbv_steps_stroke_reached_color,stepsStrokeReachedColor) - stepsStrokeUnReachedColor = a.getColor(R.styleable.StepBarView_sbv_steps_stroke_unreached_color,stepsStrokeUnReachedColor) - stepsStrokeCurrentColor = a.getColor(R.styleable.StepBarView_sbv_steps_stroke_current_color,stepsStrokeCurrentColor) + stepsStrokeReachedColor = a.getColor( + R.styleable.StepBarView_sbv_steps_stroke_reached_color, + stepsStrokeReachedColor + ) + stepsStrokeUnReachedColor = a.getColor( + R.styleable.StepBarView_sbv_steps_stroke_unreached_color, + stepsStrokeUnReachedColor + ) + stepsStrokeCurrentColor = a.getColor( + R.styleable.StepBarView_sbv_steps_stroke_current_color, + stepsStrokeCurrentColor + ) - showStepStroke = a.getBoolean(R.styleable.StepBarView_sbv_show_step_stroke, showStepStroke) + showStepStroke = + a.getBoolean(R.styleable.StepBarView_sbv_show_step_stroke, showStepStroke) isRtl = a.getBoolean(R.styleable.StepBarView_sbv_is_rtl, isRtl) - isFixedStepsLineWidth = a.getBoolean(R.styleable.StepBarView_sbv_is_fixed_steps_line_width, isFixedStepsLineWidth) + isFixedStepsLineWidth = a.getBoolean( + R.styleable.StepBarView_sbv_is_fixed_steps_line_width, + isFixedStepsLineWidth + ) - stepsLineWidth = a.getDimension(R.styleable.StepBarView_sbv_steps_line_width, stepsLineWidth) + stepsLineWidth = + a.getDimension(R.styleable.StepBarView_sbv_steps_line_width, stepsLineWidth) a.recycle() } @@ -333,7 +371,7 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = val heightMeasureSize = MeasureSpec.getSize(heightMeasureSpec) - val mWidth : Float = if (isFixedStepsLineWidth) { + val mWidth: Float = if (isFixedStepsLineWidth) { calculateDesireWidth() } else { widthMeasureSize.toFloat() @@ -344,33 +382,36 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = } - private fun calculateDesireHeight() : Float = rawHeiht + paddingTop + paddingBottom + titleTextHeight() - private fun calculateDesireWidth() : Float = rawWidth + paddingLeft + paddingRight + private fun calculateDesireHeight(): Float = + rawHeiht + paddingTop + paddingBottom + titleTextHeight() + + private fun calculateDesireWidth(): Float = rawWidth + paddingLeft + paddingRight //To include the steps name height when onMeasure is called //if showStepName is false, this is 0 - private fun titleTextHeight() : Int { + private fun titleTextHeight(): Int { if (!showStepName) return 0 stepsTextPaint.getTextBounds("sample", 0, "sample".length, tmpRect) return tmpRect.height() + NAME_STEP_SEPARATION_PX * 2 } - private fun getHorizontalCirclesPosition() : FloatArray { + + private fun getHorizontalCirclesPosition(): FloatArray { val stepsHorizontalPositions = FloatArray(maxCount) val linesSize = linesWidth - val range = when(isRtl) { - true -> maxCount-1 downTo 0 + val range = when (isRtl) { + true -> maxCount - 1 downTo 0 false -> 0 until maxCount } - for(i in range) { + for (i in range) { //This offset contains step circle and right line val oneStepOffset = stepsSize + stepsLineMarginLeft + linesSize + stepsLineMarginRight val offsetToDraw = paddingLeft + (i * oneStepOffset) - val pos = if(isRtl) (maxCount-1)-i else i + val pos = if (isRtl) (maxCount - 1) - i else i stepsHorizontalPositions[pos] = offsetToDraw + stepsSize / 2 } @@ -381,7 +422,7 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = super.onDraw(canvas) val linesSize = linesWidth - for(i in 0 until maxCount) { + for (i in 0 until maxCount) { if (IS_DEBUG) { Log.d("SS", "drawing $i step") } @@ -389,43 +430,44 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = val xPos = getHorizontalCirclesPosition()[i] //Draw Steps Line - if(i xPos - (stepsSize/2) - stepsLineMarginRight - false -> xPos + (stepsSize/2) + stepsLineMarginLeft - } + when (isRtl) { + true -> xPos - (stepsSize / 2) - stepsLineMarginRight + false -> xPos + (stepsSize / 2) + stepsLineMarginLeft + } val endXPoint = - when(isRtl){ - false -> startXPoint + linesSize - true -> startXPoint - linesSize - } + when (isRtl) { + false -> startXPoint + linesSize + true -> startXPoint - linesSize + } canvas?.drawLine( - startXPoint, - yPos, - endXPoint, - yPos, - stepsLinePaint + startXPoint, + yPos, + endXPoint, + yPos, + stepsLinePaint ) } //Draw Steps Circle - stepsPaint.color = if(i stepsStrokeReachedColor @@ -433,35 +475,38 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = else -> stepsStrokeUnReachedColor } canvas?.drawCircle( - xPos, - yPos, - (stepsSize / 2) - (stepsStrokePaint.strokeWidth / 2), - stepsStrokePaint) + xPos, + yPos, + (stepsSize / 2) - (stepsStrokePaint.strokeWidth / 2), + stepsStrokePaint + ) } //Draw Steps Index - if(showStepIndex) { + if (showStepIndex) { stepsTextPaint.color = stepsTextColor stepsTextPaint.textSize = stepsTextSize val drawingText = (i + 1).toString() stepsTextPaint.getTextBounds(drawingText, 0, drawingText.length, tmpRect) canvas?.drawText( - drawingText, - xPos, - (yPos + (tmpRect.height() / 2)), - stepsTextPaint) + drawingText, + xPos, + (yPos + (tmpRect.height() / 2)), + stepsTextPaint + ) } //Draw Steps Names - if(showStepName) { + if (showStepName) { val name = stepsTitleSetter.getStepTitle(i + 1); stepsTextPaint.getTextBounds(name, 0, name.length, tmpRect) canvas?.drawText( name, xPos, - yPos*2 + tmpRect.height() + NAME_STEP_SEPARATION_PX, - stepsTextPaint) + yPos * 2 + tmpRect.height() + NAME_STEP_SEPARATION_PX, + stepsTextPaint + ) } } @@ -469,7 +514,7 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = private var touchDownPoint = PointF() override fun onTouchEvent(event: MotionEvent?): Boolean { - when(event?.action){ + when (event?.action) { MotionEvent.ACTION_DOWN -> { if (isFixedStepsLineWidth) { touchDownPoint.set(event.x, event.y) @@ -486,7 +531,8 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = } MotionEvent.ACTION_UP -> { //Click Happened if (touchDownPoint.x == event.x && - touchDownPoint.y == event.y) { + touchDownPoint.y == event.y + ) { handleTouchPost(event.x, event.y) } } @@ -496,51 +542,52 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = private fun handleTouchPost(x: Float, y: Float) { val lineSize = linesWidth - for(i in 0 until getHorizontalCirclesPosition().size){ + for (i in 0 until getHorizontalCirclesPosition().size) { //Disallow touch more than allowTouchStepTo - if(i >= allowTouchStepTo) continue + if (i >= allowTouchStepTo) continue //Disallow touch user prevent steps - if(!allowSelectStep.allowSelectStep(i+1)) continue + if (!allowSelectStep.allowSelectStep(i + 1)) continue val xDotPos = getHorizontalCirclesPosition()[i] val yDotPos = yPos - val stepHalf = stepsSize/2 + val stepHalf = stepsSize / 2 //verticalExtraSpace is the extra space for vertical touching - val verticalExtraSpace = (rawHeiht*2) + val verticalExtraSpace = (rawHeiht * 2) val leftArea = - when(isRtl){ - false -> xDotPos-stepHalf-lineSize-stepsLineMarginRight - true -> xDotPos-stepHalf-stepsLineMarginRight - } + when (isRtl) { + false -> xDotPos - stepHalf - lineSize - stepsLineMarginRight + true -> xDotPos - stepHalf - stepsLineMarginRight + } val rightArea = - when(isRtl){ - false -> xDotPos+stepHalf+stepsLineMarginLeft - true -> xDotPos+stepHalf+stepsLineMarginLeft+lineSize - } + when (isRtl) { + false -> xDotPos + stepHalf + stepsLineMarginLeft + true -> xDotPos + stepHalf + stepsLineMarginLeft + lineSize + } val stepArea = RectF( - leftArea, - yDotPos-stepHalf-verticalExtraSpace, - rightArea, - yDotPos+stepHalf+verticalExtraSpace + leftArea, + yDotPos - stepHalf - verticalExtraSpace, + rightArea, + yDotPos + stepHalf + verticalExtraSpace ) - if(stepArea.contains(x, y)){ + if (stepArea.contains(x, y)) { //Touched Step (i+1) - reachedStep = (i+1) + reachedStep = (i + 1) } } } - override fun onSaveInstanceState(): Parcelable { - val superState = super.onSaveInstanceState() + override fun onSaveInstanceState(): Parcelable? { + val superState = super.onSaveInstanceState() ?: return null + val ss = SavedState(superState) ss.maxCount = maxCount ss.reachedStep = reachedStep @@ -590,36 +637,36 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = isRtl = savedState.isRtl } - class SavedState : BaseSavedState{ - companion object CREATOR : Parcelable.Creator{ + class SavedState : BaseSavedState { + companion object CREATOR : Parcelable.Creator { override fun createFromParcel(source: Parcel?) = SavedState(source) override fun newArray(size: Int) = arrayOfNulls(size) } - var maxCount : Int = 1 + var maxCount: Int = 1 var reachedStep: Int = 1 - var stepsReachedColor : Int = 1 - var stepsUnreachedColor : Int = 1 + var stepsReachedColor: Int = 1 + var stepsUnreachedColor: Int = 1 var stepsLineReachedColor: Int = 1 var stepsLineUnreachedColor: Int = 1 var stepsLineHeight: Float = 1f var stepsSize: Float = 1f var stepsTextColor: Int = 1 - var stepsTextSize : Float = 1f - var stepsLineMarginLeft : Float = 1f - var stepsLineMarginRight : Float = 1f - var allowTouchStepTo : Int = 1 + var stepsTextSize: Float = 1f + var stepsLineMarginLeft: Float = 1f + var stepsLineMarginRight: Float = 1f + var allowTouchStepTo: Int = 1 var showStepIndex: Boolean = true - var stepsStrokeSize : Float = 1f - var stepsStrokeReachedColor : Int = 1 - var stepsStrokeUnReachedColor : Int = 1 - var stepsStrokeCurrentColor : Int = 1 - var showStepStroke : Boolean = false - var isRtl : Boolean = false + var stepsStrokeSize: Float = 1f + var stepsStrokeReachedColor: Int = 1 + var stepsStrokeUnReachedColor: Int = 1 + var stepsStrokeCurrentColor: Int = 1 + var showStepStroke: Boolean = false + var isRtl: Boolean = false constructor(parcelable: Parcelable) : super(parcelable) - constructor(parcel : Parcel?) : super(parcel){ + constructor(parcel: Parcel?) : super(parcel) { parcel?.let { maxCount = it.readInt() reachedStep = it.readInt() @@ -634,13 +681,13 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = stepsLineMarginLeft = it.readFloat() stepsLineMarginRight = it.readFloat() allowTouchStepTo = it.readInt() - showStepIndex = it.readInt()==1 + showStepIndex = it.readInt() == 1 stepsStrokeSize = it.readFloat() stepsStrokeReachedColor = it.readInt() stepsStrokeUnReachedColor = it.readInt() stepsStrokeCurrentColor = it.readInt() - showStepStroke = it.readInt()==1 - isRtl = it.readInt()==1 + showStepStroke = it.readInt() == 1 + isRtl = it.readInt() == 1 } } @@ -662,26 +709,26 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int = it.writeFloat(stepsLineMarginLeft) it.writeFloat(stepsLineMarginRight) it.writeInt(allowTouchStepTo) - it.writeInt(if(showStepIndex) 1 else 0) + it.writeInt(if (showStepIndex) 1 else 0) it.writeFloat(stepsStrokeSize) it.writeInt(stepsStrokeReachedColor) it.writeInt(stepsStrokeUnReachedColor) it.writeInt(stepsStrokeCurrentColor) - it.writeInt(if(showStepStroke) 1 else 0) - it.writeInt(if(isRtl) 1 else 0) + it.writeInt(if (showStepStroke) 1 else 0) + it.writeInt(if (isRtl) 1 else 0) } } } - interface OnStepChangeListener{ - fun onStepChanged(currentStep:Int) + interface OnStepChangeListener { + fun onStepChanged(currentStep: Int) } - interface AllowSelectStep{ - fun allowSelectStep(step: Int) : Boolean + interface AllowSelectStep { + fun allowSelectStep(step: Int): Boolean } - interface StepsTitleSetter{ - fun getStepTitle(step: Int) : String + interface StepsTitleSetter { + fun getStepTitle(step: Int): String } } \ No newline at end of file