From 01f751ab7048fcce92c76830b38fd2fac9886689 Mon Sep 17 00:00:00 2001 From: eldhopj Date: Mon, 25 Jul 2022 18:38:10 +0530 Subject: [PATCH 1/2] unified snackbar --- README.md | 42 +++--- android-extensions/build.gradle | 4 +- .../android_extensions/FragmentExtensions.kt | 4 +- .../android_extensions/ViewExtensions.kt | 137 ++++-------------- .../sampleandroidextensions/MainActivity.kt | 18 ++- app/src/main/res/values/strings.xml | 3 +- 6 files changed, 67 insertions(+), 141 deletions(-) diff --git a/README.md b/README.md index d1da876..ce6cafd 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ allprojects { dependency to your module `build.gradle` file ```gradle dependencies { - implementation 'com.github.Eldhopj:android-extensions:0.1' + implementation 'com.github.Eldhopj:android-extensions:0.2' } ``` @@ -125,37 +125,31 @@ Please go thorough the [TextView Extensions][7] code documentation for more info - .**setOnSafeClickListener** -> Restrict multiple consecutive click events for the view. - Parameters - 1.defaultInterval -> defaultInterval Interval to wait until click is enabled (not mandatory). Default interval is 800ms + Parameters + 1.defaultInterval -> defaultInterval Interval to wait until click is enabled (not mandatory). Default interval is 800ms - .**enable** -> Make View Enable - .**disable** -> Make view Disable, goes in a disabled color and clicks wont accept - .**gone** -> Make the view Gone - .**visible** -> Make the view Visible -- .**snackBar** -> Show a Snackbar with message +- .**snackbar** -> Shows Snackbar - Parameters - 1.message -> snackBar message, either in string or string res - 2.length -> snackBar duration (not mandatory). Default value is long -- .**snackBarAction** -> Show a Snackbar with message and the action execute immediately after the message shown + Parameters + 1.messageRes -> snackbar message + 2.length -> duration + 3.actionRes -> action button text (optinal). Needed only if there is any action for snackbar + 4.actionColor -> Color of action button (optinal) - Parameters - 1.message -> snackBar message, either in string or string res - 2.length -> snackBar duration (not mandatory). Default value is long -```kotlin -button.setOnSafeClickListener { - it?.snackBarAction("Snackbar text") { - // ... Snackbar action - } -} -``` -*For showing snackbar **with action button** and, execute the action on tapping on action button* +Sample Code: ```kotlin -button.setOnSafeClickListener { - it?.snackBarAction("Snackbar text") { - action("R.string.snackbar_done_button") { - // ... Snackbar action - } +button.setOnSafeClickListener { view -> + view?.snackbar( + R.string.app_name, + Snackbar.LENGTH_INDEFINITE, + R.string.retry, + R.color.black + ){ + toast("action clicked") } } ``` diff --git a/android-extensions/build.gradle b/android-extensions/build.gradle index f26ee1b..9267f91 100644 --- a/android-extensions/build.gradle +++ b/android-extensions/build.gradle @@ -24,8 +24,8 @@ android { } dependencies { - implementation 'androidx.core:core-ktx:1.7.0' - implementation 'com.google.android.material:material:1.5.0' + implementation 'androidx.core:core-ktx:1.8.0' + implementation 'com.google.android.material:material:1.6.1' } afterEvaluate { diff --git a/android-extensions/src/main/java/com/eldhopj/android_extensions/FragmentExtensions.kt b/android-extensions/src/main/java/com/eldhopj/android_extensions/FragmentExtensions.kt index 9e66a51..441d8df 100644 --- a/android-extensions/src/main/java/com/eldhopj/android_extensions/FragmentExtensions.kt +++ b/android-extensions/src/main/java/com/eldhopj/android_extensions/FragmentExtensions.kt @@ -12,7 +12,7 @@ import androidx.fragment.app.Fragment * @param duration Toast duration. Default is short */ fun Fragment.toast(message: String, duration: Int = Toast.LENGTH_SHORT) { - requireContext().toast(message, duration) + context.toast(message, duration) } /** @@ -22,5 +22,5 @@ fun Fragment.toast(message: String, duration: Int = Toast.LENGTH_SHORT) { * @param duration Toast duration. Default is short */ fun Fragment.toast(@StringRes stringRes: Int, duration: Int = Toast.LENGTH_SHORT) { - requireContext().toast(stringRes, duration) + context.toast(stringRes, duration) } diff --git a/android-extensions/src/main/java/com/eldhopj/android_extensions/ViewExtensions.kt b/android-extensions/src/main/java/com/eldhopj/android_extensions/ViewExtensions.kt index 028265e..6ade904 100644 --- a/android-extensions/src/main/java/com/eldhopj/android_extensions/ViewExtensions.kt +++ b/android-extensions/src/main/java/com/eldhopj/android_extensions/ViewExtensions.kt @@ -2,7 +2,6 @@ package com.eldhopj.android_extensions import android.view.View import androidx.annotation.ColorRes -import androidx.annotation.NonNull import androidx.annotation.StringRes import com.eldhopj.android_extensions.utils.SafeClickListener import com.google.android.material.snackbar.BaseTransientBottomBar @@ -67,113 +66,41 @@ fun View?.visible() { } /** - * Show a Snackbar with message + * Show a Snackbar * - * @param message Snackbar message - * @param length Snackbar duration. Default is Long - */ -fun View.snackBar( - @NonNull message: String, - @BaseTransientBottomBar.Duration length: Int = Snackbar.LENGTH_LONG -) = snackBarAction(message, length) {} - -/** - * Show a Snackbar with message res - * - * @param messageRes Snackbar message res - * @param length Snackbar duration. Default is Long - */ -fun View.snackBar( - @StringRes messageRes: Int, - @BaseTransientBottomBar.Duration length: Int = Snackbar.LENGTH_LONG -) = snackBarAction(messageRes, length) {} - - -/** - * Show a Snackbar with message and the action execute immediately after the message shown + * ``` + * button.setOnSafeClickListener { view -> + * view?.snackbar( + * R.string.app_name, + * Snackbar.LENGTH_INDEFINITE, + * R.string.retry, + * R.color.black + * ){ + * toast("action clicked") + * } + * } + * ``` * - * Sample code : -button.setOnSafeClickListener { -it?.snackBarAction("Snackbar text") { -// ... Snackbar action -} -} - * @param message Snackbar message - * @param length Snackbar duration. Default is Long - * @receiver - */ -inline fun View.snackBarAction( - @NonNull message: String, - @BaseTransientBottomBar.Duration length: Int = Snackbar.LENGTH_LONG, - action: Snackbar.() -> Unit -) { - val snack = Snackbar.make(this, message, length) - snack.action() - snack.show() -} - -/** - * Show a Snackbar with message and the action execute immediately after the message shown - * - * Sample code : -button.setOnSafeClickListener { -it?.snackBarAction("Snackbar text") { -// ... Snackbar action -} -} - * @param messageRes Snackbar message res - * @param length Snackbar duration. Default is Long - * @receiver + * @param messageRes snackbar message string resource + * @param length snackbar duration. + * @param actionRes Action button text res. optional needed only if we are setting an action + * @param actionColor Color resource for action text. optional + * @param action action happens when we click on snackbar action button. optional */ -inline fun View.snackBarAction( +fun View.snackbar( @StringRes messageRes: Int, - @BaseTransientBottomBar.Duration length: Int = Snackbar.LENGTH_LONG, - action: Snackbar.() -> Unit + @BaseTransientBottomBar.Duration length: Int, + @StringRes actionRes: Int?, + @ColorRes actionColor: Int? = null, + action: ((View) -> Unit)? = null ) { - val snack = Snackbar.make(this, messageRes, length) - snack.action() - snack.show() -} - -/** - * Show a Snackbar action with [actionRes] button, execute the action on tapping on action button - * - * Sample code : -button.setOnSafeClickListener { -it?.snackBarAction("Snackbar text") { -action(R.string.snackbar_done_button) { -// ... Snackbar action -} -} -} - * - * @param actionRes Action button text res - * @param color Color res of action text - * */ -fun Snackbar.action( - @StringRes actionRes: Int, - @ColorRes color: Int? = null, - listener: (View) -> Unit -) { - setAction(actionRes, listener) - color?.let { setActionTextColor(context.getColorCompat(color)) } -} - -/** - * Show a Snackbar action with [action] button, execute the action on tapping on action button - * - * Sample code : -button.setOnSafeClickListener { -it?.snackBarAction("Snackbar text") { -action("Done") { -// ... Snackbar action -} -} -} - * @param action Action button text - * @param color Color res of action text - * */ -fun Snackbar.action(action: String, @ColorRes color: Int? = null, listener: (View) -> Unit) { - setAction(action, listener) - color?.let { setActionTextColor(context.getColorCompat(color)) } + val snackbar = Snackbar.make(this, messageRes, length) + if (actionRes != null && action != null) { + actionColor?.let { snackbar.setActionTextColor(context.getColorCompat(it)) } + snackbar.setAction(actionRes) { + action(this) + }.show() + } else { + snackbar.show() + } } diff --git a/app/src/main/java/com/eldhopj/sampleandroidextensions/MainActivity.kt b/app/src/main/java/com/eldhopj/sampleandroidextensions/MainActivity.kt index e9a194a..0224ad4 100644 --- a/app/src/main/java/com/eldhopj/sampleandroidextensions/MainActivity.kt +++ b/app/src/main/java/com/eldhopj/sampleandroidextensions/MainActivity.kt @@ -4,10 +4,11 @@ import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity -import com.eldhopj.android_extensions.action import com.eldhopj.android_extensions.bold import com.eldhopj.android_extensions.setOnSafeClickListener -import com.eldhopj.android_extensions.snackBarAction +import com.eldhopj.android_extensions.snackbar +import com.eldhopj.android_extensions.toast +import com.google.android.material.snackbar.Snackbar class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -15,11 +16,14 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_main) val button = findViewById