Skip to content

Commit

Permalink
Support dynamic drawables for buttons #108
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkeppeler committed May 9, 2022
1 parent 5db9f76 commit 8fc36b9
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.maxkeppeler.sheets.calendar

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.graphics.drawable.InsetDrawable
import android.graphics.drawable.LayerDrawable
import android.os.Bundle
Expand All @@ -32,6 +33,7 @@ import android.widget.LinearLayout
import androidx.annotation.DrawableRes
import androidx.annotation.IntRange
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.shape.CornerFamily
import com.google.android.material.shape.MaterialShapeDrawable
Expand Down Expand Up @@ -324,7 +326,7 @@ class CalendarSheet : Sheet() {
positiveListener: CalendarDateListener? = null,
) {
this.positiveText = windowContext.getString(positiveRes)
this.positiveButtonDrawableRes = drawableRes
this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.listener = positiveListener
}

Expand All @@ -341,7 +343,7 @@ class CalendarSheet : Sheet() {
positiveListener: CalendarDateListener? = null,
) {
this.positiveText = positiveText
this.positiveButtonDrawableRes = drawableRes
this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.listener = positiveListener
}

Expand All @@ -362,7 +364,10 @@ class CalendarSheet : Sheet() {
* @param positiveRes The String resource id for the positive button.
* @param multipleListener Listener that is invoked when the positive button is clicked.
*/
fun onMultiplePositive(@StringRes positiveRes: Int, multipleListener: CalendarMultipleDatesListener? = null) {
fun onMultiplePositive(
@StringRes positiveRes: Int,
multipleListener: CalendarMultipleDatesListener? = null
) {
this.positiveText = windowContext.getString(positiveRes)
this.multipleListener = multipleListener
}
Expand All @@ -374,7 +379,10 @@ class CalendarSheet : Sheet() {
* @param positiveText The text for the positive button.
* @param multipleListener Listener that is invoked when the positive button is clicked.
*/
fun onMultiplePositive(positiveText: String, multipleListener: CalendarMultipleDatesListener? = null) {
fun onMultiplePositive(
positiveText: String,
multipleListener: CalendarMultipleDatesListener? = null
) {
this.positiveText = positiveText
this.multipleListener = multipleListener
}
Expand All @@ -393,7 +401,7 @@ class CalendarSheet : Sheet() {
multipleListener: CalendarMultipleDatesListener? = null,
) {
this.positiveText = windowContext.getString(positiveRes)
this.positiveButtonDrawableRes = drawableRes
this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.multipleListener = multipleListener
}

Expand All @@ -411,7 +419,44 @@ class CalendarSheet : Sheet() {
multipleListener: CalendarMultipleDatesListener? = null,
) {
this.positiveText = positiveText
this.positiveButtonDrawableRes = drawableRes
this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.multipleListener = multipleListener
}


/**
* Set the text and icon of the positive button and optionally a listener.
* (Multiple dates selection mode)
*
* @param positiveRes The String resource id for the positive button.
* @param drawable The drawable for the button icon.
* @param multipleListener Listener that is invoked when the positive button is clicked.
*/
fun onMultiplePositive(
@StringRes positiveRes: Int,
drawable: Drawable,
multipleListener: CalendarMultipleDatesListener? = null,
) {
this.positiveText = windowContext.getString(positiveRes)
this.positiveButtonDrawable = drawable
this.multipleListener = multipleListener
}

/**
* Set the text and icon of the positive button and optionally a listener.
* (Multiple dates selection mode)
*
* @param positiveText The text for the positive button.
* @param drawable The drawable for the button icon.
* @param multipleListener Listener that is invoked when the positive button is clicked.
*/
fun onMultiplePositive(
positiveText: String,
drawable: Drawable,
multipleListener: CalendarMultipleDatesListener? = null,
) {
this.positiveText = positiveText
this.positiveButtonDrawable = drawable
this.multipleListener = multipleListener
}

Expand Down
39 changes: 37 additions & 2 deletions color/src/main/java/com/maxkeppeler/sheets/color/ColorSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package com.maxkeppeler.sheets.color
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.RippleDrawable
import android.os.Bundle
Expand Down Expand Up @@ -192,7 +193,7 @@ class ColorSheet : Sheet(), SeekBar.OnSeekBarChangeListener {
listener: ColorListener? = null
) {
this.positiveText = windowContext.getString(positiveRes)
this.positiveButtonDrawableRes = drawableRes
this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.listener = listener
}

Expand All @@ -209,7 +210,41 @@ class ColorSheet : Sheet(), SeekBar.OnSeekBarChangeListener {
listener: ColorListener? = null
) {
this.positiveText = positiveText
this.positiveButtonDrawableRes = drawableRes
this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.listener = listener
}

/**
* Set the text of the positive button and set the [ColorListener].
*
* @param positiveRes The String resource id for the positive button.
* @param drawable The drawable for the button icon.
* @param listener Listener that is invoked with the selected color when the positive button is clicked.
*/
fun onPositive(
@StringRes positiveRes: Int,
drawable: Drawable,
listener: ColorListener? = null
) {
this.positiveText = windowContext.getString(positiveRes)
this.positiveButtonDrawable = drawable
this.listener = listener
}

/**
* Set the text of the positive button and set the [ColorListener].
*
* @param positiveText The text for the positive button.
* @param drawable The drawable for the button icon.
* @param listener Listener that is invoked with the selected color when the positive button is clicked.
*/
fun onPositive(
positiveText: String,
drawable: Drawable,
listener: ColorListener? = null
) {
this.positiveText = positiveText
this.positiveButtonDrawable = drawable
this.listener = listener
}

Expand Down
16 changes: 6 additions & 10 deletions core/src/main/java/com/maxkeppeler/sheets/core/Sheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import coil.loadAny
import com.google.android.material.shape.ShapeAppearanceModel
import com.maxkeppeler.sheets.R
import com.maxkeppeler.sheets.core.utils.*
import com.maxkeppeler.sheets.databinding.SheetsBaseBinding

Expand Down Expand Up @@ -118,11 +117,8 @@ abstract class Sheet : SheetFragment() {
protected var positiveText: String? = null
private var negativeText: String? = null

@DrawableRes
protected var positiveButtonDrawableRes: Int? = null

@DrawableRes
private var negativeButtonDrawableRes: Int? = null
protected var positiveButtonDrawable: Drawable? = null
private var negativeButtonDrawable: Drawable? = null

private var customLayoutHeight: Int? = null

Expand Down Expand Up @@ -298,7 +294,7 @@ abstract class Sheet : SheetFragment() {
negativeListener: NegativeListener? = null
) {
this.negativeText = windowContext.getString(negativeRes)
this.negativeButtonDrawableRes = drawableRes
this.negativeButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.negativeListener = negativeListener
}

Expand All @@ -315,7 +311,7 @@ abstract class Sheet : SheetFragment() {
negativeListener: NegativeListener? = null
) {
this.negativeText = negativeText
this.negativeButtonDrawableRes = drawableRes
this.negativeButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.negativeListener = negativeListener
}

Expand Down Expand Up @@ -660,7 +656,7 @@ abstract class Sheet : SheetFragment() {
buttonStyle = negativeButtonStyle,
buttonColor = negativeButtonColor,
btnText = negativeText ?: getString(android.R.string.cancel),
btnDrawable = negativeButtonDrawableRes
btnDrawable = negativeButtonDrawable
) { negativeListener?.invoke(); dismiss() }
}

Expand All @@ -669,7 +665,7 @@ abstract class Sheet : SheetFragment() {
buttonStyle = positiveButtonStyle,
buttonColor = positiveButtonColor,
btnText = positiveText ?: getString(android.R.string.ok),
btnDrawable = positiveButtonDrawableRes
btnDrawable = positiveButtonDrawable
) { positiveListener?.invoke(); dismiss() }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package com.maxkeppeler.sheets.core.views
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.Gravity
import android.view.ViewGroup
Expand Down Expand Up @@ -61,7 +62,7 @@ class SheetButtonContainer
style: ButtonStyle? = null,
buttonColor: Int? = null,
btnText: String,
@DrawableRes btnDrawable: Int?,
btnDrawable: Drawable?,
btnListener: ButtonClickListener,
negative: Boolean,
shapeModel: ShapeAppearanceModel.Builder,
Expand Down Expand Up @@ -111,7 +112,7 @@ class SheetButtonContainer
ViewGroup.LayoutParams(btnWidthLayoutParam, ViewGroup.LayoutParams.WRAP_CONTENT)

text = btnText
btnDrawable?.let { icon = ContextCompat.getDrawable(context, it) }
btnDrawable?.let { icon = btnDrawable }
iconGravity = MaterialButton.ICON_GRAVITY_TEXT_START
iconPadding = BUTTON_ICON_PADDING.toDp()
iconTint = ColorStateList.valueOf(mainButtonColor)
Expand Down Expand Up @@ -155,7 +156,7 @@ class SheetButtonContainer
buttonStyle: ButtonStyle?,
buttonColor: Int?,
btnText: String,
@DrawableRes btnDrawable: Int?,
btnDrawable: Drawable?,
btnListener: ButtonClickListener,
) {

Expand Down Expand Up @@ -236,7 +237,7 @@ class SheetButtonContainer
buttonStyle: ButtonStyle?,
buttonColor: Int?,
btnText: String,
@DrawableRes btnDrawable: Int?,
btnDrawable: Drawable?,
btnListener: ButtonClickListener,
) {

Expand Down
38 changes: 36 additions & 2 deletions info/src/main/java/com/maxkeppeler/sheets/info/InfoSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class InfoSheet : Sheet() {
positiveListener: PositiveListener? = null,
) {
this.positiveText = positiveText
this.positiveButtonDrawableRes = drawableRes
this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.positiveListener = positiveListener
}

Expand All @@ -177,7 +177,41 @@ class InfoSheet : Sheet() {
positiveListener: PositiveListener? = null,
) {
this.positiveText = windowContext.getString(positiveRes)
this.positiveButtonDrawableRes = drawableRes
this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.positiveListener = positiveListener
}

/**
* Set the text and icon of the positive button and optionally a listener.
*
* @param positiveText The text for the positive button.
* @param drawable The drawable for the button icon.
* @param positiveListener Listener that is invoked when the positive button is clicked.
*/
fun onPositive(
positiveText: String,
drawable: Drawable,
positiveListener: PositiveListener? = null,
) {
this.positiveText = positiveText
this.positiveButtonDrawable = drawable
this.positiveListener = positiveListener
}

/**
* Set the text and icon of the positive button and optionally a listener.
*
* @param positiveRes The String resource id for the positive button.
* @param drawable The drawable for the button icon.
* @param positiveListener Listener that is invoked when the positive button is clicked.
*/
fun onPositive(
@StringRes positiveRes: Int,
drawable: Drawable,
positiveListener: PositiveListener? = null,
) {
this.positiveText = windowContext.getString(positiveRes)
this.positiveButtonDrawable = drawable
this.positiveListener = positiveListener
}

Expand Down
42 changes: 39 additions & 3 deletions input/src/main/java/com/maxkeppeler/sheets/input/InputSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
package com.maxkeppeler.sheets.input

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.DrawableRes
import androidx.annotation.IntRange
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.maxkeppeler.sheets.core.Sheet
import com.maxkeppeler.sheets.core.layoutmanagers.CustomGridLayoutManager
Expand Down Expand Up @@ -110,8 +112,7 @@ class InputSheet : Sheet() {
listener: InputListener? = null
) {
this.positiveText = windowContext.getString(positiveRes)
this.positiveButtonDrawableRes = drawableRes

this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.listener = listener
}

Expand All @@ -128,7 +129,42 @@ class InputSheet : Sheet() {
listener: InputListener? = null
) {
this.positiveText = positiveText
this.positiveButtonDrawableRes = drawableRes
this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes)
this.listener = listener
}


/**
* Set the text and icon of the positive button and set the [InputListener].
*
* @param positiveRes The String resource id for the positive button.
* @param drawable The drawable for the button icon.
* @param listener Listener that is invoked with the new input data when the positive button is clicked.
*/
fun onPositive(
@StringRes positiveRes: Int,
drawable: Drawable,
listener: InputListener? = null
) {
this.positiveText = windowContext.getString(positiveRes)
this.positiveButtonDrawable = drawable
this.listener = listener
}

/**
* Set the text and icon of the positive button and set the [InputListener].
*
* @param positiveText The text for the positive button.
* @param drawable The drawable for the button icon.
* @param listener Listener that is invoked with the new input data when the positive button is clicked.
*/
fun onPositive(
positiveText: String,
drawable: Drawable,
listener: InputListener? = null
) {
this.positiveText = positiveText
this.positiveButtonDrawable = drawable
this.listener = listener
}

Expand Down
Loading

0 comments on commit 8fc36b9

Please sign in to comment.