From f5a2296b84b5f4506f8d2673a161edfddf179cae Mon Sep 17 00:00:00 2001 From: carlosmuvi Date: Fri, 23 Jun 2017 13:21:57 -0700 Subject: [PATCH] Migrated to kotlin --- .idea/kotlinc.xml | 7 + build.gradle | 2 + library/build.gradle | 17 +- .../segmentedprogressbar/PropertiesModel.kt | 10 + .../SegmentedProgressBar.java | 204 ------------------ .../SegmentedProgressBar.kt | 198 +++++++++++++++++ .../main/res/values/segmentedprogressbar.xml | 6 + 7 files changed, 238 insertions(+), 206 deletions(-) create mode 100644 .idea/kotlinc.xml create mode 100644 library/src/main/java/com/carlosmuvi/segmentedprogressbar/PropertiesModel.kt delete mode 100644 library/src/main/java/com/carlosmuvi/segmentedprogressbar/SegmentedProgressBar.java create mode 100644 library/src/main/java/com/carlosmuvi/segmentedprogressbar/SegmentedProgressBar.kt create mode 100644 library/src/main/res/values/segmentedprogressbar.xml diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..1c24f9a --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index f82ef2b..cb8cd4d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,14 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext.kotlin_version = '1.1.3' repositories { jcenter() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/library/build.gradle b/library/build.gradle index 95faa00..3a57ef5 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' android { compileSdkVersion 24 @@ -21,13 +22,25 @@ android { } dependencies { + //region production libraries compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.android.support:appcompat-v7:25.3.1' + compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" + //endregion + + //region test libraries + testCompile 'junit:junit:4.12' + //endregion + + //region android test libraries androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:25.3.1' - testCompile 'junit:junit:4.12' + //endregion } apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle' +repositories { + mavenCentral() +} diff --git a/library/src/main/java/com/carlosmuvi/segmentedprogressbar/PropertiesModel.kt b/library/src/main/java/com/carlosmuvi/segmentedprogressbar/PropertiesModel.kt new file mode 100644 index 0000000..b6bbbe5 --- /dev/null +++ b/library/src/main/java/com/carlosmuvi/segmentedprogressbar/PropertiesModel.kt @@ -0,0 +1,10 @@ +package com.carlosmuvi.segmentedprogressbar + +import android.graphics.Color + + +data class PropertiesModel( + var segmentCount : Int = 5, + var containerColor : Int = Color.LTGRAY, + var fillColor : Int = Color.BLUE +) \ No newline at end of file diff --git a/library/src/main/java/com/carlosmuvi/segmentedprogressbar/SegmentedProgressBar.java b/library/src/main/java/com/carlosmuvi/segmentedprogressbar/SegmentedProgressBar.java deleted file mode 100644 index add5b9b..0000000 --- a/library/src/main/java/com/carlosmuvi/segmentedprogressbar/SegmentedProgressBar.java +++ /dev/null @@ -1,204 +0,0 @@ -package com.carlosmuvi.segmentedprogressbar; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Path; -import android.support.annotation.ColorInt; -import android.util.AttributeSet; -import android.view.View; - -/** - * Created by carlosmuvi on 02/09/16. - */ - -public class SegmentedProgressBar extends View { - - private int segmentCount = 5; - private int lastCompletedSegment = 0; - private int currentSegmentProgressInPx = 0; - - private Paint containerRectanglePaint; - private Paint fillRectanglePaint; - private DrawingTimer drawingTimer; - - public SegmentedProgressBar(Context context) { - super(context); - initView(); - } - - public SegmentedProgressBar(Context context, AttributeSet attrs) { - super(context, attrs); - initView(); - } - - private void initView() { - initDrawingTimer(); - containerRectanglePaint = buildContainerRectanglePaint(Color.LTGRAY); - fillRectanglePaint = buildFillRectanglePaint(Color.RED); - } - - private void initDrawingTimer() { - drawingTimer = new DrawingTimer(); - drawingTimer.setListener(new DrawingTimer.Listener() { - @Override public void onTick(int currentTicks, int totalTicks) { - - int segmentWidth = getSegmentWidth(); - - currentSegmentProgressInPx = currentTicks * segmentWidth / totalTicks; - if (totalTicks <= currentTicks) { - lastCompletedSegment++; - currentSegmentProgressInPx = 0; - } - invalidate(); - } - }); - } - - @Override protected void onDraw(Canvas canvas) { - super.onDraw(canvas); - drawContainerRectangles(canvas); - drawCompletedRectangles(canvas); - drawCurrentRectangle(canvas); - } - - /* - EXPOSED ATTRIBUTE METHODS - */ - - public void setContainerColor(@ColorInt int color) { - containerRectanglePaint = buildContainerRectanglePaint(color); - } - - public void setFillColor(@ColorInt int color) { - fillRectanglePaint = buildFillRectanglePaint(color); - } - - public void setSegmentCount(int segmentCount) { - this.segmentCount = segmentCount; - } - - /* - EXPOSED ACTION METHODS - */ - - public void playSegment(long timeInMilliseconds) { - if (!drawingTimer.isRunning()) { - drawingTimer.start(timeInMilliseconds); - } - } - - public void pause() { - drawingTimer.pause(); - } - - public void setCompletedSegments(int completedSegments) { - if (completedSegments <= segmentCount) { - lastCompletedSegment = completedSegments; - invalidate(); - } - } - - /* - PRIVATE METHODS - */ - - private void drawContainerRectangles(Canvas canvas) { - int segmentWidth = getSegmentWidth(); - - int leftX = 0; - int rightX = leftX + segmentWidth; - int topY = 0; - int botY = getHeight(); - - for (int i = 0; i < segmentCount; i++) { - drawRoundedRect(canvas, leftX, topY, rightX, botY, containerRectanglePaint); - leftX = leftX + segmentWidth + getSegmentGapWidth(); - rightX = leftX + segmentWidth; - } - } - - private void drawCompletedRectangles(Canvas canvas) { - int segmentWidth = getSegmentWidth(); - - int leftX = 0; - int rightX = leftX + segmentWidth; - int topY = 0; - int botY = getHeight(); - - for (int i = 0; i < lastCompletedSegment; i++) { - drawRoundedRect(canvas, leftX, topY, rightX, botY, fillRectanglePaint); - leftX = leftX + segmentWidth + getSegmentGapWidth(); - rightX = leftX + segmentWidth; - } - } - - private void drawCurrentRectangle(Canvas canvas) { - int segmentWidth = getSegmentWidth(); - - int leftX = lastCompletedSegment * (segmentWidth + getSegmentGapWidth()); - int rightX = leftX + currentSegmentProgressInPx; - int topY = 0; - int botY = getHeight(); - drawRoundedRect(canvas, leftX, topY, rightX, botY, fillRectanglePaint); - } - - private void drawRoundedRect(Canvas canvas, float left, float top, float right, float bottom, Paint paint) { - - Path path = new Path(); - float rx = 6; - if (rx < 0) rx = 0; - float ry = 6; - if (ry < 0) ry = 0; - float width = right - left; - float height = bottom - top; - if (rx > width / 2) rx = width / 2; - if (ry > height / 2) ry = height / 2; - float widthMinusCorners = (width - (2 * rx)); - float heightMinusCorners = (height - (2 * ry)); - - path.moveTo(right, top + ry); - path.rQuadTo(0, -ry, -rx, -ry);//top-right corner - path.rLineTo(-widthMinusCorners, 0); - path.rQuadTo(-rx, 0, -rx, ry); //top-left corner - path.rLineTo(0, heightMinusCorners); - - path.rQuadTo(0, ry, rx, ry);//bottom-left corner - path.rLineTo(widthMinusCorners, 0); - path.rQuadTo(rx, 0, rx, -ry); //bottom-right corner - - path.rLineTo(0, -heightMinusCorners); - - path.close();//Given close, last lineto can be removed. - - canvas.drawPath(path, paint); - } - - private Paint buildFillRectanglePaint(@ColorInt int color) { - Paint paint = new Paint(); - paint.setColor(color); - paint.setStyle(Paint.Style.FILL); - return paint; - } - - private Paint buildContainerRectanglePaint(@ColorInt int color) { - Paint paint = new Paint(); - paint.setColor(color); - paint.setStyle(Paint.Style.FILL); - return paint; - } - - private int getSegmentWidth() { - return (getWidth() / segmentCount) - getSegmentGapWidth(); - } - - private int getSegmentGapWidth() { - return dpToPx(1); - } - - private int dpToPx(int valueInDp) { - float density = getContext().getResources().getDisplayMetrics().density; - return (int) (valueInDp * density); - } -} diff --git a/library/src/main/java/com/carlosmuvi/segmentedprogressbar/SegmentedProgressBar.kt b/library/src/main/java/com/carlosmuvi/segmentedprogressbar/SegmentedProgressBar.kt new file mode 100644 index 0000000..07494a2 --- /dev/null +++ b/library/src/main/java/com/carlosmuvi/segmentedprogressbar/SegmentedProgressBar.kt @@ -0,0 +1,198 @@ +package com.carlosmuvi.segmentedprogressbar + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.Path +import android.support.annotation.ColorInt +import android.util.AttributeSet +import android.view.View + +/** + * Created by carlosmuvi on 02/09/16. + */ + +class SegmentedProgressBar : View { + + private var lastCompletedSegment = 0 + private var currentSegmentProgressInPx = 0 + + private lateinit var containerRectanglePaint: Paint + private lateinit var fillRectanglePaint: Paint + private lateinit var drawingTimer: DrawingTimer + private val propertiesModel = PropertiesModel() + + constructor(context: Context) : super(context) { + initView() + } + + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { + initView() + } + + private fun initView() { + initDrawingTimer() + containerRectanglePaint = buildContainerRectanglePaint(propertiesModel.containerColor) + fillRectanglePaint = buildFillRectanglePaint(propertiesModel.fillColor) + } + + private fun initDrawingTimer() { + drawingTimer = DrawingTimer() + drawingTimer.setListener { currentTicks, totalTicks -> + val segmentWidth = segmentWidth + + currentSegmentProgressInPx = currentTicks * segmentWidth / totalTicks + if (totalTicks <= currentTicks) { + lastCompletedSegment++ + currentSegmentProgressInPx = 0 + } + invalidate() + } + } + + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + drawContainerRectangles(canvas) + drawCompletedRectangles(canvas) + drawCurrentRectangle(canvas) + } + + /* + EXPOSED ATTRIBUTE METHODS + */ + + fun setContainerColor(@ColorInt color: Int) { + containerRectanglePaint = buildContainerRectanglePaint(color) + } + + fun setFillColor(@ColorInt color: Int) { + fillRectanglePaint = buildFillRectanglePaint(color) + } + + fun setSegmentCount(segmentCount: Int) { + propertiesModel.segmentCount = segmentCount + } + + /* + EXPOSED ACTION METHODS + */ + + fun playSegment(timeInMilliseconds: Long) { + if (!drawingTimer.isRunning) { + drawingTimer.start(timeInMilliseconds) + } + } + + fun pause() { + drawingTimer.pause() + } + + fun setCompletedSegments(completedSegments: Int) { + if (completedSegments <= propertiesModel.segmentCount) { + lastCompletedSegment = completedSegments + invalidate() + } + } + + /* + PRIVATE METHODS + */ + private fun drawContainerRectangles(canvas: Canvas) { + val segmentWidth = segmentWidth + + var leftX = 0 + var rightX = leftX + segmentWidth + val topY = 0 + val botY = height + + for (i in 0..propertiesModel.segmentCount - 1) { + drawRoundedRect(canvas, leftX.toFloat(), topY.toFloat(), rightX.toFloat(), botY.toFloat(), + containerRectanglePaint) + leftX = leftX + segmentWidth + segmentGapWidth + rightX = leftX + segmentWidth + } + } + + private fun drawCompletedRectangles(canvas: Canvas) { + val segmentWidth = segmentWidth + + var leftX = 0 + var rightX = leftX + segmentWidth + val topY = 0 + val botY = height + + for (i in 0..lastCompletedSegment - 1) { + drawRoundedRect(canvas, leftX.toFloat(), topY.toFloat(), rightX.toFloat(), botY.toFloat(), fillRectanglePaint) + leftX = leftX + segmentWidth + segmentGapWidth + rightX = leftX + segmentWidth + } + } + + private fun drawCurrentRectangle(canvas: Canvas) { + val segmentWidth = segmentWidth + + val leftX = lastCompletedSegment * (segmentWidth + segmentGapWidth) + val rightX = leftX + currentSegmentProgressInPx + val topY = 0 + val botY = height + drawRoundedRect(canvas, leftX.toFloat(), topY.toFloat(), rightX.toFloat(), botY.toFloat(), fillRectanglePaint) + } + + private fun drawRoundedRect(canvas: Canvas, left: Float, top: Float, right: Float, bottom: Float, paint: Paint) { + + val path = Path() + var rx = 6f + if (rx < 0) rx = 0f + var ry = 6f + if (ry < 0) ry = 0f + val width = right - left + val height = bottom - top + if (rx > width / 2) rx = width / 2 + if (ry > height / 2) ry = height / 2 + val widthMinusCorners = width - 2 * rx + val heightMinusCorners = height - 2 * ry + + with(path) { + moveTo(right, top + ry) + rQuadTo(0f, -ry, -rx, -ry)//top-right corner + rLineTo(-widthMinusCorners, 0f) + rQuadTo(-rx, 0f, -rx, ry) //top-left corner + rLineTo(0f, heightMinusCorners) + + rQuadTo(0f, ry, rx, ry)//bottom-left corner + rLineTo(widthMinusCorners, 0f) + rQuadTo(rx, 0f, rx, -ry) //bottom-right corner + + rLineTo(0f, -heightMinusCorners) + + close() + } + + canvas.drawPath(path, paint) + } + + private fun buildFillRectanglePaint(@ColorInt color: Int): Paint { + val paint = Paint() + paint.color = color + paint.style = Paint.Style.FILL + return paint + } + + private fun buildContainerRectanglePaint(@ColorInt color: Int): Paint { + val paint = Paint() + paint.color = color + paint.style = Paint.Style.FILL + return paint + } + + private val segmentWidth: Int + get() = width / propertiesModel.segmentCount - segmentGapWidth + + private val segmentGapWidth: Int + get() = dpToPx(1) + + private fun dpToPx(valueInDp: Int): Int { + val density = context.resources.displayMetrics.density + return (valueInDp * density).toInt() + } +} diff --git a/library/src/main/res/values/segmentedprogressbar.xml b/library/src/main/res/values/segmentedprogressbar.xml new file mode 100644 index 0000000..dab41da --- /dev/null +++ b/library/src/main/res/values/segmentedprogressbar.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file