From 8fc36b9983e95a8c1a0517a332e43a5d2f9f5966 Mon Sep 17 00:00:00 2001 From: Maximilian Keppeler Date: Mon, 9 May 2022 16:55:48 +0200 Subject: [PATCH] Support dynamic drawables for buttons #108 --- .../sheets/calendar/CalendarSheet.kt | 57 +++++++++++-- .../maxkeppeler/sheets/color/ColorSheet.kt | 39 ++++++++- .../java/com/maxkeppeler/sheets/core/Sheet.kt | 16 ++-- .../core/views/SheetsButtonContainer.kt | 9 +- .../com/maxkeppeler/sheets/info/InfoSheet.kt | 38 ++++++++- .../maxkeppeler/sheets/input/InputSheet.kt | 42 +++++++++- .../sheets/options/OptionsSheet.kt | 83 +++++++++++++++++-- .../sheets/storage/StorageSheet.kt | 39 ++++++++- .../sheets/time_clock/ClockTimeSheet.kt | 42 +++++++++- .../com/maxkeppeler/sheets/time/TimeSheet.kt | 41 ++++++++- 10 files changed, 368 insertions(+), 38 deletions(-) diff --git a/calendar/src/main/java/com/maxkeppeler/sheets/calendar/CalendarSheet.kt b/calendar/src/main/java/com/maxkeppeler/sheets/calendar/CalendarSheet.kt index 3ed8bad4..f98ec369 100644 --- a/calendar/src/main/java/com/maxkeppeler/sheets/calendar/CalendarSheet.kt +++ b/calendar/src/main/java/com/maxkeppeler/sheets/calendar/CalendarSheet.kt @@ -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 @@ -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 @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/color/src/main/java/com/maxkeppeler/sheets/color/ColorSheet.kt b/color/src/main/java/com/maxkeppeler/sheets/color/ColorSheet.kt index ca7fa398..e4e76187 100644 --- a/color/src/main/java/com/maxkeppeler/sheets/color/ColorSheet.kt +++ b/color/src/main/java/com/maxkeppeler/sheets/color/ColorSheet.kt @@ -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 @@ -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 } @@ -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 } diff --git a/core/src/main/java/com/maxkeppeler/sheets/core/Sheet.kt b/core/src/main/java/com/maxkeppeler/sheets/core/Sheet.kt index 7f3166e5..d5d74284 100644 --- a/core/src/main/java/com/maxkeppeler/sheets/core/Sheet.kt +++ b/core/src/main/java/com/maxkeppeler/sheets/core/Sheet.kt @@ -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 @@ -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 @@ -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 } @@ -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 } @@ -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() } } @@ -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() } } } diff --git a/core/src/main/java/com/maxkeppeler/sheets/core/views/SheetsButtonContainer.kt b/core/src/main/java/com/maxkeppeler/sheets/core/views/SheetsButtonContainer.kt index 2b01a7f9..9dccdc2e 100644 --- a/core/src/main/java/com/maxkeppeler/sheets/core/views/SheetsButtonContainer.kt +++ b/core/src/main/java/com/maxkeppeler/sheets/core/views/SheetsButtonContainer.kt @@ -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 @@ -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, @@ -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) @@ -155,7 +156,7 @@ class SheetButtonContainer buttonStyle: ButtonStyle?, buttonColor: Int?, btnText: String, - @DrawableRes btnDrawable: Int?, + btnDrawable: Drawable?, btnListener: ButtonClickListener, ) { @@ -236,7 +237,7 @@ class SheetButtonContainer buttonStyle: ButtonStyle?, buttonColor: Int?, btnText: String, - @DrawableRes btnDrawable: Int?, + btnDrawable: Drawable?, btnListener: ButtonClickListener, ) { diff --git a/info/src/main/java/com/maxkeppeler/sheets/info/InfoSheet.kt b/info/src/main/java/com/maxkeppeler/sheets/info/InfoSheet.kt index 8135949c..0fe4e7e4 100644 --- a/info/src/main/java/com/maxkeppeler/sheets/info/InfoSheet.kt +++ b/info/src/main/java/com/maxkeppeler/sheets/info/InfoSheet.kt @@ -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 } @@ -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 } diff --git a/input/src/main/java/com/maxkeppeler/sheets/input/InputSheet.kt b/input/src/main/java/com/maxkeppeler/sheets/input/InputSheet.kt index c5613fe0..ce968a95 100644 --- a/input/src/main/java/com/maxkeppeler/sheets/input/InputSheet.kt +++ b/input/src/main/java/com/maxkeppeler/sheets/input/InputSheet.kt @@ -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 @@ -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 } @@ -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 } diff --git a/options/src/main/java/com/maxkeppeler/sheets/options/OptionsSheet.kt b/options/src/main/java/com/maxkeppeler/sheets/options/OptionsSheet.kt index c92e705b..3955b27a 100644 --- a/options/src/main/java/com/maxkeppeler/sheets/options/OptionsSheet.kt +++ b/options/src/main/java/com/maxkeppeler/sheets/options/OptionsSheet.kt @@ -19,6 +19,7 @@ package com.maxkeppeler.sheets.options import android.content.Context +import android.graphics.drawable.Drawable import android.os.Bundle import android.os.Handler import android.os.Looper @@ -28,8 +29,10 @@ import android.text.style.AbsoluteSizeSpan import android.view.LayoutInflater import android.view.Menu import android.view.View -import androidx.annotation.* +import androidx.annotation.DrawableRes import androidx.annotation.IntRange +import androidx.annotation.StringRes +import androidx.core.content.ContextCompat import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import com.maxkeppeler.sheets.core.Sheet @@ -195,7 +198,7 @@ class OptionsSheet : Sheet() { listener: OptionListener? = null ) { this.positiveText = windowContext.getString(positiveRes) - this.positiveButtonDrawableRes = drawableRes + this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes) this.listener = listener } @@ -212,10 +215,46 @@ class OptionsSheet : Sheet() { listener: OptionListener? = 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 [OptionListener]. + * + * @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 options when the positive button is clicked. + */ + fun onPositive( + @StringRes positiveRes: Int, + drawable: Drawable, + listener: OptionListener? = null + ) { + this.positiveText = windowContext.getString(positiveRes) + this.positiveButtonDrawable = drawable + this.listener = listener + } + + /** + * Set the text and icon of the positive button and set the [OptionListener]. + * + * @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 options when the positive button is clicked. + */ + fun onPositive( + positiveText: String, + drawable: Drawable, + listener: OptionListener? = null + ) { + this.positiveText = positiveText + this.positiveButtonDrawable = drawable + this.listener = listener + } + + /** * Set the [OptionListener]. * @@ -260,7 +299,7 @@ class OptionsSheet : Sheet() { listener: OptionsListener? = null ) { this.positiveText = windowContext.getString(positiveRes) - this.positiveButtonDrawableRes = drawableRes + this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes) this.listenerMultiple = listener } @@ -277,7 +316,41 @@ class OptionsSheet : Sheet() { listener: OptionsListener? = null ) { this.positiveText = positiveText - this.positiveButtonDrawableRes = drawableRes + this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes) + this.listenerMultiple = listener + } + + /** + * Set the text and icon of the positive button and set the [OptionListener]. + * + * @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 options when the positive button is clicked. + */ + fun onPositiveMultiple( + @StringRes positiveRes: Int, + drawable: Drawable, + listener: OptionsListener? = null + ) { + this.positiveText = windowContext.getString(positiveRes) + this.positiveButtonDrawable = drawable + this.listenerMultiple = listener + } + + /** + * Set the text and icon of the positive button and set the [OptionListener]. + * + * @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 options when the positive button is clicked. + */ + fun onPositiveMultiple( + positiveText: String, + drawable: Drawable, + listener: OptionsListener? = null + ) { + this.positiveText = positiveText + this.positiveButtonDrawable = drawable this.listenerMultiple = listener } diff --git a/storage/src/main/java/com/maxkeppeler/sheets/storage/StorageSheet.kt b/storage/src/main/java/com/maxkeppeler/sheets/storage/StorageSheet.kt index 0dff71b7..876bbfab 100644 --- a/storage/src/main/java/com/maxkeppeler/sheets/storage/StorageSheet.kt +++ b/storage/src/main/java/com/maxkeppeler/sheets/storage/StorageSheet.kt @@ -21,6 +21,7 @@ package com.maxkeppeler.sheets.storage import android.Manifest import android.content.Context import android.content.pm.PackageManager +import android.graphics.drawable.Drawable import android.os.Bundle import android.os.Environment import android.os.Handler @@ -203,7 +204,7 @@ class StorageSheet : Sheet() { listener: SelectionListener? = null, ) { this.positiveText = windowContext.getString(positiveRes) - this.positiveButtonDrawableRes = drawableRes + this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes) this.listener = listener } @@ -220,7 +221,41 @@ class StorageSheet : Sheet() { listener: SelectionListener? = 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 [SelectionListener]. + * + * @param positiveRes The String resource id for the positive button. + * @param drawable The drawable for the button icon. + * @param listener Listener that is invoked when a file or folder was selected. + */ + fun onPositive( + @StringRes positiveRes: Int, + drawable: Drawable, + listener: SelectionListener? = null, + ) { + this.positiveText = windowContext.getString(positiveRes) + this.positiveButtonDrawable = drawable + this.listener = listener + } + + /** + * Set the text and icon of the positive button and set the [SelectionListener]. + * + * @param positiveText The text for the positive button. + * @param drawable The drawable for the button icon. + * @param listener Listener that is invoked when a file or folder was selected. + */ + fun onPositive( + positiveText: String, + drawable: Drawable, + listener: SelectionListener? = null, + ) { + this.positiveText = positiveText + this.positiveButtonDrawable = drawable this.listener = listener } diff --git a/time-clock/src/main/java/com/maxkeppeler/sheets/time_clock/ClockTimeSheet.kt b/time-clock/src/main/java/com/maxkeppeler/sheets/time_clock/ClockTimeSheet.kt index 7efd9b25..d46aa213 100644 --- a/time-clock/src/main/java/com/maxkeppeler/sheets/time_clock/ClockTimeSheet.kt +++ b/time-clock/src/main/java/com/maxkeppeler/sheets/time_clock/ClockTimeSheet.kt @@ -19,11 +19,13 @@ package com.maxkeppeler.sheets.time_clock 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.StringRes +import androidx.core.content.ContextCompat import com.maxkeppeler.sheets.core.Sheet import com.maxkeppeler.sheets.time_clock.databinding.SheetsTimeClockBinding import java.util.* @@ -90,6 +92,7 @@ class ClockTimeSheet : Sheet() { * Set the text and icon of the positive button and set the [ClockTimeListener]. * * @param positiveRes The String resource id for the positive button. + * @param drawableRes The drawable resource for the button icon. * @param listener Listener that is invoked with the clock time when the positive button is clicked. */ fun onPositive( @@ -98,7 +101,7 @@ class ClockTimeSheet : Sheet() { listener: ClockTimeListener? = null ) { this.positiveText = windowContext.getString(positiveRes) - this.positiveButtonDrawableRes = drawableRes + this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes) this.listener = listener } @@ -106,6 +109,7 @@ class ClockTimeSheet : Sheet() { * Set the text and icon of the positive button and set the [ClockTimeListener]. * * @param positiveText The text for the positive button. + * @param drawableRes The drawable resource for the button icon. * @param listener Listener that is invoked with the clock time when the positive button is clicked. */ fun onPositive( @@ -114,7 +118,41 @@ class ClockTimeSheet : Sheet() { listener: ClockTimeListener? = 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 [ClockTimeListener]. + * + * @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 clock time when the positive button is clicked. + */ + fun onPositive( + @StringRes positiveRes: Int, + drawable: Drawable, + listener: ClockTimeListener? = null + ) { + this.positiveText = windowContext.getString(positiveRes) + this.positiveButtonDrawable = drawable + this.listener = listener + } + + /** + * Set the text and icon of the positive button and set the [ClockTimeListener]. + * + * @param positiveText The text for the positive button. + * @param drawable The drawable for the button icon. + * @param listener Listener that is invoked with the clock time when the positive button is clicked. + */ + fun onPositive( + positiveText: String, + drawable: Drawable, + listener: ClockTimeListener? = null + ) { + this.positiveText = positiveText + this.positiveButtonDrawable = drawable this.listener = listener } diff --git a/time/src/main/java/com/maxkeppeler/sheets/time/TimeSheet.kt b/time/src/main/java/com/maxkeppeler/sheets/time/TimeSheet.kt index 20a7c52e..e69a0e2c 100644 --- a/time/src/main/java/com/maxkeppeler/sheets/time/TimeSheet.kt +++ b/time/src/main/java/com/maxkeppeler/sheets/time/TimeSheet.kt @@ -19,11 +19,13 @@ package com.maxkeppeler.sheets.time 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.StringRes +import androidx.core.content.ContextCompat import com.maxkeppeler.sheets.core.Sheet import com.maxkeppeler.sheets.time.databinding.SheetsTimeBinding @@ -111,7 +113,7 @@ class TimeSheet : Sheet() { listener: DurationTimeListener? = null ) { this.positiveText = windowContext.getString(positiveRes) - this.positiveButtonDrawableRes = drawableRes + this.positiveButtonDrawable = ContextCompat.getDrawable(windowContext, drawableRes) this.listener = listener } @@ -128,7 +130,42 @@ class TimeSheet : Sheet() { listener: DurationTimeListener? = 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 [DurationTimeListener]. + * + * @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 duration time when the positive button is clicked. + */ + fun onPositive( + @StringRes positiveRes: Int, + drawable: Drawable, + listener: DurationTimeListener? = null + ) { + this.positiveText = windowContext.getString(positiveRes) + this.positiveButtonDrawable = drawable + this.listener = listener + } + + /** + * Set the text and icon of the positive button and set the [DurationTimeListener]. + * + * @param positiveText The text for the positive button. + * @param drawable The drawable for the button icon. + * @param listener Listener that is invoked with the duration time when the positive button is clicked. + */ + fun onPositive( + positiveText: String, + drawable: Drawable, + listener: DurationTimeListener? = null + ) { + this.positiveText = positiveText + this.positiveButtonDrawable = drawable this.listener = listener }