From 1cf2272425bf3f9258342490d13e68255b8ab794 Mon Sep 17 00:00:00 2001 From: raheemadamboev Date: Sun, 19 Dec 2021 23:10:32 +0500 Subject: [PATCH] refactored properly --- .idea/misc.xml | 5 +++ .../GravityImageRadioButton.kt | 41 ++++++++++--------- .../imageradiobutton/GravityRadioGroup.kt | 4 +- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index a3188e8..d92ee27 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -12,6 +12,11 @@ + + + + + diff --git a/image_radio_button/src/main/java/xyz/teamgravity/imageradiobutton/GravityImageRadioButton.kt b/image_radio_button/src/main/java/xyz/teamgravity/imageradiobutton/GravityImageRadioButton.kt index 62b291c..e180741 100644 --- a/image_radio_button/src/main/java/xyz/teamgravity/imageradiobutton/GravityImageRadioButton.kt +++ b/image_radio_button/src/main/java/xyz/teamgravity/imageradiobutton/GravityImageRadioButton.kt @@ -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 @@ -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() @@ -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?) { @@ -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) { diff --git a/image_radio_button/src/main/java/xyz/teamgravity/imageradiobutton/GravityRadioGroup.kt b/image_radio_button/src/main/java/xyz/teamgravity/imageradiobutton/GravityRadioGroup.kt index 5a7304a..3dc7fce 100644 --- a/image_radio_button/src/main/java/xyz/teamgravity/imageradiobutton/GravityRadioGroup.kt +++ b/image_radio_button/src/main/java/xyz/teamgravity/imageradiobutton/GravityRadioGroup.kt @@ -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)