-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Dax dialog with two buttons #713
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
Changes from all commits
afc6f48
927d345
704af49
d9d5b3d
b847031
5227754
afbcba2
2ee17bc
4c5a1bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,30 +22,44 @@ import android.graphics.Color | |
| import android.graphics.Rect | ||
| import android.graphics.drawable.ColorDrawable | ||
| import android.os.Bundle | ||
| import android.view.Gravity | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.fragment.app.DialogFragment | ||
| import com.duckduckgo.app.browser.R | ||
| import android.view.Gravity | ||
| import kotlinx.android.synthetic.main.content_dax_dialog.* | ||
| import android.view.animation.Animation | ||
| import android.view.animation.OvershootInterpolator | ||
| import android.view.animation.ScaleAnimation | ||
| import android.widget.ImageView | ||
| import android.widget.RelativeLayout | ||
| import androidx.core.content.ContextCompat.getColor | ||
| import androidx.fragment.app.DialogFragment | ||
| import com.duckduckgo.app.browser.R | ||
| import kotlinx.android.synthetic.main.content_dax_dialog.* | ||
|
|
||
| class DaxDialog( | ||
| var daxText: String, | ||
| var buttonText: String, | ||
| interface DaxDialog { | ||
| fun setDaxText(daxText: String) | ||
| fun setButtonText(buttonText: String) | ||
| fun setDialogAndStartAnimation() | ||
| fun onAnimationFinishedListener(onAnimationFinished: () -> Unit) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, |
||
| fun setPrimaryCtaClickListener(clickListener: () -> Unit) | ||
| fun setSecondaryCtaClickListener(clickListener: () -> Unit) | ||
| fun setHideClickListener(clickListener: () -> Unit) | ||
| fun setDismissListener(clickListener: () -> Unit) | ||
| fun getDaxDialog(): DialogFragment | ||
| } | ||
|
|
||
| class TypewriterDaxDialog( | ||
| private var daxText: String, | ||
| private var primaryButtonText: String, | ||
| private var secondaryButtonText: String? = "", | ||
| private val toolbarDimmed: Boolean = true, | ||
| private val dismissible: Boolean = true, | ||
| private val typingDelayInMs: Long = 20 | ||
| ) : DialogFragment() { | ||
| ) : DialogFragment(), DaxDialog { | ||
|
|
||
| private var onAnimationFinished: () -> Unit = {} | ||
| private var primaryCtaClickListener: () -> Unit = { dismiss() } | ||
| private var secondaryCtaClickListener: (() -> Unit)? = null | ||
| private var hideClickListener: () -> Unit = { dismiss() } | ||
| private var dismissListener: () -> Unit = { } | ||
|
|
||
|
|
@@ -60,11 +74,13 @@ class DaxDialog( | |
| attributes?.gravity = Gravity.BOTTOM | ||
| window?.attributes = attributes | ||
| window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) | ||
| setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_NoActionBar) | ||
|
|
||
| return dialog | ||
| } | ||
|
|
||
| override fun getTheme(): Int { | ||
| return R.style.DaxDialogFragment | ||
| } | ||
|
|
||
| override fun onStart() { | ||
| super.onStart() | ||
| dialog?.window?.attributes?.dimAmount = 0f | ||
|
|
@@ -82,40 +98,40 @@ class DaxDialog( | |
| super.onDismiss(dialog) | ||
| } | ||
|
|
||
| fun setDialogAndStartAnimation() { | ||
| override fun getDaxDialog(): DialogFragment = this | ||
|
|
||
| override fun setDaxText(daxText: String) { | ||
| this.daxText = daxText | ||
| } | ||
|
|
||
| override fun setButtonText(buttonText: String) { | ||
| this.primaryButtonText = buttonText | ||
| } | ||
|
|
||
| override fun setDialogAndStartAnimation() { | ||
| setDialog() | ||
| setListeners() | ||
| dialogText.startTypingAnimation(daxText, true, onAnimationFinished) | ||
| } | ||
|
|
||
| fun onAnimationFinishedListener(onAnimationFinished: () -> Unit) { | ||
| override fun onAnimationFinishedListener(onAnimationFinished: () -> Unit) { | ||
| this.onAnimationFinished = onAnimationFinished | ||
| } | ||
|
|
||
| fun setPrimaryCtaClickListener(clickListener: () -> Unit) { | ||
| override fun setPrimaryCtaClickListener(clickListener: () -> Unit) { | ||
| primaryCtaClickListener = clickListener | ||
| } | ||
|
|
||
| fun setHideClickListener(clickListener: () -> Unit) { | ||
| hideClickListener = clickListener | ||
| } | ||
|
|
||
| fun setDismissListener(clickListener: () -> Unit) { | ||
| dismissListener = clickListener | ||
| override fun setSecondaryCtaClickListener(clickListener: () -> Unit) { | ||
| secondaryCtaClickListener = clickListener | ||
| } | ||
|
|
||
| fun startHighlightViewAnimation(targetView: View, duration: Long = 400, timesBigger: Float = 0f) { | ||
| val highlightImageView = addHighlightView(targetView, timesBigger) | ||
| val scaleAnimation = buildScaleAnimation(duration) | ||
| highlightImageView?.startAnimation(scaleAnimation) | ||
| override fun setHideClickListener(clickListener: () -> Unit) { | ||
| hideClickListener = clickListener | ||
| } | ||
|
|
||
| private fun buildScaleAnimation(duration: Long = 400): Animation { | ||
| val scaleAnimation = ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f) | ||
| scaleAnimation.duration = duration | ||
| scaleAnimation.fillAfter = true | ||
| scaleAnimation.interpolator = OvershootInterpolator(OVERSHOOT_TENSION) | ||
| return scaleAnimation | ||
| override fun setDismissListener(clickListener: () -> Unit) { | ||
| dismissListener = clickListener | ||
| } | ||
|
|
||
| private fun setListeners() { | ||
|
|
@@ -129,6 +145,13 @@ class DaxDialog( | |
| primaryCtaClickListener() | ||
| } | ||
|
|
||
| secondaryCtaClickListener?.let { | ||
| secondaryCta.setOnClickListener { | ||
| dialogText.cancelAnimation() | ||
| it() | ||
| } | ||
| } | ||
|
|
||
| if (dismissible) { | ||
| dialogContainer.setOnClickListener { | ||
| dialogText.cancelAnimation() | ||
|
|
@@ -147,14 +170,35 @@ class DaxDialog( | |
| val toolbarColor = if (toolbarDimmed) getColor(it, R.color.dimmed) else getColor(it, android.R.color.transparent) | ||
| toolbarDialogLayout.setBackgroundColor(toolbarColor) | ||
| hiddenText.text = daxText.html(it) | ||
| primaryCta.text = buttonText | ||
| primaryCta.text = primaryButtonText | ||
| secondaryCta.text = secondaryButtonText | ||
| secondaryCta.visibility = if (secondaryButtonText.isNullOrEmpty()) View.GONE else View.VISIBLE | ||
| dialogText.typingDelayInMs = typingDelayInMs | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class DaxDialogHighlightView( | ||
| daxDialog: TypewriterDaxDialog | ||
| ) : DaxDialog by daxDialog { | ||
|
|
||
| fun startHighlightViewAnimation(targetView: View, duration: Long = 400, timesBigger: Float = 0f) { | ||
| val highlightImageView = addHighlightView(targetView, timesBigger) | ||
| val scaleAnimation = buildScaleAnimation(duration) | ||
| highlightImageView?.startAnimation(scaleAnimation) | ||
| } | ||
|
|
||
| private fun buildScaleAnimation(duration: Long = 400): Animation { | ||
| val scaleAnimation = ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f) | ||
| scaleAnimation.duration = duration | ||
| scaleAnimation.fillAfter = true | ||
| scaleAnimation.interpolator = OvershootInterpolator(OVERSHOOT_TENSION) | ||
| return scaleAnimation | ||
| } | ||
|
|
||
| private fun addHighlightView(targetView: View, timesBigger: Float): View? { | ||
| return activity?.let { | ||
| val highlightImageView = ImageView(context) | ||
| return getDaxDialog().activity?.let { | ||
| val highlightImageView = ImageView(getDaxDialog().context) | ||
| highlightImageView.id = View.generateViewId() | ||
| highlightImageView.setImageResource(R.drawable.ic_circle) | ||
|
|
||
|
|
@@ -174,7 +218,7 @@ class DaxDialog( | |
| val params = RelativeLayout.LayoutParams((width + timesBiggerX).toInt(), (height + timesBiggerY).toInt()) | ||
| params.leftMargin = locationOnScreen[0] - (timesBiggerX / 2).toInt() | ||
| params.topMargin = (locationOnScreen[1] - statusBarHeight) - (timesBiggerY / 2).toInt() | ||
| dialogContainer.addView(highlightImageView, params) | ||
| getDaxDialog().dialogContainer.addView(highlightImageView, params) | ||
| highlightImageView | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this ok being launched in the main thread?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ApiBasedPixel:Pixelwill excecute it in a background thread. So we are good here.