Skip to content

Commit

Permalink
Merge pull request #3 from minibugdev/feature/search-items
Browse files Browse the repository at this point in the history
Supported to search items
  • Loading branch information
minibugdev committed Mar 16, 2020
2 parents 3327b03 + e0665b5 commit cb4b0c8
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 19 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ repositories {
Add the dependency
``` groovy
dependencies {
implementation 'com.github.minibugdev:sheetselection:0.0.1'
implementation 'com.github.minibugdev:sheetselection:0.0.2'
}
```

## How to use
![Sheet Selection](screenshot/ss_1.png)
![Sheet Selection](screenshot/ss_1_0.0.2.png)

``` kotlin
val items = listOf(
Expand All @@ -37,6 +37,7 @@ SheetSelection.Builder(context)
.items(items)
.selectedPosition(2)
.showDraggedIndicator(true)
.searchEnabled(true)
.onItemClickListener { item, position ->
// DO SOMETHING
}
Expand All @@ -47,6 +48,7 @@ SheetSelection.Builder(context)
- Set items by `Builder.items(List<SheetSelectionItem>)`.
- Set selected item by `Builder.selectedPosition(Int)`. default is `SheetSelection.NO_SELECT`
- Show dragged indicator by `Builder.showDraggedIndicator(Boolean)`. default is `false`
- Set search enabled by `Builder.searchEnabled(Boolean)`. default is `false`
- Set custom theme by `Builder.theme(@StyleRes)`.
- To handle the item click listener by `Builder.onItemClickListener()`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ class MainActivity : AppCompatActivity() {
SheetSelectionItem("1", "Item #1", R.drawable.ic_extension),
SheetSelectionItem("2", "Item #2", R.drawable.ic_nature),
SheetSelectionItem("3", "Item #3", R.drawable.ic_fingerprint),
SheetSelectionItem("4", "Item #4", R.drawable.ic_face)
SheetSelectionItem("4", "Item #4", R.drawable.ic_face),
SheetSelectionItem("5", "Item #5", R.drawable.ic_extension),
SheetSelectionItem("6", "Item #6", R.drawable.ic_fingerprint)
)

SheetSelection.Builder(this)
.title("Sheet Selection")
.items(items)
.selectedPosition(2)
.showDraggedIndicator(true)
.searchEnabled(true)
.onItemClickListener { item, position ->
textview.text = "You selected `${item.value}`, At position [$position]."
}
Expand All @@ -46,6 +49,7 @@ class MainActivity : AppCompatActivity() {
)
.selectedPosition(2)
.showDraggedIndicator(true)
.searchEnabled(true)
.theme(R.style.Theme_Custom_SheetSelection)
.onItemClickListener { item, position ->
textview.text = "You selected `${item.value}`, At position [$position]."
Expand Down
Binary file removed screenshot/ss_1.png
Binary file not shown.
Binary file added screenshot/ss_1_0.0.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sheetselection/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
minSdkVersion 17
targetSdkVersion 29
versionCode 1
versionName "0.0.1"
versionName "0.0.2"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,37 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.StyleRes
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.android.synthetic.main.dialog_sheet_selection.*

class SheetSelection private constructor() : BottomSheetDialogFragment() {

var onItemClickListener: OnItemSelectedListener? = null

private val adapter by lazy {
SheetSelectionAdapter(
source = arguments?.getParcelableArrayList(ARGS_ITEMS) ?: emptyList(),
selectedPosition = arguments?.getInt(ARGS_SELECTED_POSITION, NO_SELECT) ?: NO_SELECT,
onItemSelectedListener = onItemSelectedListener
)
}

private val screenHeight by lazy {
val statusBarHeight = try {
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
resources.getDimensionPixelSize(resourceId)
} catch (e: Exception) {
0
}
resources.displayMetrics.heightPixels - statusBarHeight
}

override fun getTheme(): Int = arguments?.getInt(ARGS_THEME) ?: super.getTheme()

override fun onCreateView(
Expand All @@ -36,23 +57,61 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
val title = args.getString(ARGS_TITLE)
if (title.isNullOrEmpty()) {
textViewTitle.visibility = View.GONE
textViewTitle.text = null
} else {
textViewTitle.visibility = View.VISIBLE
textViewTitle.text = title
}

recyclerViewSelectionItems.adapter = SheetSelectionAdapter(
items = args.getParcelableArrayList(ARGS_ITEMS) ?: emptyList(),
selectedPosition = args.getInt(ARGS_SELECTED_POSITION, NO_SELECT),
onItemSelectedListener = internalOnItemSelectedListener
)
if (args.getBoolean(ARGS_SEARCH_ENABLED)) {
buttonSearch.visibility = View.VISIBLE
buttonSearch.setOnClickListener(onSearchClickListener)
searchView.setOnCloseListener(onSearchCloseListener)
searchView.setOnQueryTextListener(onSearchQueryTextListener)
}

recyclerViewSelectionItems.setHasFixedSize(true)
recyclerViewSelectionItems.adapter = adapter
}
}

private val internalOnItemSelectedListener: OnItemSelectedListener = { item, position ->
private fun updateSheetHeight(viewHeight: Int) {
rootLayout.layoutParams = rootLayout.layoutParams
.apply { height = viewHeight }
}

private val onItemSelectedListener: OnItemSelectedListener = { item, position ->
dismiss()
onItemClickListener?.invoke(item, position)
}

private val onSearchClickListener = View.OnClickListener {
(dialog as? BottomSheetDialog)?.run {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
updateSheetHeight(screenHeight)
viewSwitcherHeader.displayedChild = 1
searchView.isIconified = false
}

private val onSearchCloseListener = SearchView.OnCloseListener {
updateSheetHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
viewSwitcherHeader.displayedChild = 0
true
}

private val onSearchQueryTextListener = object : SearchView.OnQueryTextListener {
override fun onQueryTextChange(newText: String?): Boolean {
adapter.search(newText)
return true
}

override fun onQueryTextSubmit(query: String?): Boolean {
adapter.search(query)
return true
}
}

class Builder(context: Context) {
private val manager: FragmentManager? = when (context) {
is FragmentActivity -> context.supportFragmentManager
Expand All @@ -66,6 +125,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
private var items: List<SheetSelectionItem> = emptyList()
private var selectedPosition: Int = NO_SELECT
private var showDraggedIndicator: Boolean = false
private var searchEnabled: Boolean = false
private var listener: OnItemSelectedListener? = null

fun theme(@StyleRes themeId: Int) = apply {
Expand Down Expand Up @@ -93,6 +153,10 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
this.showDraggedIndicator = show
}

fun searchEnabled(enabled: Boolean) = apply {
this.searchEnabled = enabled
}

fun onItemClickListener(listener: OnItemSelectedListener) = apply {
this.listener = listener
}
Expand All @@ -105,6 +169,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
putParcelableArrayList(ARGS_ITEMS, ArrayList(items))
putInt(ARGS_SELECTED_POSITION, selectedPosition)
putBoolean(ARGS_SHOW_DRAGGED_INDICATOR, showDraggedIndicator)
putBoolean(ARGS_SEARCH_ENABLED, searchEnabled)
}
onItemClickListener = listener
}
Expand All @@ -124,5 +189,6 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
private const val ARGS_ITEMS = "SheetSelection:ARGS_ITEMS"
private const val ARGS_SELECTED_POSITION = "SheetSelection:ARGS_SELECTED_POSITION"
private const val ARGS_SHOW_DRAGGED_INDICATOR = "SheetSelection:ARGS_SHOW_DRAGGED_INDICATOR"
private const val ARGS_SEARCH_ENABLED = "SheetSelection:ARGS_SEARCH_ENABLED"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import kotlinx.android.synthetic.main.row_selection_item.*
typealias OnItemSelectedListener = (item: SheetSelectionItem, position: Int) -> Unit

class SheetSelectionAdapter(
private val items: List<SheetSelectionItem>,
private val source: List<SheetSelectionItem>,
private val selectedPosition: Int,
private val onItemSelectedListener: OnItemSelectedListener?
) : RecyclerView.Adapter<SheetSelectionAdapter.ViewHolder>() {

private var items: List<SheetSelectionItem> = source

override fun getItemCount() = items.size

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand All @@ -27,13 +29,31 @@ class SheetSelectionAdapter(

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.onBindView(
items[position],
position,
position == selectedPosition,
onItemSelectedListener
item = items[position],
position = position,
selected = position == selectedPosition,
onItemSelectedListener = onItemSelectedListener
)
}

fun search(keyword: String?) {
if (keyword.isNullOrBlank()) {
updateItems(source)
} else {
val searchResult = source.filter { it.value.contains(keyword, true) }
if (searchResult.isEmpty()) {
updateItems(listOf(SheetSelectionItem("search_not_found", "Search not found.")))
} else {
updateItems(searchResult)
}
}
}

private fun updateItems(items: List<SheetSelectionItem>) {
this.items = items
notifyDataSetChanged()
}

class ViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView),
LayoutContainer {

Expand Down
9 changes: 9 additions & 0 deletions sheetselection/src/main/res/drawable/ic_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector>
41 changes: 37 additions & 4 deletions sheetselection/src/main/res/layout/dialog_sheet_selection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<LinearLayout 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"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
Expand All @@ -15,12 +16,44 @@
android:visibility="gone"
tools:visibility="visible" />

<TextView
android:id="@+id/textViewTitle"
style="?attr/sheetSelection_titleStyle"
<ViewSwitcher
android:id="@+id/viewSwitcherHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="@tools:sample/lorem" />
android:measureAllChildren="false">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/textViewTitle"
style="?attr/sheetSelection_titleStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:text="@tools:sample/lorem" />

<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/buttonSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="8dp"
android:src="@drawable/ic_search"
android:visibility="gone"
app:backgroundTint="?attr/colorSurface"
tools:visibility="visible" />
</LinearLayout>

<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:queryBackground="@null"
app:submitBackground="@null" />
</ViewSwitcher>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewSelectionItems"
Expand Down

0 comments on commit cb4b0c8

Please sign in to comment.