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

Add onHideListener to hide() & clearCurrent() methods #260

Merged
Changes from 1 commit
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
Next Next commit
Added onHideListener to hide() & clearCurrent() methods
  • Loading branch information
jassielcastro committed May 13, 2021
commit 34ac70737b40914502d0bc909d0e6041b69d1acc
22 changes: 14 additions & 8 deletions alerter/src/main/java/com/tapadoo/alerter/Alerter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -770,34 +770,38 @@ class Alerter private constructor() {
*
* @param activity The current Activity
* @param dialog The current Dialog
* @param listener OnHideAlertListener to known when Alert is dismissed
*/
@JvmStatic
fun clearCurrent(activity: Activity?, dialog: Dialog?) {
@JvmOverloads
fun clearCurrent(activity: Activity?, dialog: Dialog?, listener: OnHideAlertListener? = null) {
dialog?.let {
it.window?.decorView as? ViewGroup
} ?: kotlin.run {
activity?.window?.decorView as? ViewGroup
}?.also {
removeAlertFromParent(it)
removeAlertFromParent(it, listener)
}
}

/**
* Hides the currently showing alert view, if one is present
* @param listener to known when Alert is dismissed
*/
@JvmStatic
fun hide() {
@JvmOverloads
fun hide(listener: OnHideAlertListener? = null) {
decorView?.get()?.let {
removeAlertFromParent(it)
removeAlertFromParent(it, listener)
}
}

private fun removeAlertFromParent(decorView: ViewGroup) {
private fun removeAlertFromParent(decorView: ViewGroup, listener: OnHideAlertListener?) {
//Find all Alert Views in Parent layout
for (i in 0..decorView.childCount) {
val childView = if (decorView.getChildAt(i) is Alert) decorView.getChildAt(i) as Alert else null
if (childView != null && childView.windowToken != null) {
ViewCompat.animate(childView).alpha(0f).withEndAction(getRemoveViewRunnable(childView))
ViewCompat.animate(childView).alpha(0f).withEndAction(getRemoveViewRunnable(childView, listener))
}
}
}
Expand All @@ -819,10 +823,12 @@ class Alerter private constructor() {
return isShowing
}

private fun getRemoveViewRunnable(childView: Alert?): Runnable {
private fun getRemoveViewRunnable(childView: Alert?, listener: OnHideAlertListener?): Runnable {
return Runnable {
childView?.let {
(childView.parent as? ViewGroup)?.removeView(childView)
(childView.parent as? ViewGroup)?.removeView(childView).also {
listener?.onHide()
}
}
}
}
Expand Down