Skip to content

Commit

Permalink
Make check state get/set
Browse files Browse the repository at this point in the history
  • Loading branch information
ozgurg committed Jul 17, 2022
1 parent 57791c4 commit 418b5ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
26 changes: 12 additions & 14 deletions lib/src/main/java/og/android/lib/toggleiconview/ToggleIconView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,22 @@ abstract class ToggleIconView @JvmOverloads constructor(
}

fun toggle() {
setChecked(!mIsChecked)
isChecked = !isChecked
}

fun isChecked(): Boolean {
return mIsChecked
}

fun setChecked(isChecked: Boolean) {
// We won't update the status if the status is the same as the current status
// This is to prevent the animation from restarting when the state is set again
if (isChecked == mIsChecked) {
return
var isChecked: Boolean
get() = mIsChecked
set(isChecked) {
// We will not update the state if the state is the same as the current state
// This is to prevent the animation from restarting when the state is set again
if (isChecked == mIsChecked) {
return
}

handleCheckState(isChecked)
mOnCheckedChangeListener?.invoke(this, isChecked)
}

handleCheckState(isChecked)
mOnCheckedChangeListener?.invoke(this, isChecked)
}

open fun setOnCheckedChangeListener(listener: (view: ToggleIconView, isChecked: Boolean) -> Unit) {
mOnCheckedChangeListener = listener
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ class MainActivity : AppCompatActivity() {
toggleIconView.toggle()
}

toggleIconView.setOnLongClickListener {
// Just to use its setter
toggleIconView.isChecked = !toggleIconView.isChecked
false
}

toggleIconView.setOnCheckedChangeListener { view: ToggleIconView, _: Boolean ->
val value = "[${view::class.qualifiedName.toString()}:onCheckedChanged] isChecked: ${toggleIconView.isChecked()}"
val value = "[${view::class.qualifiedName.toString()}:onCheckedChanged] isChecked: ${toggleIconView.isChecked}"
Log.d("TOGGLEICONVIEW_SAMPLE", value)
}
}
Expand Down

0 comments on commit 418b5ab

Please sign in to comment.