Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to dismiss popup from viewBoundCallback #57

Merged
merged 2 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class MaterialPopupMenu internal constructor(

internal data class PopupMenuCustomItem(
@LayoutRes val layoutResId: Int,
val viewBoundCallback: (View) -> Unit,
val viewBoundCallback: ViewBoundCallback,
override val callback: () -> Unit,
override val dismissOnSelect: Boolean
) : AbstractPopupMenuItem(callback, dismissOnSelect)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ class MaterialPopupMenuBuilder {
/**
* Callback to be invoked once the custom item view gets created and bound.
* It is to be used when some views inside need to be updated once inflated.
*
* You can set this to [ViewBoundCallback] to gain access to additional
* features.
*
* @see ViewBoundCallback
*/
var viewBoundCallback: (View) -> Unit = {}

Expand All @@ -227,9 +232,11 @@ class MaterialPopupMenuBuilder {

override fun convertToPopupMenuItem(): MaterialPopupMenu.PopupMenuCustomItem {
require(layoutResId != 0) { "Layout resource ID must be set for a custom item!" }
val resolvedViewBoundCallback = viewBoundCallback as? ViewBoundCallback
?: ViewBoundCallback { viewBoundCallback(it) }
return MaterialPopupMenu.PopupMenuCustomItem(
layoutResId = layoutResId,
viewBoundCallback = viewBoundCallback,
viewBoundCallback = resolvedViewBoundCallback,
callback = callback,
dismissOnSelect = dismissOnSelect)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.zawadz88.materialpopupmenu

import android.view.View

/**
* Callback to be invoked once the custom item view gets created and bound.
* It is to be used when some views inside need to be updated once inflated.
*
* This class is an extension for closure based callback that provides
* additional functionality such as dismissing popup.
*
* @param callback block of the callback in which you can bind the given view
*/
class ViewBoundCallback(
private val callback: ViewBoundCallback.(View) -> Unit
) : (View) -> Unit {
Copy link
Owner

@zawadz88 zawadz88 Apr 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha quite clever to extend a closure here ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a nice feature. I found it by accident :)

internal var dismissPopupAction: () -> Unit = {
throw IllegalStateException("Dismiss popup action has not been initialized. Make sure that you invoke dismissPopup function only after the popup has been shown.")
}

/**
* Dismisses the shown popup.
*/
fun dismissPopup() {
dismissPopupAction()
}

override fun invoke(view: View) {
callback(view)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.github.zawadz88.materialpopupmenu.R
@SuppressLint("RestrictedApi")
internal class PopupMenuAdapter(
private val sections: List<MaterialPopupMenu.PopupMenuSection>,
private val onItemClickedCallback: (MaterialPopupMenu.AbstractPopupMenuItem) -> Unit
private val dismissPopupCallback: () -> Unit
)
: SectionedRecyclerViewAdapter<PopupMenuAdapter.SectionHeaderViewHolder, PopupMenuAdapter.AbstractItemViewHolder>() {

Expand Down Expand Up @@ -53,7 +53,7 @@ internal class PopupMenuAdapter(
ItemViewHolder(v)
} else {
val v = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
CustomItemViewHolder(v)
CustomItemViewHolder(v, dismissPopupCallback)
}
}

Expand All @@ -75,7 +75,7 @@ internal class PopupMenuAdapter(
holder.itemView.setOnClickListener {
popupMenuItem.callback()
if (popupMenuItem.dismissOnSelect) {
onItemClickedCallback(popupMenuItem)
dismissPopupCallback()
}
}
}
Expand Down Expand Up @@ -117,10 +117,14 @@ internal class PopupMenuAdapter(

}

internal class CustomItemViewHolder(itemView: View) : AbstractItemViewHolder(itemView) {
internal class CustomItemViewHolder(
itemView: View,
private val dismissPopupCallback: () -> Unit
) : AbstractItemViewHolder(itemView) {

override fun bindItem(popupMenuItem: MaterialPopupMenu.AbstractPopupMenuItem) {
val popupMenuCustomItem = popupMenuItem as MaterialPopupMenu.PopupMenuCustomItem
popupMenuCustomItem.viewBoundCallback.dismissPopupAction = dismissPopupCallback
popupMenuCustomItem.viewBoundCallback.invoke(itemView)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.hamcrest.Matchers.hasSize
import org.hamcrest.Matchers.instanceOf
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test

/**
Expand Down Expand Up @@ -274,7 +275,30 @@ class MaterialPopupMenuBuilderTest {
val secondPopupMenuItem = secondItem as MaterialPopupMenu.PopupMenuCustomItem
assertEquals("Invalid item layout ID", CUSTOM_ITEM_LAYOUT, secondPopupMenuItem.layoutResId)
assertEquals("Invalid item callback", customCallback, secondPopupMenuItem.callback)
assertEquals("Invalid item view bound callback", customViewBoundCallback, secondPopupMenuItem.viewBoundCallback)
}

@Test
fun `Should invoke custom view bound callback`() {
//given
val view = mock<View> {}
var callbackInvoked = false
val customViewBoundCallback: (View) -> Unit = { callbackInvoked = true }
val popupMenu = popupMenu {
section {
customItem {
layoutResId = CUSTOM_ITEM_LAYOUT
viewBoundCallback = customViewBoundCallback
}
}
}

//when
val item = popupMenu.sections[0].items[0]
val popupMenuItem = item as MaterialPopupMenu.PopupMenuCustomItem
popupMenuItem.viewBoundCallback.invoke(view)

//then
assertTrue("View bound callback has not beed invoked", callbackInvoked)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.widget.Toast
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.github.zawadz88.materialpopupmenu.ViewBoundCallback
import com.github.zawadz88.materialpopupmenu.popupMenu
import com.github.zawadz88.materialpopupmenu.popupMenuBuilder

Expand Down Expand Up @@ -334,9 +335,14 @@ class DarkActivity : AppCompatActivity() {
}
customItem {
layoutResId = R.layout.view_custom_item_checkable
viewBoundCallback = { view ->
viewBoundCallback = ViewBoundCallback { view ->
val checkBox: CheckBox = view.findViewById(R.id.customItemCheckbox)
checkBox.isChecked = true
checkBox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
dismissPopup()
}
}
}
callback = {
Toast.makeText(this@DarkActivity, "Disabled!", Toast.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.core.content.ContextCompat
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.github.zawadz88.materialpopupmenu.ViewBoundCallback
import com.github.zawadz88.materialpopupmenu.popupMenu
import com.github.zawadz88.materialpopupmenu.popupMenuBuilder

Expand Down Expand Up @@ -313,9 +314,14 @@ class LightActivity : AppCompatActivity() {
}
customItem {
layoutResId = R.layout.view_custom_item_checkable
viewBoundCallback = { view ->
viewBoundCallback = ViewBoundCallback { view ->
val checkBox: CheckBox = view.findViewById(R.id.customItemCheckbox)
checkBox.isChecked = true
checkBox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
dismissPopup()
}
}
}
callback = {
Toast.makeText(this@LightActivity, "Disabled!", Toast.LENGTH_SHORT).show()
Expand Down
6 changes: 2 additions & 4 deletions sample/src/main/res/layout/view_custom_item_checkable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
android:id="@+id/customItemCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:clickable="false"
android:focusable="false" />
android:background="@null" />

</LinearLayout>
</LinearLayout>