Skip to content

Commit

Permalink
Merge pull request #3 from Eldhopj/dev/eldhopj/snackbar_unified
Browse files Browse the repository at this point in the history
unified snackbar
  • Loading branch information
Eldhopj authored Jul 25, 2022
2 parents f73910f + 8a2a245 commit 93357f8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 141 deletions.
42 changes: 18 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ allprojects {
dependency to your module `build.gradle` file
```gradle
dependencies {
implementation 'com.github.Eldhopj:android-extensions:1.0'
implementation 'com.github.Eldhopj:android-extensions:1.1'
}
```

Expand Down Expand Up @@ -126,37 +126,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")
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions android-extensions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ 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?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
val textview = findViewById<TextView>(R.id.textView)
button.setOnSafeClickListener {
it?.snackBarAction(R.string.app_name) {
action("Retry") {

}
button.setOnSafeClickListener { view ->
view?.snackbar(
R.string.app_name,
Snackbar.LENGTH_INDEFINITE,
R.string.retry,
R.color.black,
) {
toast("action clicked")
}
}
textview.bold()
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">Sample Android Extensions</string>
</resources>
<string name="retry">Retry</string>
</resources>

0 comments on commit 93357f8

Please sign in to comment.