Skip to content

Commit

Permalink
Merge pull request #28 from minibugdev/feature/show-number
Browse files Browse the repository at this point in the history
Feature show counter number
  • Loading branch information
minibugdev authored Dec 29, 2020
2 parents 631c14c + 2f3ae63 commit fc58797
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 38 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Drawable Badge
[![CircleCI](https://circleci.com/gh/minibugdev/DrawableBadge.svg?style=shield)](https://circleci.com/gh/minibugdev/DrawableBadge)
[![Release](https://jitpack.io/v/minibugdev/DrawableBadge.svg)](https://jitpack.io/#minibugdev/DrawableBadge/2.0.0)
[![Release](https://jitpack.io/v/minibugdev/DrawableBadge.svg)](https://jitpack.io/#minibugdev/DrawableBadge/2.0.1)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/minibugdev/DrawableBadge/master/LICENSE)

Drawable Badge is a android library for adding badges to drawables.
Expand All @@ -21,6 +21,7 @@ val drawable = DrawableBadge.Builder(context)
.badgeBorderColor(R.color.badgeBorderColor)
.badgeBorderSize(R.dimen.badge_border_size)
.maximumCounter(99)
.showCounter(true)
.build()
.get(99)
```
Expand All @@ -42,6 +43,7 @@ imageViewBadge.setImageDrawable(drawable)
- `badgeBorderColor`: Badge border color resource id , default `#FFFFFF`.
- `badgeBorderSize`: Badge border size supported `@DimenRes` or `@Px`, default `0.5dp`.
- `maximumCounter`: Maximum counter text will append with `+`, default and not more than `99`.
- `showCounter`: Set visible of badge counter number, default `true`.

#### Deprecated
- `badgePosition`: Position of Badge which need to added.
Expand All @@ -54,7 +56,7 @@ repositories {
```

``` groovy
compile 'com.github.minibugdev:drawablebadge:2.0.0'
compile 'com.github.minibugdev:drawablebadge:2.0.1'
```


Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
buildscript {
ext.kotlin_version = '1.3.61'
ext.androidx_version = '1.1.0'
ext.kotlin_version = '1.4.21'
ext.androidx_version = '1.2.0'
ext.android_annotation_version = '1.1.0'

ext.test_runner_version = '1.2.0'
ext.junit_version = '4.12'
ext.mockk_version = '1.9.2'
ext.robolectric_version = '4.3.1'
ext.robolectric_version = '4.4'

repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

Expand Down
6 changes: 3 additions & 3 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.minibugdev"
minSdkVersion 15
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class DemoActivity : AppCompatActivity() {
.badgeBorderSize(R.dimen.badge_border_size)
.maximumCounter(99)
.badgeMargin(10f)
.showCounter(true)
.build()
.get(number)
.let { drawable -> imageViewBadge.setImageDrawable(drawable) }
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# The setting is particularly useful for tweaking memory settings.
android.enableJetifier=true
android.useAndroidX=true
android.enableUnitTestBinaryResources=true
org.gradle.jvmargs=-Xmx1536m

# When configured, Gradle will run in incubating parallel mode.
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Jun 24 11:14:39 ICT 2019
#Tue Dec 29 15:08:23 ICT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
10 changes: 5 additions & 5 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.minibugdev'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
minSdkVersion 14
targetSdkVersion 29
versionCode 5
versionName "2.0.0"
targetSdkVersion 30
versionCode 6
versionName "2.0.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
49 changes: 28 additions & 21 deletions library/src/main/java/com/minibugdev/drawablebadge/DrawableBadge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class DrawableBadge private constructor(val context: Context,
val badgeMargin: Float,
val bitmap: Bitmap,
val isShowBorder: Boolean,
val maximumCounter: Int) {
val maximumCounter: Int,
val isShowCounter: Boolean,){

class Builder(private val context: Context) {

Expand All @@ -35,6 +36,7 @@ class DrawableBadge private constructor(val context: Context,
private var bitmap: Bitmap? = null
private var isShowBorder: Boolean? = null
private var maximumCounter: Int? = null
private var isShowCounter: Boolean? = null

private fun createBitmapFromDrawable(drawable: Drawable): Bitmap {
val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
Expand Down Expand Up @@ -100,6 +102,8 @@ class DrawableBadge private constructor(val context: Context,

fun maximumCounter(maximumCounter: Int) = apply { this.maximumCounter = maximumCounter }

fun showCounter(isShowCounter: Boolean) = apply { this.isShowCounter = isShowCounter }

fun build(): DrawableBadge {
if (bitmap == null) throw IllegalArgumentException("Badge drawable/bitmap can not be null.")
if (badgeSize == null) badgeSize(R.dimen.default_badge_size)
Expand All @@ -110,6 +114,7 @@ class DrawableBadge private constructor(val context: Context,
if (badgeGravity == null) badgeGravity(Gravity.TOP or Gravity.END)
if (isShowBorder == null) showBorder(true)
if (maximumCounter == null) maximumCounter(MAXIMUM_COUNT)
if (isShowCounter == null) showCounter(true)

return DrawableBadge(
context = context,
Expand All @@ -122,7 +127,8 @@ class DrawableBadge private constructor(val context: Context,
badgeGravity = badgeGravity!!,
badgeMargin = badgeMargin ?: 0.0f,
isShowBorder = isShowBorder!!,
maximumCounter = maximumCounter!!)
maximumCounter = maximumCounter!!,
isShowCounter = isShowCounter!!)
}
}

Expand Down Expand Up @@ -162,27 +168,28 @@ class DrawableBadge private constructor(val context: Context,
canvas.drawOval(badgeRect, paintBorder)
}

val textSize: Float
val text: String
val max = if (maximumCounter > MAXIMUM_COUNT) MAXIMUM_COUNT else maximumCounter
if (counter > max) {
textSize = badgeRect.height() * 0.45f
text = "$max+"
}
else {
textSize = badgeRect.height() * 0.55f
text = counter.toString()
}
if(isShowCounter) {
val textSize: Float
val text: String
val max = if (maximumCounter > MAXIMUM_COUNT) MAXIMUM_COUNT else maximumCounter
if (counter > max) {
textSize = badgeRect.height() * 0.45f
text = "$max+"
} else {
textSize = badgeRect.height() * 0.55f
text = counter.toString()
}

val textPaint = TextPaint().apply {
this.isAntiAlias = true
this.color = textColor
this.textSize = textSize
}
val textPaint = TextPaint().apply {
this.isAntiAlias = true
this.color = textColor
this.textSize = textSize
}

val x = badgeRect.centerX() - (textPaint.measureText(text) / 2f)
val y = badgeRect.centerY() - (textPaint.ascent() + textPaint.descent()) * 0.5f
canvas.drawText(text, x, y, textPaint)
val x = badgeRect.centerX() - (textPaint.measureText(text) / 2f)
val y = badgeRect.centerY() - (textPaint.ascent() + textPaint.descent()) * 0.5f
canvas.drawText(text, x, y, textPaint)
}

return BitmapDrawable(resources, output)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class DrawableBadgeBuilderTest {
.badgeGravity(Gravity.CENTER_VERTICAL or Gravity.END)
.showBorder(false)
.maximumCounter(50)
.showCounter(false)
.build()

val expectedBadgeColor = ContextCompat.getColor(context, android.R.color.holo_red_dark)
Expand All @@ -83,5 +84,6 @@ class DrawableBadgeBuilderTest {
assertEquals(Gravity.CENTER_VERTICAL or Gravity.END, actual.badgeGravity)
assertEquals(false, actual.isShowBorder)
assertEquals(50, actual.maximumCounter)
assertEquals(false, actual.isShowCounter)
}
}

0 comments on commit fc58797

Please sign in to comment.