Skip to content

Commit

Permalink
Merge pull request #6 from minibugdev/bugs/search_not_found
Browse files Browse the repository at this point in the history
Fix can selected item when search not found
  • Loading branch information
minibugdev authored Jul 9, 2020
2 parents 0f99a35 + ca45836 commit d6958f9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 22 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repositories {
Add the dependency
``` groovy
dependencies {
implementation 'com.github.minibugdev:sheetselection:0.0.2'
implementation 'com.github.minibugdev:sheetselection:0.0.3'
}
```

Expand All @@ -39,6 +39,7 @@ SheetSelection.Builder(context)
.selectedPosition(2)
.showDraggedIndicator(true)
.searchEnabled(true)
.searchNotFoundText("Nothing!!")
.onItemClickListener { item, position ->
// DO SOMETHING
}
Expand All @@ -50,6 +51,7 @@ SheetSelection.Builder(context)
- 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 search not found text by `Builder.searchNotFoundText(String)`. default is `Search not found.`
- 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 @@ -50,6 +50,7 @@ class MainActivity : AppCompatActivity() {
.selectedPosition(2)
.showDraggedIndicator(true)
.searchEnabled(true)
.searchNotFoundText("Nothing!!")
.theme(R.style.Theme_Custom_SheetSelection)
.onItemClickListener { item, position ->
textview.text = "You selected `${item.value}`, At position [$position]."
Expand Down
4 changes: 2 additions & 2 deletions sheetselection/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
defaultConfig {
minSdkVersion 17
targetSdkVersion 29
versionCode 1
versionName "0.0.2"
versionCode 2
versionName "0.0.3"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
Expand All @@ -23,6 +24,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
SheetSelectionAdapter(
source = arguments?.getParcelableArrayList(ARGS_ITEMS) ?: emptyList(),
selectedPosition = arguments?.getInt(ARGS_SELECTED_POSITION, NO_SELECT) ?: NO_SELECT,
searchNotFoundText = arguments?.getString(ARGS_SEARCH_NOT_FOUND_TEXT) ?: "Search not found.",
onItemSelectedListener = onItemSelectedListener
)
}
Expand Down Expand Up @@ -112,7 +114,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
}
}

class Builder(context: Context) {
class Builder(private val context: Context) {
private val manager: FragmentManager? = when (context) {
is FragmentActivity -> context.supportFragmentManager
is Fragment -> context.requireFragmentManager()
Expand All @@ -126,6 +128,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
private var selectedPosition: Int = NO_SELECT
private var showDraggedIndicator: Boolean = false
private var searchEnabled: Boolean = false
private var searchNotFoundText: String? = null
private var listener: OnItemSelectedListener? = null

fun theme(@StyleRes themeId: Int) = apply {
Expand Down Expand Up @@ -157,6 +160,14 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
this.searchEnabled = enabled
}

fun searchNotFoundText(text: String) = apply {
this.searchNotFoundText = text
}

fun searchNotFoundText(@StringRes textResId: Int) = apply {
this.searchNotFoundText = context.getString(textResId)
}

fun onItemClickListener(listener: OnItemSelectedListener) = apply {
this.listener = listener
}
Expand All @@ -170,6 +181,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
putInt(ARGS_SELECTED_POSITION, selectedPosition)
putBoolean(ARGS_SHOW_DRAGGED_INDICATOR, showDraggedIndicator)
putBoolean(ARGS_SEARCH_ENABLED, searchEnabled)
putString(ARGS_SEARCH_NOT_FOUND_TEXT, searchNotFoundText)
}
onItemClickListener = listener
}
Expand All @@ -187,6 +199,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
private const val ARGS_THEME = "SheetSelection:ARGS_THEME"
private const val ARGS_TITLE = "SheetSelection:ARGS_TITLE"
private const val ARGS_ITEMS = "SheetSelection:ARGS_ITEMS"
private const val ARGS_SEARCH_NOT_FOUND_TEXT = "SheetSelection:ARGS_SEARCH_NOT_FOUND_TEXT"
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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,38 @@ typealias OnItemSelectedListener = (item: SheetSelectionItem, position: Int) ->
class SheetSelectionAdapter(
private val source: List<SheetSelectionItem>,
private val selectedPosition: Int,
private val searchNotFoundText: String,
private val onItemSelectedListener: OnItemSelectedListener?
) : RecyclerView.Adapter<SheetSelectionAdapter.ViewHolder>() {
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private var items: List<SheetSelectionItem> = source

override fun getItemCount() = items.size

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return LayoutInflater.from(parent.context)
.inflate(R.layout.row_selection_item, parent, false)
.run {
ViewHolder(this)
}
override fun getItemViewType(position: Int): Int =
when (items[position].key) {
KEY_SEARCH_NOT_FOUND -> R.layout.row_empty_item
else -> R.layout.row_selection_item
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
return when (viewType) {
R.layout.row_selection_item -> ItemViewHolder(view)
R.layout.row_empty_item -> EmptyViewHolder(view, searchNotFoundText)
else -> throw IllegalAccessException("Item view type doesn't match.")
}
}

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

fun search(keyword: String?) {
Expand All @@ -42,7 +52,11 @@ class SheetSelectionAdapter(
} else {
val searchResult = source.filter { it.value.contains(keyword, true) }
if (searchResult.isEmpty()) {
updateItems(listOf(SheetSelectionItem("search_not_found", "Search not found.")))
updateItems(
listOf(
SheetSelectionItem(KEY_SEARCH_NOT_FOUND, searchNotFoundText)
)
)
} else {
updateItems(searchResult)
}
Expand All @@ -54,11 +68,12 @@ class SheetSelectionAdapter(
notifyDataSetChanged()
}

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

fun onBindView(
item: SheetSelectionItem, position: Int, selected: Boolean,
item: SheetSelectionItem,
position: Int,
selected: Boolean,
onItemSelectedListener: OnItemSelectedListener?
) {
val selectedIcon = if (selected) R.drawable.ic_check else 0
Expand All @@ -70,4 +85,16 @@ class SheetSelectionAdapter(
}
}
}

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

init {
textViewItem.text = emptyText
}
}

companion object {
private const val KEY_SEARCH_NOT_FOUND = "SheetSelectionAdapter:search_not_found"
}
}
9 changes: 9 additions & 0 deletions sheetselection/src/main/res/layout/row_empty_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textViewItem"
style="?attr/sheetSelection_itemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="@tools:sample/lorem"
tools:theme="@style/Theme.SheetSelection" />

0 comments on commit d6958f9

Please sign in to comment.