Skip to content

Commit

Permalink
refactored properly
Browse files Browse the repository at this point in the history
  • Loading branch information
raheemadamboev committed Dec 19, 2021
1 parent fda9793 commit 1cf2272
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@ class GravityImageRadioButton : RelativeLayout, GravityRadioCheckable {

companion object {
private val DEFAULT_PRESSED_BACKGROUND_PRESSED_ID = R.drawable.background_shape_preset_button_pressed
private const val DEFAULT_UNPRESSED_TEXT_COLOR = Color.BLACK
private const val DEFAULT_PRESSED_TEXT_COLOR = Color.WHITE
}

private lateinit var textT: TextView
private lateinit var imageI: ImageView
private var textT: TextView? = null
private var imageI: ImageView? = null

private var text = ""
private var unpressedTextColor = DEFAULT_UNPRESSED_TEXT_COLOR
private var pressedTextColor = DEFAULT_PRESSED_TEXT_COLOR

private var image: Drawable? = null
private var imageResId = -1

private var drawableResId = -1
private var unpressedTextColor = Color.BLACK
private var pressedTextColor = Color.WHITE
private var pressedBackgroundDrawable: Drawable? = null

private var unpressedBackgroundDrawable: Drawable? = null

private var myOnClickListener: OnClickListener? = null
private var myOnTouchListener: OnTouchListener? = null

Expand Down Expand Up @@ -69,8 +72,8 @@ class GravityImageRadioButton : RelativeLayout, GravityRadioCheckable {
try {
text = array.getString(R.styleable.GravityImageRadioButton_unpressedText) ?: ""
image = array.getDrawable(R.styleable.GravityImageRadioButton_image)
unpressedTextColor = array.getColor(R.styleable.GravityImageRadioButton_unpressedTextColor, Color.BLACK)
pressedTextColor = array.getColor(R.styleable.GravityImageRadioButton_pressedTextColor, Color.WHITE)
unpressedTextColor = array.getColor(R.styleable.GravityImageRadioButton_unpressedTextColor, DEFAULT_UNPRESSED_TEXT_COLOR)
pressedTextColor = array.getColor(R.styleable.GravityImageRadioButton_pressedTextColor, DEFAULT_PRESSED_TEXT_COLOR)
pressedBackgroundDrawable = array.getDrawable(R.styleable.GravityImageRadioButton_pressedBackgroundDrawable)
} finally {
array.recycle()
Expand All @@ -91,9 +94,9 @@ class GravityImageRadioButton : RelativeLayout, GravityRadioCheckable {
}

private fun bindView() {
textT.setTextColor(unpressedTextColor)
textT.text = text
imageI.setImageDrawable(image)
textT?.setTextColor(unpressedTextColor)
textT?.text = text
imageI?.setImageDrawable(image)
}

override fun setOnClickListener(l: OnClickListener?) {
Expand All @@ -119,32 +122,32 @@ class GravityImageRadioButton : RelativeLayout, GravityRadioCheckable {
myOnClickListener?.onClick(this)
}

fun setPressedState() {
private fun setPressedState() {
if (pressedBackgroundDrawable == null) setBackgroundResource(DEFAULT_PRESSED_BACKGROUND_PRESSED_ID)
else background = pressedBackgroundDrawable

textT.setTextColor(pressedTextColor)
textT?.setTextColor(pressedTextColor)
}

fun setUnpressedState() {
private fun setUnpressedState() {
background = unpressedBackgroundDrawable
textT.setTextColor(unpressedTextColor)
textT?.setTextColor(unpressedTextColor)
}

fun text() = text

fun setText(text: String) {
this.text = text
textT.text = text
textT?.text = text
}

fun setTextColor(@ColorRes color: Int) {
textT.setTextColor(ContextCompat.getColor(context, color))
textT?.setTextColor(ContextCompat.getColor(context, color))
}

fun setImageResource(@DrawableRes imageResId: Int) {
drawableResId = imageResId
imageI.setImageResource(drawableResId)
this.imageResId = imageResId
imageI?.setImageResource(this.imageResId)
}

override fun addOnCheckedChangeListener(listener: GravityRadioCheckable.OnCheckedChangeListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ class GravityRadioGroup : LinearLayout {
return p is LayoutParams
}

fun clearCheck() = check(View.NO_ID)
private fun clearCheck() = check(View.NO_ID)

fun check(@IdRes id: Int) {
private fun check(@IdRes id: Int) {
if (id != View.NO_ID && id == radioButtonCheckedId) return // don't even bother
if (radioButtonCheckedId != View.NO_ID) setCheckedStateForView(radioButtonCheckedId, false)
if (id != View.NO_ID) setCheckedStateForView(id, true)
Expand Down

0 comments on commit 1cf2272

Please sign in to comment.