Skip to content

Commit

Permalink
Merge pull request #213 from Tapadoo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kpmmmurphy authored Jan 29, 2020
2 parents 7fa64cc + 4aa4a89 commit d7f28e3
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 52 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Change Log
All notable changes to this project will be documented in this file.

## 5.1.1 - 28/01/2020
* Minor refactoring

## 5.1.0 - 28/01/2020
* Added support for center or bottom gravity

## 5.0.0 - 18/09/2019
* Added support for setting custom layout.
* Added set icon size.
Expand Down
2 changes: 1 addition & 1 deletion alerter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply from: rootProject.file('quality.gradle')

final String GROUP_ID = "com.tapadoo.android"

final String VERSION = "5.0.0"
final String VERSION = "5.1.1"

final String DESCRIPTION = "An Android Alerting Library"
final String GITHUB_URL = "https://github.com/Tapadoo/Alerter"
Expand Down
97 changes: 63 additions & 34 deletions alerter/src/main/java/com/tapadoo/alerter/Alert.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tapadoo.alerter

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.Activity
import android.content.Context
import android.graphics.*
import android.graphics.drawable.Drawable
Expand All @@ -10,10 +10,7 @@ import android.os.Build
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.view.HapticFeedbackConstants
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.*
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Button
Expand All @@ -24,6 +21,9 @@ import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.widget.TextViewCompat
import com.tapadoo.alerter.utils.getDimenPixelSize
import com.tapadoo.alerter.utils.notchHeight
import kotlinx.android.synthetic.main.alerter_alert_default_layout.view.*
import kotlinx.android.synthetic.main.alerter_alert_view.view.*

