Skip to content

Commit

Permalink
Merge pull request Tapadoo#156 from azisuazusa/153_center_vertical
Browse files Browse the repository at this point in the history
153 center vertical
  • Loading branch information
kpmmmurphy authored Oct 25, 2018
2 parents aff5e2d + 29dbb52 commit af0c577
Show file tree
Hide file tree
Showing 18 changed files with 150 additions and 132 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ A customisable Alert view is dynamically added to the Decor View of the Window,

```groovy
dependencies {
implementation 'com.tapadoo.android:alerter:3.0.0'
implementation 'com.tapadoo.android:alerter:3.0.1'
}
```

## Kotlin

Version 3.0.0 and above requires Kotlin

# Usage

![Default Alert](./documentation/alert_default.gif)
Expand Down
4 changes: 2 additions & 2 deletions alerter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apply plugin: 'org.jetbrains.dokka'
apply from: rootProject.file('quality.gradle')

final String GROUP_ID = "com.tapadoo.android"
final String VERSION = "3.0.0"
final String VERSION = "3.0.1"
final String DESCRIPTION = "An Android Alerting Library"
final String GITHUB_URL = "https://github.com/Tapadoo/Alerter"

Expand Down Expand Up @@ -114,7 +114,7 @@ dependencies {
implementation rootProject.ext.libs.appcompat_v7
implementation rootProject.ext.libs.support_annotations
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support.constraint:constraint-layout:1.1.0'


// Local Unit Tests - in src/test
testImplementation rootProject.ext.libs.junit
Expand Down
6 changes: 3 additions & 3 deletions alerter/src/androidTest/java/com/tapadoo/alerter/AlertTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AlertTest {
val alert = Alert(activityRule.activity)

//Ensure all elements are present
Assert.assertNotNull(alert.findViewById(R.id.flAlertBackground))
Assert.assertNotNull(alert.findViewById(R.id.llAlertBackground))
Assert.assertNotNull(alert.findViewById(R.id.tvTitle))
Assert.assertNotNull(alert.findViewById(R.id.tvText))
Assert.assertNotNull(alert.findViewById(R.id.ivIcon))
Expand Down Expand Up @@ -116,7 +116,7 @@ class AlertTest {
fun testBackgroundColour() {
alert.setAlertBackgroundColor(ContextCompat.getColor(activityRule.activity, android.R.color.darker_gray))

alert.findViewById<ViewGroup>(R.id.flAlertBackground)?.let {
alert.findViewById<ViewGroup>(R.id.llAlertBackground)?.let {
Assert.assertNotNull(it.background)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
Expand All @@ -138,7 +138,7 @@ class AlertTest {
return
}

alert.findViewById<ViewGroup>(R.id.flAlertBackground)?.let {
alert.findViewById<ViewGroup>(R.id.llAlertBackground)?.let {
//Check default onClickListener
Assert.assertTrue(it.hasOnClickListeners())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ class AlerterTest {
.setBackgroundColorRes(android.R.color.darker_gray)
.show()

Assert.assertNotNull(alert?.findViewById<ViewGroup>(R.id.flAlertBackground)?.background)
Assert.assertNotNull(alert?.findViewById<ViewGroup>(R.id.llAlertBackground)?.background)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Assert.assertEquals((alert?.findViewById<ViewGroup>(R.id.flAlertBackground)?.background as ColorDrawable).color.toLong(), ContextCompat.getColor(activityRule.activity, android.R.color.darker_gray).toLong())
Assert.assertEquals((alert?.findViewById<ViewGroup>(R.id.llAlertBackground)?.background as ColorDrawable).color.toLong(), ContextCompat.getColor(activityRule.activity, android.R.color.darker_gray).toLong())
}
}

Expand All @@ -117,13 +117,13 @@ class AlerterTest {
//Test default hide listener
val alert1 = Alerter.create(activityRule.activity).show()

Assert.assertTrue(alert1?.findViewById<ViewGroup>(R.id.flAlertBackground)?.hasOnClickListeners() ?: false)
Assert.assertTrue(alert1?.findViewById<ViewGroup>(R.id.llAlertBackground)?.hasOnClickListeners() ?: false)

//Test setting listener
val alert3 = Alerter.create(activityRule.activity).setOnClickListener(View.OnClickListener {
//Ignore
}).show()

Assert.assertTrue(alert3?.findViewById<ViewGroup>(R.id.flAlertBackground)?.hasOnClickListeners() ?: false)
Assert.assertTrue(alert3?.findViewById<ViewGroup>(R.id.llAlertBackground)?.hasOnClickListeners() ?: false)
}
}
50 changes: 28 additions & 22 deletions alerter/src/main/java/com/tapadoo/alerter/Alert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ import kotlinx.android.synthetic.main.alerter_alert_view.view.*
class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0)
: FrameLayout(context, attrs, defStyle), View.OnClickListener, Animation.AnimationListener, SwipeDismissTouchListener.DismissCallbacks {

internal var onShowListener: OnShowAlertListener? = null
private var onShowListener: OnShowAlertListener? = null
internal var onHideListener: OnHideAlertListener? = null

internal var enterAnimation: Animation = AnimationUtils.loadAnimation(context, R.anim.alerter_slide_in_from_top)
internal var exitAnimation: Animation = AnimationUtils.loadAnimation(context, R.anim.alerter_slide_out_to_top)

internal var duration = DISPLAY_TIME_IN_SECONDS

private var showIcon: Boolean = true
private var enableIconPulse = true
private var enableInfiniteDuration: Boolean = false
private var enableProgress: Boolean = false
Expand All @@ -64,7 +65,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param contentGravity Gravity of the Alert
*/
var contentGravity: Int
get() = (flAlertBackground!!.layoutParams as FrameLayout.LayoutParams).gravity
get() = (llAlertBackground!!.layoutParams as FrameLayout.LayoutParams).gravity
set(contentGravity) {
val paramsTitle = tvTitle?.layoutParams as LinearLayout.LayoutParams
paramsTitle.gravity = contentGravity
Expand All @@ -81,7 +82,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n

ViewCompat.setTranslationZ(this, Integer.MAX_VALUE.toFloat())

flAlertBackground.setOnClickListener(this)
llAlertBackground.setOnClickListener(this)
}

override fun onAttachedToWindow() {
Expand Down Expand Up @@ -121,7 +122,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
/* Override Methods */

override fun onTouchEvent(event: MotionEvent): Boolean {
performClick()
super.performClick()
return super.onTouchEvent(event)
}

Expand All @@ -132,7 +133,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
}

override fun setOnClickListener(listener: View.OnClickListener?) {
flAlertBackground.setOnClickListener(listener)
llAlertBackground.setOnClickListener(listener)
}

override fun setVisibility(visibility: Int) {
Expand All @@ -155,9 +156,14 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
if (enableProgress) {
ivIcon?.visibility = View.INVISIBLE
pbProgress?.visibility = View.VISIBLE
} else if (enableIconPulse) {
} else if (showIcon) {
ivIcon?.visibility = View.VISIBLE
ivIcon?.startAnimation(AnimationUtils.loadAnimation(context, R.anim.alerter_pulse))
// Only pulse if we're not showing the progress
if (enableIconPulse) {
ivIcon?.startAnimation(AnimationUtils.loadAnimation(context, R.anim.alerter_pulse))
}
} else {
flIconContainer.visibility = View.GONE
}
}
}
Expand Down Expand Up @@ -187,12 +193,12 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
/**
* Cleans up the currently showing alert view.
*/
fun hide() {
private fun hide() {
try {
exitAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation) {
flAlertBackground?.setOnClickListener(null)
flAlertBackground?.isClickable = false
llAlertBackground?.setOnClickListener(null)
llAlertBackground?.isClickable = false
}

override fun onAnimationEnd(animation: Animation) {
Expand Down Expand Up @@ -248,7 +254,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param color The qualified colour integer
*/
fun setAlertBackgroundColor(@ColorInt color: Int) {
flAlertBackground.setBackgroundColor(color)
llAlertBackground.setBackgroundColor(color)
}

/**
Expand All @@ -257,7 +263,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param resource The qualified drawable integer
*/
fun setAlertBackgroundResource(@DrawableRes resource: Int) {
flAlertBackground.setBackgroundResource(resource)
llAlertBackground.setBackgroundResource(resource)
}

/**
Expand All @@ -267,9 +273,9 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
*/
fun setAlertBackgroundDrawable(drawable: Drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
flAlertBackground.background = drawable
llAlertBackground.background = drawable
} else {
flAlertBackground.setBackgroundDrawable(drawable)
llAlertBackground.setBackgroundDrawable(drawable)
}
}

Expand Down Expand Up @@ -427,16 +433,16 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param showIcon True to show the icon, false otherwise
*/
fun showIcon(showIcon: Boolean) {
ivIcon.visibility = if (showIcon) View.VISIBLE else View.GONE
this.showIcon = showIcon
}

/**
* Set if the alerter is isDismissable or not
*
* @param dismissible True if alert can be dismissed
*/
fun setDismissable(dismissable: Boolean) {
this.isDismissable = dismissable
fun setDismissible(dismissible: Boolean) {
this.isDismissable = dismissible
}

/**
Expand All @@ -451,7 +457,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* Set whether to enable swipe to dismiss or not
*/
fun enableSwipeToDismiss() {
flAlertBackground.let {
llAlertBackground.let {
it.setOnTouchListener(SwipeDismissTouchListener(it, object : SwipeDismissTouchListener.DismissCallbacks {
override fun canDismiss(): Boolean {
return true
Expand Down Expand Up @@ -546,7 +552,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
}

// Alter padding
flAlertBackground?.apply {
llAlertBackground?.apply {
this.setPadding(this.paddingLeft, this.paddingTop, this.paddingRight, this.paddingBottom / 2)
}
}
Expand All @@ -556,7 +562,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
}

override fun onDismiss(view: View) {
flClickShield?.removeView(flAlertBackground)
flClickShield?.removeView(llAlertBackground)
}

override fun onTouch(view: View, touch: Boolean) {
Expand All @@ -569,12 +575,12 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n

companion object {

private val CLEAN_UP_DELAY_MILLIS = 100
private const val CLEAN_UP_DELAY_MILLIS = 100

/**
* The amount of time the alert will be visible on screen in seconds
*/
private val DISPLAY_TIME_IN_SECONDS: Long = 3000
private val MUL = -0x1000000
private const val MUL = -0x1000000
}
}
4 changes: 2 additions & 2 deletions alerter/src/main/java/com/tapadoo/alerter/Alerter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ class Alerter private constructor() {
* @return This Alerter
*/
fun setDismissable(dismissable: Boolean): Alerter {
alert?.setDismissable(dismissable)
alert?.setDismissible(dismissable)

return this
}
Expand Down Expand Up @@ -582,7 +582,7 @@ class Alerter private constructor() {
var isShowing = false

activityWeakReference?.get()?.let {
isShowing = it.findViewById<View>(R.id.flAlertBackground) != null
isShowing = it.findViewById<View>(R.id.llAlertBackground) != null
}

return isShowing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import android.view.ViewConfiguration
* A [View.OnTouchListener] that makes any [View] dismissable when the
* user swipes (drags her finger) horizontally across the view.
*
* @param view The view to make dismissable.
* @param callbacks The callback to trigger when the user has indicated that she would like to
* @param mView The view to make dismissable.
* @param mCallbacks The callback to trigger when the user has indicated that she would like to
* dismiss this view.
*/
internal class SwipeDismissTouchListener(
Expand Down
6 changes: 2 additions & 4 deletions alerter/src/main/res/layout/activity_mock.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_mock"
<RelativeLayout android:id="@+id/activity_mock"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/alerter_activity_vertical_margin"
android:paddingLeft="@dimen/alerter_activity_horizontal_margin"
android:paddingRight="@dimen/alerter_activity_horizontal_margin"
android:paddingTop="@dimen/alerter_activity_vertical_margin"/>
android:paddingTop="@dimen/alerter_activity_vertical_margin" />
Loading

0 comments on commit af0c577

Please sign in to comment.