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

- Added dismiss functionality regarding issue 140 #141

Merged
merged 2 commits into from
Dec 17, 2022
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
1 change: 1 addition & 0 deletions powerspinner/api/powerspinner.api
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public abstract interface class com/skydoves/powerspinner/OnSpinnerOutsideTouchL

public final class com/skydoves/powerspinner/PowerSpinnerExtensionKt {
public static final synthetic fun createPowerSpinnerView (Landroid/content/Context;Lkotlin/jvm/functions/Function1;)Lcom/skydoves/powerspinner/PowerSpinnerView;
public static final fun dismissPowerSpinner (Landroid/view/View;)V
}

public abstract interface class com/skydoves/powerspinner/PowerSpinnerInterface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package com.skydoves.powerspinner

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.skydoves.powerspinner.internals.PowerSpinnerDsl

/** creates an instance of [PowerSpinnerView] by [PowerSpinnerView.Builder] using kotlin dsl. */
Expand All @@ -29,3 +31,37 @@ public inline fun createPowerSpinnerView(
builder: PowerSpinnerView.Builder.() -> Unit
): PowerSpinnerView =
PowerSpinnerView.Builder(context).apply(builder).build()

/**
* Extension function from [View] class (@NULLABLE).
* Should be used in cases when the [PowerSpinnerView.dismiss] does not seem to work as expected.
*
* Iterates all the Parent view's children, If a power spinner can be found
* Then dismiss it.
*
* Usage -> parentView?.dismissPowerSpinner()
*/
public fun View?.dismissPowerSpinner() {
this?.getAllChildren()?.forEach {
if (it is PowerSpinnerView) {
it.dismiss()
return
}
}
}

// Returns all the View's children view
private fun View.getAllChildren(): List<View> {
val viewList = ArrayList<View>()
if (this !is ViewGroup) {
// Has no children
viewList.add(this)
} else {
// Iterate all the view children, with recursion
for (index in 0 until this.childCount) {
val child = this.getChildAt(index)
viewList.addAll(child.getAllChildren())
}
}
return viewList
}