Skip to content

Commit 8fff377

Browse files
authored
Merge pull request #8 from ozgurg/dev
release: v2.1.1
2 parents f4083b3 + 64e1ffb commit 8fff377

File tree

4 files changed

+51
-44
lines changed

4 files changed

+51
-44
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,5 @@ lint/generated/
7373
lint/outputs/
7474
lint/tmp/
7575
# lint/reports/
76+
77+
.DS_Store

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ToggleIconView is a collection library of animated two-stage toggle icons for An
1111
### JitPack repository
1212

1313
```gradle
14-
// build.gradle Project
14+
// Project level build.gradle
1515
allprojects {
1616
repositories {
1717
...
@@ -23,7 +23,7 @@ allprojects {
2323
If the above doesn't work, try the following:
2424

2525
```gradle
26-
// settings.gradle Project
26+
// settings.gradle
2727
...
2828
dependencyResolutionManagement {
2929
...
@@ -37,9 +37,9 @@ dependencyResolutionManagement {
3737

3838
### Implementation
3939
```gradle
40-
// build.gradle App
40+
// App level build.gradle
4141
dependencies {
42-
implementation "com.github.ozgurg:ToggleIconView:2.1.0"
42+
implementation "com.github.ozgurg:ToggleIconView:2.1.1"
4343
}
4444
```
4545

@@ -75,7 +75,7 @@ anything you can with `AppCompatImageView`.
7575

7676
| Event | Description |
7777
|------------------------------------------------------------------------|----------------------------------------------------|
78-
| `onCheckedChanged(toggleIconView: ToggleIconView, isChecked: Boolean)` | Triggers when the ticked state of the icon changed |
78+
| `onCheckedChanged(toggleIconView: ToggleIconView, isChecked: Boolean)` | Triggers when the checked state of the icon changed |
7979

8080
## Built-in icons
8181

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ afterEvaluate {
3939

4040
groupId = "com.github.ozgurg"
4141
artifactId = "toggle-icon-view"
42-
version = "2.1.0"
42+
version = "2.1.1"
4343
}
4444
}
4545
}

lib/src/main/java/og/android/lib/toggleiconview/ToggleIconView.kt

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,88 @@ package og.android.lib.toggleiconview
22

33
import android.content.Context
44
import android.util.AttributeSet
5+
import androidx.annotation.DrawableRes
56
import androidx.appcompat.widget.AppCompatImageView
67
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
78

89
abstract class ToggleIconView @JvmOverloads constructor(
910
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0,
10-
checkedDrawableRes: Int,
11-
uncheckedDrawableRes: Int,
11+
@DrawableRes checkedDrawableResId: Int,
12+
@DrawableRes uncheckedDrawableResId: Int,
1213
) : AppCompatImageView(context, attrs, defStyleAttr) {
13-
private var onCheckedChangeListener: ((toggleIconView: ToggleIconView, isChecked: Boolean) -> Unit)? =
14-
null
14+
private lateinit var checkedDrawable: AnimatedVectorDrawableCompat
15+
private lateinit var uncheckedDrawable: AnimatedVectorDrawableCompat
1516

16-
private var checkedDrawable: AnimatedVectorDrawableCompat? = null
17-
private var uncheckedDrawable: AnimatedVectorDrawableCompat? = null
18-
private var isChecked = false
17+
private var isChecked: Boolean = false
18+
private var onCheckedChangeListener: ((toggleIconView: ToggleIconView, isChecked: Boolean) -> Unit)? = null
1919

2020
init {
21-
setCheckedDrawable(checkedDrawableRes)
22-
setUncheckedDrawable(uncheckedDrawableRes)
21+
setCheckedDrawable(checkedDrawableResId)
22+
setUncheckedDrawable(uncheckedDrawableResId)
2323
handleAttributes(attrs, defStyleAttr)
2424
}
2525

26-
fun toggle() {
27-
setChecked(!isChecked)
26+
private fun setCheckedDrawable(@DrawableRes drawableResId: Int) {
27+
checkedDrawable = AnimatedVectorDrawableCompat.create(context, drawableResId)!!
2828
}
2929

30-
fun isChecked(): Boolean {
31-
return isChecked
30+
private fun setUncheckedDrawable(@DrawableRes drawableResId: Int) {
31+
uncheckedDrawable = AnimatedVectorDrawableCompat.create(context, drawableResId)!!
3232
}
3333

34-
fun setChecked(checked: Boolean) {
35-
handleCheckState(checked)
36-
isChecked = checked
37-
38-
onCheckedChangeListener?.invoke(this, checked)
39-
}
40-
41-
interface OnCheckedChangeListener {
42-
fun onCheckedChanged(toggleIconView: ToggleIconView, isChecked: Boolean)
43-
}
44-
45-
private fun handleCheckedState() {
34+
private fun setAndAnimateCheckedDrawable() {
4635
setImageDrawable(checkedDrawable)
47-
checkedDrawable!!.start()
36+
checkedDrawable.start()
4837
}
4938

50-
private fun handleUncheckedState() {
39+
private fun setAndAnimateUncheckedDrawable() {
5140
setImageDrawable(uncheckedDrawable)
52-
uncheckedDrawable!!.start()
41+
uncheckedDrawable.start()
5342
}
5443

55-
private fun handleCheckState(checked: Boolean) {
44+
private fun setAndAnimateDrawableByCheckState(checked: Boolean) {
5645
if (checked) {
57-
handleCheckedState()
46+
setAndAnimateCheckedDrawable()
5847
} else {
59-
handleUncheckedState()
48+
setAndAnimateUncheckedDrawable()
6049
}
6150
}
6251

52+
private fun handleCheckState(checked: Boolean) {
53+
setAndAnimateDrawableByCheckState(checked)
54+
isChecked = checked
55+
}
56+
6357
private fun handleAttributes(attrs: AttributeSet? = null, defStyleAttr: Int = 0) {
64-
val typedArray =
65-
context.theme.obtainStyledAttributes(attrs, R.styleable.ToggleIconView, defStyleAttr, 0)
58+
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.ToggleIconView, defStyleAttr, 0)
6659

6760
try {
6861
// app:checked
6962
val checked = typedArray.getBoolean(R.styleable.ToggleIconView_checked, isChecked)
70-
setChecked(checked)
63+
64+
handleCheckState(checked)
7165
} finally {
7266
typedArray.recycle()
7367
}
7468
}
7569

76-
private fun setCheckedDrawable(checkedDrawableRes: Int) {
77-
checkedDrawable = AnimatedVectorDrawableCompat.create(context, checkedDrawableRes)
70+
fun toggle() {
71+
setChecked(!isChecked)
7872
}
7973

80-
private fun setUncheckedDrawable(uncheckedDrawableRes: Int) {
81-
uncheckedDrawable = AnimatedVectorDrawableCompat.create(context, uncheckedDrawableRes)
74+
fun isChecked(): Boolean {
75+
return isChecked
76+
}
77+
78+
fun setChecked(checked: Boolean) {
79+
// We won't update the status if the status is the same as the current status
80+
// This is to prevent the animation from restarting when the state is set again
81+
if (checked == isChecked) {
82+
return
83+
}
84+
85+
handleCheckState(checked)
86+
onCheckedChangeListener?.invoke(this, checked)
8287
}
8388

8489
open fun setOnCheckedChangeListener(listener: (toggleIconView: ToggleIconView, isChecked: Boolean) -> Unit) {

0 commit comments

Comments
 (0)