Skip to content

Commit

Permalink
Reformat Kotlin files
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Mar 15, 2022
1 parent 3c4dd79 commit 9d19499
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 100 deletions.
104 changes: 59 additions & 45 deletions lib/src/main/java/com/davidmiguel/multistateswitch/MultiStateSwitch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package com.davidmiguel.multistateswitch

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.graphics.Bitmap
import android.graphics.BlurMaskFilter
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Point
import android.graphics.Rect
import android.graphics.Typeface
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
Expand All @@ -11,7 +17,11 @@ import android.util.AttributeSet
import android.util.Log
import android.util.SparseArray
import android.util.TypedValue
import android.view.*
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.Dimension
Expand All @@ -28,10 +38,10 @@ private const val SHADOW_BOTTOM_OVERFLOW_DP = 10f

@Suppress("MemberVisibilityCanBePrivate", "unused")
class MultiStateSwitch @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.multistateswitch_MultiStateSwitchStyle,
defStyleRes: Int = R.style.multistateswitch_MultiStateSwitch,
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.multistateswitch_MultiStateSwitchStyle,
defStyleRes: Int = R.style.multistateswitch_MultiStateSwitch,
) : View(context, attrs, defStyleAttr, defStyleRes) {

// START Styleable properties
Expand Down Expand Up @@ -309,33 +319,34 @@ class MultiStateSwitch @JvmOverloads constructor(
val stateStyle = statesStyles[stateIndex] ?: StateStyle.Builder().build()
// Create the drawable representing the state (normal, selected or disabled)
val bitmapNormal = createStateDrawable(
stateText = text,
textWidth = stateSize.x,
textHeight = stateSize.y,
backgroundColor = 0,
shadow = false,
textColor = stateStyle.textColor ?: textColor,
textSize = stateStyle.textSize ?: textSize,
textTypeface = stateStyle.textTypeface ?: textTypeface
stateText = text,
textWidth = stateSize.x,
textHeight = stateSize.y,
backgroundColor = 0,
shadow = false,
textColor = stateStyle.textColor ?: textColor,
textSize = stateStyle.textSize ?: textSize,
textTypeface = stateStyle.textTypeface ?: textTypeface
)
val bitmapSelected = if (disabledStateEnabled && stateIndex == disableStateIndex) createStateDrawable(
stateText = disabledText,
textWidth = stateSize.x,
textHeight = stateSize.y,
backgroundColor = stateStyle.disabledBackgroundColor ?: disabledBackgroundColor,
shadow = true,
textColor = stateStyle.disabledTextColor ?: disabledTextColor,
textSize = stateStyle.disabledTextSize ?: disabledTextSize,
textTypeface = stateStyle.disabledTextTypeface ?: textTypeface
stateText = disabledText,
textWidth = stateSize.x,
textHeight = stateSize.y,
backgroundColor = stateStyle.disabledBackgroundColor ?: disabledBackgroundColor,
shadow = true,
textColor = stateStyle.disabledTextColor ?: disabledTextColor,
textSize = stateStyle.disabledTextSize ?: disabledTextSize,
textTypeface = stateStyle.disabledTextTypeface ?: textTypeface
)
else createStateDrawable(selectedText,
textWidth = stateSize.x,
textHeight = stateSize.y,
backgroundColor = stateStyle.selectedBackgroundColor ?: selectedBackgroundColor,
shadow = true,
textColor = stateStyle.selectedTextColor ?: selectedTextColor,
textSize = stateStyle.selectedTextSize ?: selectedTextSize,
textTypeface = stateStyle.selectedTextTypeface ?: textTypeface
else createStateDrawable(
selectedText,
textWidth = stateSize.x,
textHeight = stateSize.y,
backgroundColor = stateStyle.selectedBackgroundColor ?: selectedBackgroundColor,
shadow = true,
textColor = stateStyle.selectedTextColor ?: selectedTextColor,
textSize = stateStyle.selectedTextSize ?: selectedTextSize,
textTypeface = stateStyle.selectedTextTypeface ?: textTypeface
)
return StateSelector(bitmapNormal, bitmapSelected)
}
Expand All @@ -344,14 +355,14 @@ class MultiStateSwitch @JvmOverloads constructor(
* Creates a drawable that represents the state with given styles.
*/
private fun createStateDrawable(
stateText: String,
@Dimension textWidth: Int,
@Dimension textHeight: Int,
@ColorInt backgroundColor: Int,
shadow: Boolean,
@ColorInt textColor: Int,
@Dimension textSize: Int,
textTypeface: Typeface?
stateText: String,
@Dimension textWidth: Int,
@Dimension textHeight: Int,
@ColorInt backgroundColor: Int,
shadow: Boolean,
@ColorInt textColor: Int,
@Dimension textSize: Int,
textTypeface: Typeface?
): BitmapDrawable {
// Create text view
val stateTV = TextView(context).apply {
Expand All @@ -371,8 +382,10 @@ class MultiStateSwitch @JvmOverloads constructor(
}
isDrawingCacheEnabled = true
// Draw it in cache
measure(MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED))
measure(
MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED)
)
layout(0, 0, measuredWidth, measuredHeight)
}
// Convert text view to bitmap
Expand Down Expand Up @@ -451,8 +464,8 @@ class MultiStateSwitch @JvmOverloads constructor(
* Sets current state and notifies the listeners.
*/
private fun setCurrentState(
currentStateIndex: Int,
notifyStateListeners: Boolean
currentStateIndex: Int,
notifyStateListeners: Boolean
) {
if (notifyStateListeners) {
notifyListeners(currentStateIndex)
Expand Down Expand Up @@ -599,9 +612,10 @@ class MultiStateSwitch @JvmOverloads constructor(
*/
private fun drawState(canvas: Canvas, stateDrawable: Drawable, stateCenter: Point) {
stateDrawable.setBounds(
stateCenter.x - stateRadius.x - shadowStartEndOverflowPx,
stateCenter.y - stateRadius.y,
stateCenter.x + stateRadius.x + shadowStartEndOverflowPx, shadowHeight)
stateCenter.x - stateRadius.x - shadowStartEndOverflowPx,
stateCenter.y - stateRadius.y,
stateCenter.x + stateRadius.x + shadowStartEndOverflowPx, shadowHeight
)
stateDrawable.draw(canvas)
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/main/java/com/davidmiguel/multistateswitch/State.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package com.davidmiguel.multistateswitch
* Models a state of the switch.
*/
data class State @JvmOverloads constructor(
val text: String,
val selectedText: String = text,
val disabledText: String = text
val text: String,
val selectedText: String = text,
val disabledText: String = text
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import android.graphics.drawable.Drawable
* Stores the two representations of the state.
*/
data class StateSelector(
val normal: Drawable,
val selected: Drawable
val normal: Drawable,
val selected: Drawable
)
60 changes: 30 additions & 30 deletions lib/src/main/java/com/davidmiguel/multistateswitch/StateStyle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ import androidx.annotation.Dimension
* Defines the colors of one state.
*/
data class StateStyle(
@ColorInt val textColor: Int?,
@Dimension val textSize: Int?,
val textTypeface: Typeface?,
@ColorInt val selectedBackgroundColor: Int?,
@ColorInt val selectedTextColor: Int?,
@Dimension val selectedTextSize: Int?,
val selectedTextTypeface: Typeface?,
@ColorInt val disabledBackgroundColor: Int?,
@ColorInt val disabledTextColor: Int?,
@Dimension val disabledTextSize: Int?,
val disabledTextTypeface: Typeface?
@ColorInt val textColor: Int?,
@Dimension val textSize: Int?,
val textTypeface: Typeface?,
@ColorInt val selectedBackgroundColor: Int?,
@ColorInt val selectedTextColor: Int?,
@Dimension val selectedTextSize: Int?,
val selectedTextTypeface: Typeface?,
@ColorInt val disabledBackgroundColor: Int?,
@ColorInt val disabledTextColor: Int?,
@Dimension val disabledTextSize: Int?,
val disabledTextTypeface: Typeface?
) {
@Suppress("unused")
class Builder {
private var textColor : Int? = null
private var textSize : Int? = null
private var textColor: Int? = null
private var textSize: Int? = null
private var textTypeface: Typeface? = null
private var selectedBackgroundColor : Int? = null
private var selectedTextColor : Int? = null
private var selectedTextSize : Int? = null
private var selectedBackgroundColor: Int? = null
private var selectedTextColor: Int? = null
private var selectedTextSize: Int? = null
private var selectedTextTypeface: Typeface? = null
private var disabledBackgroundColor : Int? = null
private var disabledTextColor : Int? = null
private var disabledTextSize : Int? = null
private var disabledBackgroundColor: Int? = null
private var disabledTextColor: Int? = null
private var disabledTextSize: Int? = null
private var disabledTextTypeface: Typeface? = null

/**
Expand Down Expand Up @@ -124,17 +124,17 @@ data class StateStyle(

fun build(): StateStyle {
return StateStyle(
textColor,
textSize,
textTypeface,
selectedBackgroundColor,
selectedTextColor,
selectedTextSize,
selectedTextTypeface,
disabledBackgroundColor,
disabledTextColor,
disabledTextSize,
disabledTextTypeface
textColor,
textSize,
textTypeface,
selectedBackgroundColor,
selectedTextColor,
selectedTextSize,
selectedTextTypeface,
disabledBackgroundColor,
disabledTextColor,
disabledTextSize,
disabledTextTypeface
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MainActivity : AppCompatActivity(), StateListener {
}

override fun onStateSelected(stateIndex: Int, state: State) {
Log.d("asdf", getString(R.string.listener, stateIndex, state.text))
Log.d("onStateSelected", getString(R.string.listener, stateIndex, state.text))
binding.listener.text = getString(R.string.listener, stateIndex, state.text)
}

Expand All @@ -45,26 +45,28 @@ class MainActivity : AppCompatActivity(), StateListener {

private fun setupCustomizedSwitch() {
binding.customizedSwitch.addStateFromString(
"Cold",
StateStyle.Builder()
.withSelectedBackgroundColor(Color.BLUE)
.build()
"Cold",
StateStyle.Builder()
.withSelectedBackgroundColor(Color.BLUE)
.build()
)
binding.customizedSwitch.addState(
State(
text = "ON",
selectedText = "OFF",
disabledText = "OFF"
),
StateStyle.Builder()
.withTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
.withDisabledBackgroundColor(Color.BLACK)
.withDisabledTextColor(Color.WHITE)
.build()
State(
text = "ON",
selectedText = "OFF",
disabledText = "OFF"
),
StateStyle.Builder()
.withTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
.withDisabledBackgroundColor(Color.BLACK)
.withDisabledTextColor(Color.WHITE)
.build()
)
binding.customizedSwitch.addStateFromString("Hot", StateStyle.Builder()
binding.customizedSwitch.addStateFromString(
"Hot", StateStyle.Builder()
.withSelectedBackgroundColor(Color.RED)
.build())
.build()
)
binding.customizedSwitch.addStateListener(this)
setupCustomizedButtons()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class ViewPagerPageFragment : Fragment() {
private var currentPage: Int = 0

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentViewPagerPageBinding.inflate(inflater, container, false)
currentPage = arguments?.getInt(ARG_PAGE) ?: 0
Expand Down

0 comments on commit 9d19499

Please sign in to comment.