diff --git a/bottomsheet/src/main/java/com/allco/ui/bottomsheet/BottomSheetBuilder.kt b/bottomsheet/src/main/java/com/allco/ui/bottomsheet/BottomSheetBuilder.kt index 7114a83..8ac0d1c 100644 --- a/bottomsheet/src/main/java/com/allco/ui/bottomsheet/BottomSheetBuilder.kt +++ b/bottomsheet/src/main/java/com/allco/ui/bottomsheet/BottomSheetBuilder.kt @@ -7,6 +7,7 @@ import android.databinding.ViewDataBinding import android.graphics.drawable.Drawable import android.support.annotation.ColorRes import android.support.annotation.DrawableRes +import android.support.annotation.StringRes import android.support.v4.app.Fragment /** @@ -81,30 +82,36 @@ class BottomSheetSettings { field = value.coerceIn(0, 100) } - internal interface Item /** * Represents unclickable item with title * @property title the text which will be shown as a title */ - data class TitleItem(override var title: String? = null) : Item, TitleViewModel + data class TitleItem( + override var title: String? = null, + @StringRes var titleRes: Int? = null + ) : Item, TitleViewModel /** * Represents the horizontal line item aka divider * @property leftOffset blank gap from left side in pixels * @property rightOffset blank gap from right side in pixels */ - data class DividerItem(override var leftOffset: Int? = null, - override var rightOffset: Int? = null) : Item, DividerViewModel + data class DividerItem( + override var leftOffset: Int? = null, + override var rightOffset: Int? = null + ) : Item, DividerViewModel /** * Represents an item with custom layout * @property layoutRes id of layout from resource * @property onBind action which will be called every time when the [layoutRes] is supposed to be populated with actual data. */ - data class CustomItem(var layoutRes: Int? = null, - var onBind: ((binding: ViewDataBinding, position: Int, dialog: DialogInterface) -> Unit)? = null) : Item + data class CustomItem( + var layoutRes: Int? = null, + var onBind: ((binding: ViewDataBinding, position: Int, dialog: DialogInterface) -> Unit)? = null + ) : Item /** * Represents an clickable item which can optionally can have a title and an icon. @@ -116,13 +123,16 @@ class BottomSheetSettings { * @property onClicked an action which will be invoked is the user tapped the item * @property dismissOnClick if `false` then the BottomSheet will not be dismissed automatically if the user tapped the item */ - data class ClickableItem(override var title: String? = null, - override var iconUrl: String? = null, - override var iconDrawable: Drawable? = null, - override var onClicked: (() -> Unit)? = null, - @DrawableRes override var iconRes: Int? = null, - @ColorRes override var iconResTintColor: Int = R.color.bottom_sheet_item_text_title, - var dismissOnClick: Boolean = true) : Item, ClickableViewModel + data class ClickableItem( + override var title: String? = null, + @StringRes var titleRes: Int? = null, + override var iconUrl: String? = null, + override var iconDrawable: Drawable? = null, + override var onClicked: (() -> Unit)? = null, + @DrawableRes override var iconRes: Int? = null, + @ColorRes override var iconResTintColor: Int = R.color.bottom_sheet_item_text_title, + var dismissOnClick: Boolean = true + ) : Item, ClickableViewModel internal val listItems = mutableListOf() } diff --git a/bottomsheet/src/main/java/com/allco/ui/bottomsheet/BottomSheetDialog.kt b/bottomsheet/src/main/java/com/allco/ui/bottomsheet/BottomSheetDialog.kt index ccf43c5..3a32cce 100644 --- a/bottomsheet/src/main/java/com/allco/ui/bottomsheet/BottomSheetDialog.kt +++ b/bottomsheet/src/main/java/com/allco/ui/bottomsheet/BottomSheetDialog.kt @@ -35,14 +35,20 @@ class BottomSheetDialog(context: Context) : android.support.design.widget.Bottom private fun convertToViewModelList(settings: BottomSheetSettings): ObserverBasedAdapter.ItemList { return ObserverBasedAdapter.ItemList().also { list -> - for (it in settings.listItems) { + for (item in settings.listItems) { // in case of linear BS use stack of linear VM's - list += when (it) { - is BottomSheetSettings.TitleItem -> TitleViewModelImpl(it) - is BottomSheetSettings.DividerItem -> DividerViewModelImpl(it) - is BottomSheetSettings.ClickableItem -> ClickableViewModelImpl(it, this) - is BottomSheetSettings.CustomItem -> CustomItemViewModel(it, this) - else -> throw IllegalArgumentException("`it` has unknown type") + list += when (item) { + is BottomSheetSettings.TitleItem -> { + item.titleRes?.also { item.title = context.getString(it) } + TitleViewModelImpl(item) + } + is BottomSheetSettings.ClickableItem -> { + item.titleRes?.also { item.title = context.getString(it) } + ClickableViewModelImpl(item, this) + } + is BottomSheetSettings.DividerItem -> DividerViewModelImpl(item) + is BottomSheetSettings.CustomItem -> CustomItemViewModel(item, this) + else -> throw IllegalArgumentException("`item` has unknown type") } } } diff --git a/bottomsheet/src/main/res/layout/bottom_sheet_list_item_divider.xml b/bottomsheet/src/main/res/layout/bottom_sheet_list_item_divider.xml index c06353f..e986680 100644 --- a/bottomsheet/src/main/res/layout/bottom_sheet_list_item_divider.xml +++ b/bottomsheet/src/main/res/layout/bottom_sheet_list_item_divider.xml @@ -5,7 +5,6 @@ > - (this, R.layout.activity_main) } + @Suppress("UNUSED_PARAMETER") + fun runExampleYesNo(view: View) { + bottomSheet { + clickableItem { + titleRes = R.string.yes + onClicked = { toast(title.toString()) } + } + clickableItem { + titleRes = R.string.no + onClicked = { toast(title.toString()) } + } + }.show() + } + + @Suppress("UNUSED_PARAMETER") fun runExample1(view: View) { bottomSheet { clickableItem { @@ -47,6 +62,7 @@ class MainActivity : AppCompatActivity() { }.show() } + @Suppress("UNUSED_PARAMETER") fun runExample2(view: View) { bottomSheet { clickableItem { @@ -83,7 +99,7 @@ class MainActivity : AppCompatActivity() { }.show() } - + @Suppress("UNUSED_PARAMETER") fun runExample3(view: View) { bottomSheet { maxInitialHeightInPercents = 100 @@ -106,17 +122,19 @@ class MainActivity : AppCompatActivity() { custom { layoutRes = R.layout.custom_layout - onBind = { binding, position, dialogInterface -> + onBind = { binding, _, dialogInterface -> (binding as CustomLayoutBinding).apply { //model = setup data accordingly `position` - binding.ratingBar.setOnRatingBarChangeListener { ratingBar, rating, _ -> - binding.root.setBackgroundColor(when { - rating < 1 -> Color.RED.withAlpha(0x88) - rating < 2 -> Color.MAGENTA.withAlpha(0x88) - rating < 3 -> Color.YELLOW.withAlpha(0x88) - rating < 4 -> Color.CYAN.withAlpha(0x88) - else -> Color.GREEN.withAlpha(0x88) - }) + binding.ratingBar.setOnRatingBarChangeListener { _, rating, _ -> + binding.root.setBackgroundColor( + when { + rating < 1 -> Color.RED.withAlpha(0x88) + rating < 2 -> Color.MAGENTA.withAlpha(0x88) + rating < 3 -> Color.YELLOW.withAlpha(0x88) + rating < 4 -> Color.CYAN.withAlpha(0x88) + else -> Color.GREEN.withAlpha(0x88) + } + ) } button.setOnClickListener { dialogInterface.dismiss() diff --git a/example/src/main/res/layout/activity_main.xml b/example/src/main/res/layout/activity_main.xml index 80bd7af..df10d15 100644 --- a/example/src/main/res/layout/activity_main.xml +++ b/example/src/main/res/layout/activity_main.xml @@ -30,5 +30,13 @@ android:onClick="runExample3" android:text="Example #3" /> +