Skip to content

Commit

Permalink
introduce titleRes and reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
allco committed May 22, 2018
1 parent 34400cc commit 0063522
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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<Item>()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
>

<data>

<variable
name="model"
type="com.allco.ui.bottomsheet.DividerViewModel"
Expand All @@ -16,8 +15,8 @@
android:id="@+id/fr_bottom_sheet_divider"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_sheet_divider_height"
app:marginLeft="@{model.rightOffset}"
app:marginRight="@{model.rightOffset}"
app:marginLeft="@{safeUnbox(model.rightOffset)}"
app:marginRight="@{safeUnbox(model.rightOffset)}"
android:layout_marginBottom="@dimen/bottom_sheet_padding_divider_bottom"
android:layout_marginTop="@dimen/bottom_sheet_padding_divider_top"
android:background="@color/bottom_sheet_divider_color_grey"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ class MainActivity : AppCompatActivity() {
DataBindingUtil.setContentView<ActivityMainBinding>(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 {
Expand All @@ -47,6 +62,7 @@ class MainActivity : AppCompatActivity() {
}.show()
}

@Suppress("UNUSED_PARAMETER")
fun runExample2(view: View) {
bottomSheet {
clickableItem {
Expand Down Expand Up @@ -83,7 +99,7 @@ class MainActivity : AppCompatActivity() {
}.show()
}


@Suppress("UNUSED_PARAMETER")
fun runExample3(view: View) {
bottomSheet {
maxInitialHeightInPercents = 100
Expand All @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,13 @@
android:onClick="runExample3"
android:text="Example #3" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Example Yes/No"
android:onClick="runExampleYesNo"
/>

</LinearLayout>
</layout>
2 changes: 2 additions & 0 deletions example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">example</string>
<string name="yes">Yes</string>
<string name="no">No</string>
</resources>
Binary file added pics/Example1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0063522

Please sign in to comment.