Skip to content

Commit

Permalink
Merge pull request #252 from Tapadoo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kpmmmurphy committed Jan 29, 2021
2 parents 9fa7618 + 6d6bab3 commit 81a577c
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 119 deletions.
3 changes: 3 additions & 0 deletions 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.

## 7.0.0 - 27/01/2021
* Added support in Dialog.

## 6.2.1 - 03/09/2020
* Updated negative margin to fix overshoot on Samsung S10

Expand Down
6 changes: 3 additions & 3 deletions 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 = "6.2.1"
final String VERSION = "7.0.0"

final String DESCRIPTION = "An Android Alerting Library"
final String GITHUB_URL = "https://github.com/Tapadoo/Alerter"
Expand Down Expand Up @@ -124,9 +124,9 @@ configurations {
}

dependencies {
implementation "androidx.appcompat:appcompat:1.1.0"
implementation "androidx.annotation:annotation:1.1.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "androidx.annotation:annotation:1.1.0"
implementation "androidx.appcompat:appcompat:1.2.0"

// Local Unit Tests - in src/test
testImplementation 'junit:junit:4.12'
Expand Down
120 changes: 64 additions & 56 deletions alerter/src/main/java/com/tapadoo/alerter/Alerter.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.tapadoo.alerter

import android.app.Activity
import android.app.Dialog
import android.graphics.Bitmap
import android.graphics.ColorFilter
import android.graphics.PorterDuff
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.media.RingtoneManager
import android.net.Uri
import android.os.Looper
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import androidx.annotation.*
import androidx.appcompat.app.AppCompatDialog
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import java.lang.ref.WeakReference
Expand All @@ -32,33 +35,16 @@ class Alerter private constructor() {
*/
private var alert: Alert? = null

/**
* Get the enclosing Decor View
*
* @return The Decor View of the Activity the Alerter was called from
*/
private val activityDecorView: ViewGroup?
get() {
var decorView: ViewGroup? = null

activityWeakReference?.get()?.let {
decorView = it.window.decorView as ViewGroup
}

return decorView
}

/**
* Shows the Alert, after it's built
*
* @return An Alert object check can be altered or hidden
*/
fun show(): Alert? {
//This will get the Activity Window's DecorView
activityWeakReference?.get()?.let {
it.runOnUiThread {
//Add the new Alert to the View Hierarchy
activityDecorView?.addView(alert)
decorView?.get()?.let {
android.os.Handler(Looper.getMainLooper()).post {
it.addView(alert)
}
}

Expand Down Expand Up @@ -204,8 +190,8 @@ class Alerter private constructor() {
* @return This Alerter
*/
fun setBackgroundColorRes(@ColorRes colorResId: Int): Alerter {
activityWeakReference?.get()?.let {
alert?.setAlertBackgroundColor(ContextCompat.getColor(it, colorResId))
decorView?.get()?.let {
alert?.setAlertBackgroundColor(ContextCompat.getColor(it.context.applicationContext, colorResId))
}

return this
Expand Down Expand Up @@ -607,6 +593,7 @@ class Alerter private constructor() {

return this
}

/**
* Disable touch events outside of the Alert
*
Expand Down Expand Up @@ -721,67 +708,78 @@ class Alerter private constructor() {
return alert?.layoutContainer
}

/**
* Creates a weak reference to the calling Activity
*
* @param activity The calling Activity
*/
private fun setActivity(activity: Activity) {
activityWeakReference = WeakReference(activity)
}

companion object {

private var activityWeakReference: WeakReference<Activity>? = null
private var decorView: WeakReference<ViewGroup>? = null

/**
* Creates the Alert, and maintains a reference to the calling Activity
* Creates the Alert
*
* @param activity The calling Activity
* @return This Alerter
*/
@JvmStatic
fun create(activity: Activity?): Alerter {
return create(activity, R.layout.alerter_alert_default_layout)
@JvmOverloads
fun create(activity: Activity, layoutId: Int = R.layout.alerter_alert_default_layout): Alerter {
return create(activity = activity, dialog = null, layoutId = layoutId)
}

/**
* Creates the Alert
*
* @param dialog The calling Dialog
* @return This Alerter
*/
@JvmStatic
@JvmOverloads
fun create(dialog: Dialog, layoutId: Int = R.layout.alerter_alert_default_layout): Alerter {
return create(activity = null, dialog = dialog, layoutId = layoutId)
}

/**
* Creates the Alert with custom view, and maintains a reference to the calling Activity
* Creates the Alert with custom view, and maintains a reference to the calling Activity or Dialog's
* DecorView
*
* @param activity The calling Activity
* @param dialog The calling Dialog
* @param layoutId Custom view layout res id
* @return This Alerter
*/
@JvmStatic
fun create(activity: Activity?, @LayoutRes layoutId: Int): Alerter {
requireNotNull(activity) { "Activity cannot be null!" }

private fun create(activity: Activity? = null, dialog: Dialog? = null, @LayoutRes layoutId: Int): Alerter {
val alerter = Alerter()

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

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

alerter.alert = dialog?.window?.let {
decorView = WeakReference(it.decorView as ViewGroup)
Alert(context = it.decorView.context, layoutId = layoutId)
} ?: run {
activity?.window?.let {
decorView = WeakReference(it.decorView as ViewGroup)
Alert(context = it.decorView.context, layoutId = layoutId)
}
}

return alerter
}

/**
* Cleans up the currently showing alert view, if one is present
* Cleans up the currently showing alert view, if one is present. Either pass
* the calling Activity, or the calling Dialog
*
* @param activity The current Activity
* @param dialog The current Dialog
*/
@JvmStatic
fun clearCurrent(activity: Activity?) {
(activity?.window?.decorView as? ViewGroup)?.let {
//Find all Alert Views in Parent layout
for (i in 0..it.childCount) {
val childView = if (it.getChildAt(i) is Alert) it.getChildAt(i) as Alert else null
if (childView != null && childView.windowToken != null) {
ViewCompat.animate(childView).alpha(0f).withEndAction(getRemoveViewRunnable(childView))
}
}
fun clearCurrent(activity: Activity?, dialog: Dialog?) {
dialog?.let {
it.window?.decorView as? ViewGroup
} ?: kotlin.run {
activity?.window?.decorView as? ViewGroup
}?.also {
removeAlertFromParent(it)
}
}

Expand All @@ -790,8 +788,18 @@ class Alerter private constructor() {
*/
@JvmStatic
fun hide() {
activityWeakReference?.get()?.let {
clearCurrent(it)
decorView?.get()?.let {
removeAlertFromParent(it)
}
}

private fun removeAlertFromParent(decorView: ViewGroup) {
//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))
}
}
}

Expand All @@ -805,7 +813,7 @@ class Alerter private constructor() {
get() {
var isShowing = false

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

Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ android {

dependencies {
implementation project(':alerter')
implementation "com.google.android.material:material:1.0.0"
implementation 'com.google.android.material:material:1.2.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.tapadoo.alerter.demo

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.tapadoo.alerter.Alerter
import kotlinx.android.synthetic.main.layout_modal_bottom_sheet.btnAlertColoured
import kotlinx.android.synthetic.main.layout_modal_bottom_sheet.btnAlertCustomIcon
import kotlinx.android.synthetic.main.layout_modal_bottom_sheet.btnAlertDefault

class CustomBottomSheetDialogFragment : BottomSheetDialogFragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.layout_modal_bottom_sheet, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

btnAlertDefault.setOnClickListener {
//handle click event
showAlertDefault(dialog)
}
btnAlertColoured.setOnClickListener {
//handle click event
showAlertColoured(dialog)
}
btnAlertCustomIcon.setOnClickListener {
//handle click event
showAlertWithIcon(dialog)
}
}

private fun showAlertDefault(view: Dialog?) {
view?.let {
Alerter.create(dialog = it)
.setTitle(R.string.title_activity_example)
.setText("Alert text...")
.show()
}
}

private fun showAlertColoured(view: Dialog?) {
view?.let {
Alerter.create(dialog = it)
.setTitle("Alert Title")
.setText("Alert text...")
.setBackgroundColorRes(R.color.colorAccent)
.show()
}
}

private fun showAlertWithIcon(view: Dialog?) {
view?.let {
Alerter.create(dialog = it)
.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()
}
}

}
Loading

0 comments on commit 81a577c

Please sign in to comment.