Expand All @@ -33,6 +33,7 @@ import kotlinx.android.synthetic.main.alerter_alert_view.view.*
* @author Kevin Murphy, Tapadoo, Dublin, Ireland, Europe, Earth.
* @since 26/01/2016
*/
@SuppressLint("ViewConstructor")
class Alert @JvmOverloads constructor(context: Context,
@LayoutRes layoutId: Int,
attrs: AttributeSet? = null,
Expand All @@ -54,7 +55,7 @@ class Alert @JvmOverloads constructor(context: Context,

private var runningAnimation: Runnable? = null

private var isDismissable = true
private var isDismissible = true

private var buttons = ArrayList<Button>()
var buttonTypeFace: Typeface? = null
Expand All @@ -74,17 +75,34 @@ class Alert @JvmOverloads constructor(context: Context,
*/
private var soundEnabled = false

/**
* Sets the Layout Gravity of the Alert
*
* @param layoutGravity Layout Gravity of the Alert
*/
var layoutGravity = Gravity.TOP
set(value) {

if (value != Gravity.TOP) {
enterAnimation = AnimationUtils.loadAnimation(context, R.anim.alerter_slide_in_from_bottom)
exitAnimation = AnimationUtils.loadAnimation(context, R.anim.alerter_slide_out_to_bottom)
}

field = value
}

/**
* Sets the Gravity of the Alert
*
* @param contentGravity Gravity of the Alert
*/
var contentGravity: Int
get() = (llAlertBackground?.layoutParams as FrameLayout.LayoutParams).gravity
get() = (llAlertBackground?.layoutParams as LayoutParams).gravity
set(contentGravity) {
val paramsTitle = tvTitle?.layoutParams as? LinearLayout.LayoutParams
paramsTitle?.gravity = contentGravity
tvTitle?.layoutParams = paramsTitle

(tvTitle?.layoutParams as? LinearLayout.LayoutParams)?.apply {
gravity = contentGravity
}

val paramsText = tvText?.layoutParams as? LinearLayout.LayoutParams
paramsText?.gravity = contentGravity
Expand All @@ -109,6 +127,24 @@ class Alert @JvmOverloads constructor(context: Context,
override fun onAttachedToWindow() {
super.onAttachedToWindow()

llAlertBackground.apply {

(layoutParams as LayoutParams).gravity = layoutGravity

if (layoutGravity != Gravity.TOP) {
setPadding(
0, getDimenPixelSize(R.dimen.alerter_padding_default),
0, getDimenPixelSize(R.dimen.alerter_alert_padding)
)
}
}

(layoutParams as MarginLayoutParams).apply {
if (layoutGravity != Gravity.TOP) {
bottomMargin = getDimenPixelSize(R.dimen.navigation_bar_height)
}
}

enterAnimation.setAnimationListener(this)

// Set Animation to be Run when View is added to Window
Expand All @@ -126,16 +162,12 @@ class Alert @JvmOverloads constructor(context: Context,
marginSet = true

// Add a negative top margin to compensate for overshoot enter animation
val params = layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = context.resources.getDimensionPixelSize(R.dimen.alerter_alert_negative_margin_top)
(layoutParams as MarginLayoutParams).topMargin = getDimenPixelSize(R.dimen.alerter_alert_negative_margin_top)

// Check for Cutout
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val displayCutout = (context as? Activity)?.window?.decorView?.rootWindowInsets?.displayCutout

val notchHeight = displayCutout?.safeInsetTop ?: 0
llAlertBackground.apply {
setPadding(paddingLeft, paddingTop + (notchHeight / 2), paddingRight, paddingBottom)
setPadding(paddingLeft, paddingTop + (notchHeight() / 2), paddingRight, paddingBottom)
}
}
}
Expand All @@ -152,18 +184,19 @@ class Alert @JvmOverloads constructor(context: Context,

/* Override Methods */

@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
super.performClick()
return super.onTouchEvent(event)
}

override fun onClick(v: View) {
if (isDismissable) {
if (isDismissible) {
hide()
}
}

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

Expand Down Expand Up @@ -309,11 +342,7 @@ class Alert @JvmOverloads constructor(context: Context,
* @param drawable The qualified drawable
*/
fun setAlertBackgroundDrawable(drawable: Drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
llAlertBackground.background = drawable
} else {
llAlertBackground.setBackgroundDrawable(drawable)
}
ViewCompat.setBackground(llAlertBackground, drawable)
}

/**
Expand Down Expand Up @@ -362,7 +391,7 @@ class Alert @JvmOverloads constructor(context: Context,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tvTitle?.setTextAppearance(textAppearance)
} else {
tvTitle?.setTextAppearance(tvText?.context, textAppearance)
TextViewCompat.setTextAppearance(tvTitle, textAppearance)
}
}

Expand Down Expand Up @@ -405,7 +434,7 @@ class Alert @JvmOverloads constructor(context: Context,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tvText?.setTextAppearance(textAppearance)
} else {
tvText?.setTextAppearance(tvText?.context, textAppearance)
TextViewCompat.setTextAppearance(tvText, textAppearance)
}
}

Expand Down Expand Up @@ -470,7 +499,7 @@ class Alert @JvmOverloads constructor(context: Context,
* @param size Dimension int.
*/
fun setIconSize(@DimenRes size: Int) {
val pixelSize = context.resources.getDimensionPixelSize(size)
val pixelSize = getDimenPixelSize(size)
setIconPixelSize(pixelSize)
}

Expand Down Expand Up @@ -498,20 +527,20 @@ class Alert @JvmOverloads constructor(context: Context,
}

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

/**
* Get if the alert is isDismissable
* Get if the alert is isDismissible
* @return
*/
fun isDismissable(): Boolean {
return isDismissable
fun isDismissible(): Boolean {
return isDismissible
}

/**
Expand Down Expand Up @@ -613,7 +642,7 @@ class Alert @JvmOverloads constructor(context: Context,
* @param text The text to display on the button
* @param onClick The on click listener
*/
fun addButton(text: CharSequence, @StyleRes style: Int, onClick: View.OnClickListener) {
fun addButton(text: CharSequence, @StyleRes style: Int, onClick: OnClickListener) {
Button(ContextThemeWrapper(context, style), null, style).apply {
this.text = text
this.setOnClickListener(onClick)
Expand All @@ -628,7 +657,7 @@ class Alert @JvmOverloads constructor(context: Context,
}

override fun canDismiss(): Boolean {
return isDismissable
return isDismissible
}

override fun onDismiss(view: View) {
Expand All @@ -650,7 +679,7 @@ class Alert @JvmOverloads constructor(context: Context,
/**
* The amount of time the alert will be visible on screen in seconds
*/
private val DISPLAY_TIME_IN_SECONDS: Long = 3000
private const val DISPLAY_TIME_IN_SECONDS: Long = 3000
private const val MUL = -0x1000000
}
}
38 changes: 24 additions & 14 deletions alerter/src/main/java/com/tapadoo/alerter/Alerter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import android.graphics.ColorFilter
import android.graphics.PorterDuff
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import androidx.annotation.*
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import androidx.annotation.*
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import java.lang.ref.WeakReference

/**
Expand Down Expand Up @@ -111,6 +111,18 @@ class Alerter private constructor() {
return this
}

/**
* Set Layout Gravity of the Alert
*
* @param layoutGravity of Alert
* @return This Alerter
*/
fun setLayoutGravity(layoutGravity: Int): Alerter {
alert?.layoutGravity = layoutGravity

return this
}

/**
* Set Gravity of the Alert
*
Expand Down Expand Up @@ -496,13 +508,13 @@ class Alerter private constructor() {
}

/**
* Set if the Alert is dismissable or not
* Set if the Alert is dismissible or not
*
* @param dismissable true if it can be dismissed
* @param dismissible true if it can be dismissed
* @return This Alerter
*/
fun setDismissable(dismissable: Boolean): Alerter {
alert?.setDismissible(dismissable)
fun setDismissable(dismissible: Boolean): Alerter {
alert?.setDismissible(dismissible)

return this
}
Expand Down Expand Up @@ -538,8 +550,8 @@ class Alerter private constructor() {
* @param onClick The on click listener
*/
fun addButton(
text: CharSequence, @StyleRes style: Int = R.style.AlertButton,
onClick: View.OnClickListener
text: CharSequence, @StyleRes style: Int = R.style.AlertButton,
onClick: View.OnClickListener
): Alerter {
alert?.addButton(text, style, onClick)

Expand Down Expand Up @@ -590,19 +602,17 @@ class Alerter private constructor() {
* Creates the Alert with custom view, and maintains a reference to the calling Activity
*
* @param activity The calling Activity
* @param customLayoutId Custom view layout res id
* @param layoutId Custom view layout res id
* @return This Alerter
*/
@JvmStatic
fun create(activity: Activity?, @LayoutRes layoutId: Int): Alerter {
if (activity == null) {
throw IllegalArgumentException("Activity cannot be null!")
}
requireNotNull(activity) { "Activity cannot be null!" }

val alerter = Alerter()

//Hide current Alert, if one is active
Alerter.clearCurrent(activity)
clearCurrent(activity)

alerter.setActivity(activity)
alerter.alert = Alert(activity, layoutId)
Expand Down
13 changes: 13 additions & 0 deletions alerter/src/main/java/com/tapadoo/alerter/utils/ExAlert.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.tapadoo.alerter.utils

import android.app.Activity
import android.os.Build
import androidx.annotation.DimenRes
import androidx.annotation.RequiresApi
import com.tapadoo.alerter.Alert

fun Alert.getDimenPixelSize(@DimenRes id: Int) = resources.getDimensionPixelSize(id)

@RequiresApi(Build.VERSION_CODES.P)
fun Alert.notchHeight() = (context as? Activity)?.window?.decorView?.rootWindowInsets?.displayCutout?.safeInsetTop
?: 0
10 changes: 10 additions & 0 deletions alerter/src/main/res/anim/alerter_slide_in_from_bottom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="700"
android:interpolator="@anim/interpolator_slight_overshoot">

<translate
android:fromYDelta="100%"
android:toYDelta="0%"/>

</set>
11 changes: 11 additions & 0 deletions alerter/src/main/res/anim/alerter_slide_out_to_bottom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fillAfter="true"
android:interpolator="@anim/interpolator_slight_anticipate">

<translate
android:fromYDelta="0%"
android:toYDelta="100%"/>

</set>
Loading

0 comments on commit d7f28e3

Please sign in to comment.