Skip to content

Commit

Permalink
add LiveData support by custom items
Browse files Browse the repository at this point in the history
  • Loading branch information
allco committed Feb 28, 2019
1 parent e646418 commit c2b75b7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
package com.allco.ui.bottomsheet

import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import android.graphics.drawable.Drawable
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner

/**
* Creates [BottomSheetBuilder]
* @param action an configuration action for the [BottomSheetSettings]
*/
fun Activity.bottomSheet(action: BottomSheetSettings.() -> Unit): BottomSheetBuilder {
return BottomSheetBuilder(this, BottomSheetSettings().apply { action() })
fun AppCompatActivity.bottomSheet(action: BottomSheetSettings.() -> Unit): BottomSheetBuilder {
return BottomSheetBuilder(this, this, BottomSheetSettings().apply { action() })
}

/**
* Creates [BottomSheetBuilder]
* @param action an configuration action for the [BottomSheetSettings]
*/
fun Fragment.bottomSheet(action: BottomSheetSettings.() -> Unit): BottomSheetBuilder {
return BottomSheetBuilder(this.context!!, BottomSheetSettings().apply { action() })
return BottomSheetBuilder(this.context!!, this, BottomSheetSettings().apply { action() })
}

/** Represents the configured BottomSheet */
class BottomSheetBuilder internal constructor(private val context: Context, private var settings: BottomSheetSettings) {
class BottomSheetBuilder internal constructor(
private val context: Context,
private val lifecycleOwner: LifecycleOwner,
private var settings: BottomSheetSettings
) {
/** Actually shows the BottomSheet */
fun show(): DialogInterface {
return BottomSheetDialog(context).apply {
return BottomSheetDialog(context, lifecycleOwner).apply {
init(settings)
show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.os.Bundle
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.appcompat.view.ContextThemeWrapper
import androidx.lifecycle.LifecycleOwner
import com.allco.ui.bottomsheet.databinding.BottomSheetListBinding
import com.allco.ui.bottomsheet.utils.ObserverBasedAdapter
import com.allco.ui.bottomsheet.utils.getDrawableCompat
Expand All @@ -17,7 +18,7 @@ import com.google.android.material.bottomsheet.BottomSheetDialog
import kotlin.math.min
import kotlin.math.roundToInt

class BottomSheetDialog(context: Context) : BottomSheetDialog(
class BottomSheetDialog(context: Context, private val lifecycleOwner: LifecycleOwner) : BottomSheetDialog(
ContextThemeWrapper(context, context.getStyleRes(R.attr.bottomSheetLibStyle, R.style.BottomSheetLib))
) {

Expand Down Expand Up @@ -55,7 +56,7 @@ class BottomSheetDialog(context: Context) : BottomSheetDialog(
ClickableViewModelImpl(item, this)
}
is BottomSheetSettings.DividerItem -> DividerViewModelImpl(item)
is BottomSheetSettings.CustomItem -> CustomItemViewModel(item, this)
is BottomSheetSettings.CustomItem -> CustomItemViewModel(item, lifecycleOwner, this)
else -> throw IllegalArgumentException("`item` has unknown type")
}
}
Expand Down
11 changes: 8 additions & 3 deletions bottomsheet/src/main/java/com/allco/ui/bottomsheet/ViewModels.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.allco.ui.bottomsheet

import android.content.DialogInterface
import androidx.databinding.ViewDataBinding
import android.graphics.drawable.Drawable
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LifecycleOwner
import com.allco.ui.bottomsheet.utils.ObserverBasedAdapter

interface TitleViewModel {
Expand Down Expand Up @@ -33,14 +34,18 @@ class DividerViewModelImpl(data: BottomSheetSettings.DividerItem) : DividerViewM
override val layout = R.layout.bottom_sheet_list_item_divider
}

class CustomItemViewModel(private val data: BottomSheetSettings.CustomItem, private val dialog: DialogInterface) :
ObserverBasedAdapter.Item {
class CustomItemViewModel(
private val data: BottomSheetSettings.CustomItem,
private val lifecycleOwner: LifecycleOwner,
private val dialog: DialogInterface
) : ObserverBasedAdapter.Item {
override val layout: Int
get() = data.layoutRes
?: throw IllegalStateException("`layout` property is required for 'custom{}' item")

override val binder: ((ViewDataBinding, Int) -> Unit)
get() = { binding, position ->
binding.lifecycleOwner = lifecycleOwner
data.onBind?.invoke(binding, position, dialog) ?: super.binder
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.allco.ui.bottomsheet.example

import androidx.databinding.DataBindingUtil
import android.graphics.Color
import android.os.Bundle
import androidx.core.content.res.ResourcesCompat
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.allco.ui.bottomsheet.bottomSheet
import com.allco.ui.bottomsheet.example.databinding.ActivityMainBinding
import com.allco.ui.bottomsheet.example.databinding.CustomLayoutBinding
Expand Down Expand Up @@ -86,6 +88,8 @@ class MainActivity : AppCompatActivity() {
}.show()
}

class OneDummyStringViewModel(val dummyText: LiveData<String>)

@Suppress("UNUSED_PARAMETER")
fun runExample3(view: View) {
bottomSheet {
Expand Down Expand Up @@ -126,6 +130,10 @@ class MainActivity : AppCompatActivity() {
button.setOnClickListener {
dialogInterface.dismiss()
}

// LiveData usage
val dummyText = MutableLiveData<String>().apply { postValue("Lorem Ipsum") }
binding.viewModel = OneDummyStringViewModel(dummyText)
}
}
}
Expand Down
28 changes: 21 additions & 7 deletions example/src/main/res/layout/custom_layout.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools"
>

<data>

<variable
name="viewModel"
type="com.allco.ui.bottomsheet.example.MainActivity.OneDummyStringViewModel"
/>
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
tools:context=".MainActivity"
>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:text="Lorem ipsum"
android:text="@{viewModel.dummyText}"
android:textAppearance="@style/TextAppearance.AppCompat.Display2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
/>

<Button
android:id="@+id/button"
Expand All @@ -29,7 +41,8 @@
android:text="Close"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ratingBar" />
app:layout_constraintTop_toBottomOf="@+id/ratingBar"
/>

<RatingBar
android:id="@+id/ratingBar"
Expand All @@ -40,7 +53,8 @@
android:numStars="5"
android:rating="5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
app:layout_constraintTop_toBottomOf="@+id/textView"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

0 comments on commit c2b75b7

Please sign in to comment.