Skip to content

Commit

Permalink
Merge pull request #6 from 5AbhishekSaxena/dev/toast-builder
Browse files Browse the repository at this point in the history
Dev/toast builder
  • Loading branch information
5AbhishekSaxena authored Aug 17, 2020
2 parents 865304b + 3d8a42b commit 41fd3a3
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 16 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions app/src/main/java/in/abhisheksaxena/toasterexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package `in`.abhisheksaxena.toasterexample
import `in`.abhisheksaxena.toaster.Toaster
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
Expand All @@ -12,12 +11,25 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)

val message = "Some message"
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
button.setOnClickListener {
Toaster.pop(
this,
message,
R.drawable.ic_baseline_all_inclusive_24
Toaster.LENGTH_SHORT
).show()
}

builder_button.setOnClickListener {
val toastBuilder = Toaster.Builder(this)
.setMessage("Some default message")
Toaster.pop(toastBuilder.make()).show()
}

error_button.setOnClickListener {
Toaster.popError(
this,
"This is an error message",
Toaster.LENGTH_SHORT
).show()
}
}
Expand Down
25 changes: 24 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,33 @@
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_default"
android:text="Show toast"
app:layout_constraintBottom_toTopOf="@id/builder_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />

<Button
android:id="@+id/builder_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_default"
android:text="Show toast using Builder"
app:layout_constraintBottom_toTopOf="@id/error_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/button" />

<Button
android:id="@+id/error_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Error toast"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@id/builder_button" />

</androidx.constraintlayout.widget.ConstraintLayout>
131 changes: 119 additions & 12 deletions toaster/src/main/java/in/abhisheksaxena/toaster/Toaster.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,152 @@ package `in`.abhisheksaxena.toaster

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat


/**
* @author Abhishek Saxena
* @since 17-08-2020 06:25
*/

object Toaster {
class Toaster private constructor(
private val context: Context,
private var message: CharSequence,
private var duration: Int
) {

private var rootView: View? = null
private var leftDrawableRes: Int? = null

companion object {
const val LENGTH_SHORT = Toast.LENGTH_SHORT
const val LENGTH_LONG = Toast.LENGTH_LONG

fun pop(
context: Context,
message: CharSequence,
drawableRes: Int? = null,
duration: Int = LENGTH_SHORT
duration: Int
): Toast {
return pop(prepare(context, message, duration))
}

fun pop(
context: Context,
message: CharSequence,
drawableRes: Int,
duration: Int
): Toast {
return pop(prepare(context, message, drawableRes, duration))
}

fun pop(toaster: Toaster): Toast {
val toast = Toast(toaster.context)
toast.duration = toaster.duration
toast.view = toaster.rootView
return toast
}

fun popError(
context: Context,
message: CharSequence,
duration: Int
): Toast {
return pop(prepareError(context, message, duration))
}

private fun prepare(context: Context, message: CharSequence, duration: Int): Toaster {
return Builder(context)
.setMessage(message)
.setDuration(duration)
.make()
}

private fun prepare(
context: Context,
message: CharSequence,
drawableRes: Int?,
duration: Int
): Toaster {
return Builder(context)
.setMessage(message)
.apply {
drawableRes?.let {
setLeftDrawable(it)
}
}
.setDuration(duration)
.make()
}

private fun prepareError(context: Context, message: CharSequence, duration: Int): Toaster {
return Builder(context)
.setMessage(message)
.setLeftDrawable(R.drawable.ic_baseline_error_24)
.setLeftDrawableTint(Colors.ERROR)
.setDuration(duration)
.make()
}
}

class Builder(private val context: Context) {

val toast = Toast(context)
private val rootView: View
private var messageTextView: TextView? = null
private var leftDrawable: ImageView? = null
private var leftDrawableRes: Int? = null

private var message: CharSequence = ""
private var duration: Int = LENGTH_SHORT

init {
rootView = initView(context)
}

private fun initView(context: Context): View {
val inflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val rootView = inflater.inflate(R.layout.layout_toast, ConstraintLayout(context), false)

val messageTextView = rootView.findViewById<TextView>(R.id.message_text_view)
messageTextView.text = message
messageTextView = rootView.findViewById(R.id.message_text_view)
leftDrawable = rootView.findViewById(R.id.left_drawable_image_view)

return rootView
}

fun setMessage(message: CharSequence) = apply {
this.message = message
messageTextView?.text = message
}

fun setLeftDrawable(leftDrawableRes: Int) = apply {
this.leftDrawableRes = leftDrawableRes
leftDrawable?.setImageResource(leftDrawableRes)
}

fun setDuration(duration: Int) = apply {
this.duration = duration
}

fun setLeftDrawableTint(colorRes: Int) = apply {
this.leftDrawable?.setColorFilter(ContextCompat.getColor(context, colorRes))
}

val leftDrawable = rootView.findViewById<ImageView>(R.id.left_drawable_image_view)
drawableRes?.let {
leftDrawable.setImageResource(it)
fun make(): Toaster {
return Toaster(context, message, duration).also {
it.rootView = rootView
it.leftDrawableRes = leftDrawableRes
}
}
}

toast.duration = duration
toast.view = rootView
return toast
private interface Colors{
companion object{
internal val ERROR = R.color.red
}
}
}
10 changes: 10 additions & 0 deletions toaster/src/main/res/drawable/ic_baseline_error_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-2h2v2zM13,13h-2L11,7h2v6z"/>
</vector>
4 changes: 4 additions & 0 deletions toaster/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#B00020</color>
</resources>

0 comments on commit 41fd3a3

Please sign in to comment.