Skip to content

Commit 49d0d90

Browse files
committed
Support for theme
1 parent 051d683 commit 49d0d90

8 files changed

Lines changed: 27 additions & 42 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Changelog
2+
## [1.3.0] - June 27, 2022
3+
- Support .theme argument in builder

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ ModalBottomSheetDialogFragment.Builder()
7676
//custom option, without needing menu XML
7777
.add(OptionRequest(123, "Custom", R.drawable.ic_bluetooth_black_24dp))
7878
.layout(R.layout.item_custom)
79+
.theme(R.style.CustomTheme)
7980
.header("Neat")
8081
.columns(3)
8182
.show(supportFragmentManager, "custom")

app/src/main/java/com/commit451/modalbottomsheetdialogfragment/sample/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class MainActivity : AppCompatActivity(), ModalBottomSheetDialogFragment.Listene
5353
.add(R.menu.options)
5454
.add(OptionRequest(123, "Custom", R.drawable.ic_bluetooth_black_24dp))
5555
.add(R.menu.option_money)
56-
.rounded(true)
56+
.theme(R.style.ModalBottomSheetDialogTheme)
5757
.show(supportFragmentManager, "HI")
5858
}
5959
}

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@
4646
android:layout_height="wrap_content"
4747
android:layout_gravity="center"
4848
android:layout_margin="8dp"
49-
android:text="Show Rounded" />
49+
android:text="Show Material3 Rounded" />
5050

5151
</LinearLayout>

app/src/main/res/values/styles.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
<item name="colorAccent">@color/colorAccent</item>
77
</style>
88

9+
<style name="ModalBottomSheetDialogTheme" parent="Theme.Material3.DayNight.BottomSheetDialog" />
10+
911
</resources>

modalbottomsheetdialogfragment/src/main/java/com/commit451/modalbottomsheetdialogfragment/ModalBottomSheetDialogFragment.kt

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.widget.ImageView
1010
import android.widget.TextView
1111
import androidx.annotation.LayoutRes
1212
import androidx.annotation.MenuRes
13+
import androidx.annotation.StyleRes
1314
import androidx.appcompat.view.menu.MenuBuilder
1415
import androidx.fragment.app.FragmentManager
1516
import androidx.recyclerview.widget.GridLayoutManager
@@ -32,7 +33,7 @@ class ModalBottomSheetDialogFragment : BottomSheetDialogFragment() {
3233
private const val KEY_COLUMNS = "columns"
3334
private const val KEY_HEADER = "header"
3435
private const val KEY_HEADER_LAYOUT_RES = "header_layout_res"
35-
private const val KEY_ROUNDED = "rounded"
36+
private const val KEY_THEME = "theme"
3637

3738
private fun newInstance(builder: Builder): ModalBottomSheetDialogFragment {
3839
val fragment = ModalBottomSheetDialogFragment()
@@ -42,7 +43,7 @@ class ModalBottomSheetDialogFragment : BottomSheetDialogFragment() {
4243
args.putInt(KEY_COLUMNS, builder.columns)
4344
args.putString(KEY_HEADER, builder.header)
4445
args.putInt(KEY_HEADER_LAYOUT_RES, builder.headerLayoutRes)
45-
args.putBoolean(KEY_ROUNDED, builder.isRounded)
46+
builder.theme?.let { args.putInt(KEY_THEME, it) }
4647
fragment.arguments = args
4748
return fragment
4849
}
@@ -66,23 +67,18 @@ class ModalBottomSheetDialogFragment : BottomSheetDialogFragment() {
6667

6768
override fun onCreate(savedInstanceState: Bundle?) {
6869
super.onCreate(savedInstanceState)
69-
val arguments = arguments
70-
?: throw IllegalStateException("You need to create this via the builder")
71-
val isRounded = arguments.getBoolean(KEY_ROUNDED)
72-
if (isRounded) {
73-
setStyle(STYLE_NORMAL, R.style.ModalBottomSheetDialogThemeRounded)
74-
} else {
75-
setStyle(STYLE_NORMAL, R.style.ModalBottomSheetDialogTheme)
70+
val arguments = checkArguments()
71+
val theme = arguments.getInt(KEY_THEME)
72+
if (theme != 0) {
73+
setStyle(STYLE_NORMAL, theme)
7674
}
7775
}
7876

7977
@SuppressLint("RestrictedApi")
8078
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
8179
super.onViewCreated(view, savedInstanceState)
8280
list = view.findViewById(R.id.list)
83-
val arguments = arguments
84-
?: throw IllegalStateException("You need to create this via the builder")
85-
81+
val arguments = checkArguments()
8682
val optionHolders = arguments.getParcelableArrayList<OptionHolder>(KEY_OPTIONS)!!
8783

8884
val options = mutableListOf<Option>()
@@ -150,6 +146,12 @@ class ModalBottomSheetDialogFragment : BottomSheetDialogFragment() {
150146
throw IllegalStateException("ModalBottomSheetDialogFragment must be attached to a parent (activity or fragment) that implements the ModalBottomSheetDialogFragment.Listener")
151147
}
152148

149+
private fun checkArguments(): Bundle {
150+
return arguments
151+
?: throw IllegalStateException("You need to create this via the builder")
152+
153+
}
154+
153155
/**
154156
* Used to build a [ModalBottomSheetDialogFragment]
155157
*/
@@ -162,7 +164,9 @@ class ModalBottomSheetDialogFragment : BottomSheetDialogFragment() {
162164
internal var columns = 1
163165
internal var header: String? = null
164166
internal var headerLayoutRes = R.layout.modal_bottom_sheet_dialog_fragment_header
165-
internal var isRounded = false
167+
168+
@StyleRes
169+
internal var theme: Int? = null
166170

167171
/**
168172
* Inflate the given menu resource to the options
@@ -210,11 +214,8 @@ class ModalBottomSheetDialogFragment : BottomSheetDialogFragment() {
210214
return this
211215
}
212216

213-
/*
214-
* Set rounded on top bottom sheet dialog
215-
*/
216-
fun rounded(isRounded: Boolean): Builder {
217-
this.isRounded = isRounded
217+
fun theme(@StyleRes theme: Int): Builder {
218+
this.theme = theme
218219
return this
219220
}
220221

modalbottomsheetdialogfragment/src/main/res/drawable/rounded.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

modalbottomsheetdialogfragment/src/main/res/values/styles.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)