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

Develop #205

Merged
merged 14 commits into from
Oct 15, 2019
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Change Log
All notable changes to this project will be documented in this file.

## 5.0.0 - 18/09/2019
* Added support for setting custom layout.
* Added set icon size.

## 4.1.1 - 25/09/2019
* Enabled Sound for the Alert
Expand All @@ -15,7 +18,6 @@ All notable changes to this project will be documented in this file.
## 4.0.2 - 6/3/2019
* Bug Fixes - Accept CharSequence as parameters instead of String on setTitle, setText, and addButton functions


## 4.0.1 - 27/2/2019
* Bug Fixes - Removed Unneeded LinearLayoutCompat

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Alerter.create(this@DemoActivity)
.setText("Alert text...")
.setIcon(R.drawable.alerter_ic_mail_outline)
.setIconColorFilter(0) // Optional - Removes white tint
.setIconSize(R.dimen.custom_icon_size) // Optional - default is 38dp
.show()
```

Expand Down Expand Up @@ -226,6 +227,19 @@ Alerter.create(this@DemoActivity)

![Verbose Alert](./documentation/alert_with_buttons.gif)

### With Custom Layout
```kotlin
Alerter.create(this@KotlinDemoActivity, R.layout.custom_layout)
.setBackgroundColorRes(R.color.colorAccent)
.also { alerter ->
val tvCustomView = alerter.getLayoutContainer()?.tvCustomLayout
tvCustomView?.setText(R.string.with_custom_layout)
}
.show()
```

![Verbose Alert](./documentation/alert_with_custom_layout.gif)

## Sample

Clone this repo and check out the `app-base` module.
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 = "4.1.1"
final String VERSION = "5.0.0"

final String DESCRIPTION = "An Android Alerting Library"
final String GITHUB_URL = "https://github.com/Tapadoo/Alerter"
Expand Down
85 changes: 61 additions & 24 deletions alerter/src/main/java/com/tapadoo/alerter/Alert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ import android.graphics.*
import android.graphics.drawable.Drawable
import android.media.RingtoneManager
import android.os.Build
import androidx.annotation.*
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.view.ContextThemeWrapper
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.view.*
import android.view.HapticFeedbackConstants
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.annotation.*
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import kotlinx.android.synthetic.main.alerter_alert_default_layout.view.*
import kotlinx.android.synthetic.main.alerter_alert_view.view.*

/**
Expand All @@ -29,7 +33,10 @@ import kotlinx.android.synthetic.main.alerter_alert_view.view.*
* @author Kevin Murphy, Tapadoo, Dublin, Ireland, Europe, Earth.
* @since 26/01/2016
*/
class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0)
class Alert @JvmOverloads constructor(context: Context,
@LayoutRes layoutId: Int,
attrs: AttributeSet? = null,
defStyle: Int = 0)
: FrameLayout(context, attrs, defStyle), View.OnClickListener, Animation.AnimationListener, SwipeDismissTouchListener.DismissCallbacks {

private var onShowListener: OnShowAlertListener? = null
Expand Down Expand Up @@ -84,8 +91,14 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
tvText?.layoutParams = paramsText
}

val layoutContainer: View? by lazy { findViewById<View>(R.id.vAlertContentContainer) }

init {
inflate(context, R.layout.alerter_alert_view, this)

vAlertContentContainer.layoutResource = layoutId
vAlertContentContainer.inflate()

isHapticFeedbackEnabled = true

ViewCompat.setTranslationZ(this, Integer.MAX_VALUE.toFloat())
Expand Down Expand Up @@ -191,7 +204,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
ivIcon?.startAnimation(AnimationUtils.loadAnimation(context, R.anim.alerter_pulse))
}
} else {
flIconContainer.visibility = View.GONE
flIconContainer?.visibility = View.GONE
}
}
}
Expand Down Expand Up @@ -335,8 +348,8 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
*/
fun setTitle(title: CharSequence) {
if (!TextUtils.isEmpty(title)) {
tvTitle.visibility = View.VISIBLE
tvTitle.text = title
tvTitle?.visibility = View.VISIBLE
tvTitle?.text = title
}
}

Expand All @@ -347,9 +360,9 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
*/
fun setTitleAppearance(@StyleRes textAppearance: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tvTitle.setTextAppearance(textAppearance)
tvTitle?.setTextAppearance(textAppearance)
} else {
tvTitle.setTextAppearance(tvText.context, textAppearance)
tvTitle?.setTextAppearance(tvText?.context, textAppearance)
}
}

Expand All @@ -359,7 +372,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param typeface The typeface to use
*/
fun setTitleTypeface(typeface: Typeface) {
tvTitle.typeface = typeface
tvTitle?.typeface = typeface
}

/**
Expand All @@ -368,7 +381,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param typeface The typeface to use
*/
fun setTextTypeface(typeface: Typeface) {
tvText.typeface = typeface
tvText?.typeface = typeface
}

/**
Expand All @@ -378,8 +391,8 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
*/
fun setText(text: CharSequence) {
if (!TextUtils.isEmpty(text)) {
tvText.visibility = View.VISIBLE
tvText.text = text
tvText?.visibility = View.VISIBLE
tvText?.text = text
}
}

Expand All @@ -390,9 +403,9 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
*/
fun setTextAppearance(@StyleRes textAppearance: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tvText.setTextAppearance(textAppearance)
tvText?.setTextAppearance(textAppearance)
} else {
tvText.setTextAppearance(tvText.context, textAppearance)
tvText?.setTextAppearance(tvText?.context, textAppearance)
}
}

Expand All @@ -402,7 +415,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param iconId Drawable resource id of the icon to use in the Alert
*/
fun setIcon(@DrawableRes iconId: Int) {
ivIcon.setImageDrawable(AppCompatResources.getDrawable(context, iconId))
ivIcon?.setImageDrawable(AppCompatResources.getDrawable(context, iconId))
}

/**
Expand All @@ -411,7 +424,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param color Color int
*/
fun setIconColorFilter(@ColorInt color: Int) {
ivIcon.setColorFilter(color)
ivIcon?.setColorFilter(color)
}

/**
Expand All @@ -420,7 +433,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param colorFilter ColorFilter
*/
fun setIconColorFilter(colorFilter: ColorFilter) {
ivIcon.colorFilter = colorFilter
ivIcon?.colorFilter = colorFilter
}

/**
Expand All @@ -430,7 +443,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param mode PorterDuff.Mode
*/
fun setIconColorFilter(@ColorInt color: Int, mode: PorterDuff.Mode) {
ivIcon.setColorFilter(color, mode)
ivIcon?.setColorFilter(color, mode)
}

/**
Expand All @@ -439,7 +452,7 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param bitmap Bitmap image of the icon to use in the Alert.
*/
fun setIcon(bitmap: Bitmap) {
ivIcon.setImageBitmap(bitmap)
ivIcon?.setImageBitmap(bitmap)
}

/**
Expand All @@ -448,7 +461,31 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n
* @param drawable Drawable image of the icon to use in the Alert.
*/
fun setIcon(drawable: Drawable) {
ivIcon.setImageDrawable(drawable)
ivIcon?.setImageDrawable(drawable)
}

/**
* Set the inline icon size for the Alert
*
* @param size Dimension int.
*/
fun setIconSize(@DimenRes size: Int) {
val pixelSize = context.resources.getDimensionPixelSize(size)
setIconPixelSize(pixelSize)
}

/**
* Set the inline icon size for the Alert
*
* @param size Icon size in pixel.
*/
fun setIconPixelSize(@Px size: Int) {
ivIcon.layoutParams = ivIcon.layoutParams.apply {
width = size
height = size
minimumWidth = size
minimumHeight = size
}
}

/**
Expand Down
42 changes: 41 additions & 1 deletion alerter/src/main/java/com/tapadoo/alerter/Alerter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,30 @@ class Alerter private constructor() {
return this
}

/**
* Set the Alert's Icon size
*
* @param size Dimension int.
* @return This Alerter
*/
fun setIconSize(@DimenRes size: Int): Alerter {
alert?.setIconSize(size)

return this
}

/**
* Set the Alert's Icon size
*
* @param size Icon size in pixel.
* @return This Alerter
*/
fun setIconPixelSize(@Px size: Int): Alerter {
alert?.setIconPixelSize(size)

return this
}

/**
* Set the icon color for the Alert
*
Expand Down Expand Up @@ -534,6 +558,10 @@ class Alerter private constructor() {
return this
}

fun getLayoutContainer(): View? {
return alert?.layoutContainer
}

/**
* Creates a weak reference to the calling Activity
*
Expand All @@ -555,6 +583,18 @@ class Alerter private constructor() {
*/
@JvmStatic
fun create(activity: Activity?): Alerter {
return create(activity, R.layout.alerter_alert_default_layout)
}

/**
* 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
* @return This Alerter
*/
@JvmStatic
fun create(activity: Activity?, @LayoutRes layoutId: Int): Alerter {
if (activity == null) {
throw IllegalArgumentException("Activity cannot be null!")
}
Expand All @@ -565,7 +605,7 @@ class Alerter private constructor() {
Alerter.clearCurrent(activity)

alerter.setActivity(activity)
alerter.alert = Alert(activity)
alerter.alert = Alert(activity, layoutId)

return alerter
}
Expand Down
Loading