diff --git a/README.md b/README.md index 1530e5f..1fdbfce 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ It has some predefined templates for common use-cases like warning, error and su ``` dependencies { - implementation 'tech.developingdeveloper.toaster-android:toaster:2.3.0' - implementation 'tech.developingdeveloper.toaster-android:toaster-ktx:2.3.0' //for ktx support + implementation 'com.github.5AbhishekSaxena.toaster-android:toaster:2.3.0' + implementation 'com.github.5AbhishekSaxena.toaster-android:toaster-ktx:2.3.0' //for ktx support } ``` *Please Note:* toaster-ktx includes toaster module so, if you are using toaster-ktx version then you don't have to add taoster diff --git a/app/build.gradle b/app/build.gradle index a3635f3..f79dee3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,15 +3,14 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 31 - buildToolsVersion "29.0.3" + compileSdkVersion 33 defaultConfig { applicationId "tech.developingdeveloper.toasterexample" minSdkVersion 19 targetSdkVersion 31 - versionCode 1 - versionName "1.2" + versionCode 3 + versionName "1.3" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -31,21 +30,22 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + namespace 'tech.developingdeveloper.toasterexample' } dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) - implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.30" - implementation 'androidx.core:core-ktx:1.6.0' - implementation 'androidx.appcompat:appcompat:1.3.1' - implementation 'androidx.constraintlayout:constraintlayout:2.1.0' + implementation "org.jetbrains.kotlin:kotlin-stdlib:1.7.20" + implementation 'androidx.core:core-ktx:1.9.0' + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation project(path: ':toaster') implementation project(path: ':toaster-ktx') testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test.ext:junit:1.1.3' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.5' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' - implementation 'com.google.android.material:material:1.4.0' + implementation 'com.google.android.material:material:1.7.0' } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 946a08a..e296fc8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,5 @@ - + - + diff --git a/app/src/main/java/tech/developingdeveloper/toasterexample/KtxExampleActivity.kt b/app/src/main/java/tech/developingdeveloper/toasterexample/KtxExampleActivity.kt index c7af57b..5997b11 100644 --- a/app/src/main/java/tech/developingdeveloper/toasterexample/KtxExampleActivity.kt +++ b/app/src/main/java/tech/developingdeveloper/toasterexample/KtxExampleActivity.kt @@ -4,7 +4,8 @@ import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import tech.developingdeveloper.toaster.Toaster import tech.developingdeveloper.toasterexample.databinding.ActivityMainBinding -import tech.developingdeveloper.toasterktx.ToasterBuilderKtx +import tech.developingdeveloper.toasterktx.prepareToast +import tech.developingdeveloper.toasterktx.prepareToaster /** @@ -22,7 +23,7 @@ class KtxExampleActivity : AppCompatActivity() { setContentView(binding.root) binding.builderButton.setOnClickListener { - val toaster = ToasterBuilderKtx.prepareToaster(this) { + val toaster = prepareToaster(this) { message = "File uploaded successfully" leftDrawableRes = R.drawable.ic_baseline_cloud_done_24 leftDrawableTint = R.color.blue @@ -34,7 +35,7 @@ class KtxExampleActivity : AppCompatActivity() { // or - ToasterBuilderKtx.prepareToast(this) { + prepareToast(this) { message = "File uploaded successfully" leftDrawableRes = R.drawable.ic_baseline_cloud_done_24 leftDrawableTint = R.color.blue diff --git a/app/src/main/java/tech/developingdeveloper/toasterexample/MainActivity.kt b/app/src/main/java/tech/developingdeveloper/toasterexample/MainActivity.kt index 5c4e395..4a42337 100644 --- a/app/src/main/java/tech/developingdeveloper/toasterexample/MainActivity.kt +++ b/app/src/main/java/tech/developingdeveloper/toasterexample/MainActivity.kt @@ -2,6 +2,7 @@ package tech.developingdeveloper.toasterexample import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import tech.developingdeveloper.toaster.DefaultToasterType import tech.developingdeveloper.toaster.Toaster import tech.developingdeveloper.toasterexample.databinding.ActivityMainBinding @@ -30,36 +31,41 @@ class MainActivity : AppCompatActivity() { } binding.builderButton.setOnClickListener { - val toastBuilder = Toaster.Builder(this) - .setMessage("File uploaded successfully") - .setLeftDrawable(R.drawable.ic_baseline_cloud_done_24) - .setLeftDrawableTint(R.color.blue) - .setStripTint(R.color.blue) - .setDuration(Toaster.LENGTH_SHORT) - Toaster.pop(toastBuilder.make()).show() + val toaster = Toaster.Config( + message = "File uploaded successfully", + leftDrawableRes = R.drawable.ic_baseline_cloud_done_24, + leftDrawableTint = R.color.blue, + stripTint = R.color.blue, + duration = Toaster.LENGTH_SHORT, + ).make(this) + + Toaster.pop(toaster).show() } binding.errorButton.setOnClickListener { - Toaster.popError( - this, - "This is an error message", - Toaster.LENGTH_SHORT + Toaster.pop( + context = this, + message = "This is an error message", + duration = Toaster.LENGTH_SHORT, + toasterType = DefaultToasterType.ERROR, ).show() } binding.warningButton.setOnClickListener { - Toaster.popWarning( - this, - "This is a warning message", - Toaster.LENGTH_SHORT + Toaster.pop( + context = this, + message = "This is a warning message", + duration = Toaster.LENGTH_SHORT, + toasterType = DefaultToasterType.WARNING, ).show() } binding.successButton.setOnClickListener { - Toaster.popSuccess( - this, - "This is a success message", - Toaster.LENGTH_SHORT + Toaster.pop( + context = this, + message = "This is a success message", + duration = Toaster.LENGTH_SHORT, + toasterType = DefaultToasterType.SUCCESS, ).show() } } diff --git a/build.gradle b/build.gradle index 878f55a..bbec396 100644 --- a/build.gradle +++ b/build.gradle @@ -5,8 +5,8 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.3' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30" + classpath 'com.android.tools.build:gradle:7.3.1' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index bd8b72e..d7638f1 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/toaster-ktx/build.gradle b/toaster-ktx/build.gradle index f7174e7..43c9c9c 100644 --- a/toaster-ktx/build.gradle +++ b/toaster-ktx/build.gradle @@ -27,15 +27,16 @@ android { kotlinOptions { jvmTarget = '1.8' } + namespace 'tech.developingdeveloper.toasterktx' } dependencies { - implementation 'androidx.core:core-ktx:1.6.0' - implementation 'androidx.appcompat:appcompat:1.3.1' - implementation 'com.google.android.material:material:1.4.0' + implementation 'androidx.core:core-ktx:1.9.0' + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'com.google.android.material:material:1.7.0' api project(path: ':toaster') testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test.ext:junit:1.1.3' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.5' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' } \ No newline at end of file diff --git a/toaster-ktx/src/main/AndroidManifest.xml b/toaster-ktx/src/main/AndroidManifest.xml index cdbf8a3..44008a4 100644 --- a/toaster-ktx/src/main/AndroidManifest.xml +++ b/toaster-ktx/src/main/AndroidManifest.xml @@ -1,5 +1,4 @@ - + \ No newline at end of file diff --git a/toaster-ktx/src/main/java/tech/developingdeveloper/toasterktx/ToasterBuilderKtx.kt b/toaster-ktx/src/main/java/tech/developingdeveloper/toasterktx/ToasterBuilderKtx.kt index db4918d..8b734f3 100644 --- a/toaster-ktx/src/main/java/tech/developingdeveloper/toasterktx/ToasterBuilderKtx.kt +++ b/toaster-ktx/src/main/java/tech/developingdeveloper/toasterktx/ToasterBuilderKtx.kt @@ -1,8 +1,6 @@ package tech.developingdeveloper.toasterktx import android.content.Context -import android.widget.Toast -import androidx.annotation.ColorInt import androidx.annotation.ColorRes import androidx.annotation.DrawableRes import tech.developingdeveloper.toaster.Toaster @@ -13,7 +11,7 @@ import tech.developingdeveloper.toaster.Toaster * @since 07-09-2021 12:01 */ -class ToasterBuilderKtx private constructor(private val context: Context) { +class ToasterBuilderKtx internal constructor(private val context: Context) { @DrawableRes var leftDrawableRes: Int = DEFAULT_VALUE @@ -27,40 +25,17 @@ class ToasterBuilderKtx private constructor(private val context: Context) { var message: CharSequence = "" var duration: Int = Toaster.LENGTH_SHORT - private fun prepare(): Toaster { - val toasterBuilder = Toaster.Builder(context) - .setMessage(message) - .setDuration(duration) - - if (leftDrawableRes != DEFAULT_VALUE) - toasterBuilder.setLeftDrawable(leftDrawableRes) - - if (leftDrawableTint != DEFAULT_VALUE) - toasterBuilder.setLeftDrawableTint(leftDrawableTint) - - if (stripTint != DEFAULT_VALUE) - toasterBuilder.setStripTint(stripTint) - - return toasterBuilder.make() + internal fun prepare(): Toaster { + return Toaster.Config( + message = message, + duration = duration, + leftDrawableRes = leftDrawableRes.takeIf { it != DEFAULT_VALUE }, + leftDrawableTint = leftDrawableTint.takeIf { it != DEFAULT_VALUE }, + stripTint = stripTint.takeIf { it != DEFAULT_VALUE }, + ).make(context) } companion object { private const val DEFAULT_VALUE = -1 - - fun prepareToast( - context: Context, - block: ToasterBuilderKtx.() -> Unit - ): Toast { - val toaster = prepareToaster(context, block) - return Toaster.pop(toaster) - } - - fun prepareToaster( - context: Context, - block: ToasterBuilderKtx.() -> Unit - ): Toaster { - val toastBuilder = ToasterBuilderKtx(context).apply(block) - return toastBuilder.prepare() - } } } diff --git a/toaster-ktx/src/main/java/tech/developingdeveloper/toasterktx/ToasterKtx.kt b/toaster-ktx/src/main/java/tech/developingdeveloper/toasterktx/ToasterKtx.kt new file mode 100644 index 0000000..538275e --- /dev/null +++ b/toaster-ktx/src/main/java/tech/developingdeveloper/toasterktx/ToasterKtx.kt @@ -0,0 +1,21 @@ +package tech.developingdeveloper.toasterktx + +import android.content.Context +import android.widget.Toast +import tech.developingdeveloper.toaster.Toaster + +fun prepareToast( + context: Context, + block: ToasterBuilderKtx.() -> Unit +): Toast { + val toaster = prepareToaster(context, block) + return Toaster.pop(toaster) +} + +fun prepareToaster( + context: Context, + block: ToasterBuilderKtx.() -> Unit +): Toaster { + val toastBuilder = ToasterBuilderKtx(context).apply(block) + return toastBuilder.prepare() +} \ No newline at end of file diff --git a/toaster/build.gradle b/toaster/build.gradle index 340e73b..ced1190 100644 --- a/toaster/build.gradle +++ b/toaster/build.gradle @@ -3,14 +3,13 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 31 - buildToolsVersion "29.0.3" + compileSdkVersion 33 defaultConfig { minSdkVersion 19 targetSdkVersion 31 - versionCode 2 - versionName "2.3.0" + versionCode 3 + versionName "2.3.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" @@ -22,17 +21,18 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } + namespace 'tech.developingdeveloper.toaster' } dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) - implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.30" - implementation 'androidx.core:core-ktx:1.6.0' - implementation 'androidx.appcompat:appcompat:1.3.1' - implementation 'androidx.constraintlayout:constraintlayout:2.1.0' + implementation "org.jetbrains.kotlin:kotlin-stdlib:1.7.20" + implementation 'androidx.core:core-ktx:1.9.0' + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test.ext:junit:1.1.3' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.5' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' // CardView implementation "androidx.cardview:cardview:1.0.0" diff --git a/toaster/src/main/AndroidManifest.xml b/toaster/src/main/AndroidManifest.xml index 67966db..25df386 100644 --- a/toaster/src/main/AndroidManifest.xml +++ b/toaster/src/main/AndroidManifest.xml @@ -1,5 +1 @@ - - - / - \ No newline at end of file + \ No newline at end of file diff --git a/toaster/src/main/java/tech/developingdeveloper/toaster/DefaultToasterType.kt b/toaster/src/main/java/tech/developingdeveloper/toaster/DefaultToasterType.kt new file mode 100644 index 0000000..365c74a --- /dev/null +++ b/toaster/src/main/java/tech/developingdeveloper/toaster/DefaultToasterType.kt @@ -0,0 +1,11 @@ +package tech.developingdeveloper.toaster + +/** + * An enumeration of all possible default [Toaster]s that + * the user can create. + */ +enum class DefaultToasterType { + SUCCESS, + WARNING, + ERROR, +} diff --git a/toaster/src/main/java/tech/developingdeveloper/toaster/Toaster.kt b/toaster/src/main/java/tech/developingdeveloper/toaster/Toaster.kt index cc6998e..d298991 100644 --- a/toaster/src/main/java/tech/developingdeveloper/toaster/Toaster.kt +++ b/toaster/src/main/java/tech/developingdeveloper/toaster/Toaster.kt @@ -7,9 +7,12 @@ import android.view.View import android.widget.ImageView import android.widget.TextView import android.widget.Toast +import androidx.annotation.ColorRes +import androidx.annotation.DrawableRes import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat -import tech.developingdeveloper.toaster.utils.Colors +import tech.developingdeveloper.toaster.defaults.DefaultToasterFactory +import tech.developingdeveloper.toaster.utils.visibleIf /** @@ -33,18 +36,26 @@ class Toaster private constructor( fun pop( context: Context, message: CharSequence, - duration: Int + duration: Int, + drawableRes: Int? = null, ): Toast { - return pop(prepare(context, message, duration)) + val toaster = prepare(context, message, duration, drawableRes) + return pop(toaster) } + /** + * Used to create a [Toast] that has some default values based on the supplied [toasterType]. + */ fun pop( context: Context, message: CharSequence, - drawableRes: Int, - duration: Int + duration: Int, + toasterType: DefaultToasterType, ): Toast { - return pop(prepare(context, message, drawableRes, duration)) + val defaultToaster = + DefaultToasterFactory.create(context, message, duration, toasterType) + + return pop(defaultToaster) } fun pop(toaster: Toaster): Toast { @@ -58,156 +69,74 @@ class Toaster private constructor( } } - fun popSuccess( - context: Context, - message: CharSequence, - duration: Int - ): Toast { - return pop(prepareSuccess(context, message, duration)) - } - - fun popWarning( - context: Context, - message: CharSequence, - duration: Int - ): Toast { - return pop(prepareWarning(context, message, duration)) - } - - 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 prepare(context, message, null, duration) - } - private fun prepare( context: Context, message: CharSequence, - drawableRes: Int?, - duration: Int + duration: Int, + drawableRes: Int? = null, ): Toaster { - return Builder(context) - .setMessage(message) - .apply { - drawableRes?.let { - setLeftDrawable(it) - } - } - .setDuration(duration) - .make() - } - - private fun prepareSuccess( - context: Context, - message: CharSequence, - duration: Int - ): Toaster { - return Builder(context) - .setMessage(message) - .setLeftDrawable(R.drawable.ic_baseline_check_circle_24) - .setLeftDrawableTint(Colors.SUCCESS) - .setStripTint(Colors.SUCCESS) - .setDuration(duration) - .make() - } - - private fun prepareWarning( - context: Context, - message: CharSequence, - duration: Int - ): Toaster { - return Builder(context) - .setMessage(message) - .setLeftDrawable(R.drawable.ic_baseline_warning_24) - .setLeftDrawableTint(Colors.WARNING) - .setStripTint(Colors.WARNING) - .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) - .setStripTint(Colors.ERROR) - .setDuration(duration) - .make() + return Config( + message = message, + leftDrawableRes = drawableRes, + duration = duration, + ).make(context) } } - class Builder(private val context: Context) { - - private val rootView: View - private var messageTextView: TextView? = null - private var leftDrawableImageView: ImageView? = null - private var leftDrawableRes: Int? = null - private var colorStripView: View? = null - - private var message: CharSequence = "" - private var duration: Int = LENGTH_SHORT - - init { - rootView = initView(context) + /** + * In Kotlin, since we can have default parameters, we have less of a need for a builder pattern. + * + * We can create a single data class that has a property for every customizable field, and then + * in [make] we can read our properties and configure our view based on that. + */ + data class Config( + private var message: CharSequence = "", + private var duration: Int = LENGTH_SHORT, + @DrawableRes + private var leftDrawableRes: Int? = null, + @ColorRes + private var leftDrawableTint: Int? = null, + @ColorRes + private var stripTint: Int? = null, + ) { + fun make(context: Context): Toaster { + return Toaster(context, message, duration).also { + it.rootView = buildView(context) + it.leftDrawableRes = leftDrawableRes + } } - private fun initView(context: Context): View { + /** + * Creates a [View] for a [Toaster] that matches this [Config]. + */ + private fun buildView(context: Context): View { val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val rootView = inflater.inflate(R.layout.layout_toast, ConstraintLayout(context), false) - messageTextView = rootView.findViewById(R.id.message_text_view) - leftDrawableImageView = rootView.findViewById(R.id.left_drawable_image_view) - colorStripView = rootView.findViewById(R.id.color_strip_view) - - setInitViewProperties() - - return rootView - } - - fun setMessage(message: CharSequence) = apply { - this.message = message - messageTextView?.text = message - messageTextView?.visibility = View.VISIBLE - } + val messageTextView: TextView = rootView.findViewById(R.id.message_text_view) + val leftDrawableImageView: ImageView = + rootView.findViewById(R.id.left_drawable_image_view) + val colorStripView: View = rootView.findViewById(R.id.color_strip_view) - fun setLeftDrawable(leftDrawableRes: Int) = apply { - this.leftDrawableRes = leftDrawableRes - leftDrawableImageView?.setImageResource(leftDrawableRes) - leftDrawableImageView?.visibility = View.VISIBLE - } + messageTextView.text = this.message + messageTextView.visibleIf(this.message.isNotEmpty()) - fun setDuration(duration: Int) = apply { - this.duration = duration - } - - fun setLeftDrawableTint(colorRes: Int) = apply { - leftDrawableImageView?.setColorFilter(ContextCompat.getColor(context, colorRes)) - leftDrawableImageView?.visibility = View.VISIBLE - } + leftDrawableRes?.let(leftDrawableImageView::setImageResource) + leftDrawableImageView.visibleIf(leftDrawableRes != null) - fun setStripTint(colorRes: Int) = apply { - colorStripView?.setBackgroundColor(ContextCompat.getColor(this.context, colorRes)) - colorStripView?.visibility = View.VISIBLE - } + val tintColor = this.leftDrawableTint?.let { tintRes -> + ContextCompat.getColor(context, tintRes) + } + tintColor?.let(leftDrawableImageView::setColorFilter) - fun make(): Toaster { - return Toaster(context, message, duration).also { - it.rootView = rootView - it.leftDrawableRes = leftDrawableRes + val stripColor = this.stripTint?.let { tintRes -> + ContextCompat.getColor(context, tintRes) } - } + stripColor?.let(colorStripView::setBackgroundColor) + colorStripView.visibleIf(stripColor != null) - private fun setInitViewProperties() { - colorStripView?.visibility = View.GONE - leftDrawableImageView?.visibility = View.GONE - messageTextView?.visibility = View.GONE + return rootView } } } \ No newline at end of file diff --git a/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/DefaultToaster.kt b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/DefaultToaster.kt new file mode 100644 index 0000000..2d9431e --- /dev/null +++ b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/DefaultToaster.kt @@ -0,0 +1,16 @@ +package tech.developingdeveloper.toaster.defaults + +import android.content.Context +import tech.developingdeveloper.toaster.Toaster + +/** + * This interface provides a [create] method that will be used to generate a + * default [Toaster] for different scenarios. + */ +internal interface DefaultToaster { + fun create( + context: Context, + message: CharSequence, + duration: Int, + ): Toaster +} diff --git a/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/DefaultToasterFactory.kt b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/DefaultToasterFactory.kt new file mode 100644 index 0000000..18ce82b --- /dev/null +++ b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/DefaultToasterFactory.kt @@ -0,0 +1,26 @@ +package tech.developingdeveloper.toaster.defaults + +import android.content.Context +import tech.developingdeveloper.toaster.DefaultToasterType +import tech.developingdeveloper.toaster.Toaster + +/** + * A simple factory to return [DefaultToaster] instance from [DefaultToasterType]. It uses + * [java.util.EnumMap] to map the [DefaultToasterType] with the [DefaultToaster]. The map is provided + * using [ToasterMapProvider] which handles the mapping of the enum to the implementation. + */ +internal object DefaultToasterFactory { + + private val toasterMap = ToasterMapProvider.provide() + + fun create( + context: Context, + message: CharSequence, + duration: Int, + toasterType: DefaultToasterType, + ): Toaster { + val defaultToaster = toasterMap[toasterType] + ?: throw IllegalArgumentException("Unknown toaster type.") + return defaultToaster.create(context, message, duration) + } +} \ No newline at end of file diff --git a/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/ErrorToaster.kt b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/ErrorToaster.kt new file mode 100644 index 0000000..f1d16ff --- /dev/null +++ b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/ErrorToaster.kt @@ -0,0 +1,18 @@ +package tech.developingdeveloper.toaster.defaults + +import android.content.Context +import tech.developingdeveloper.toaster.R +import tech.developingdeveloper.toaster.Toaster +import tech.developingdeveloper.toaster.utils.Colors + +internal object ErrorToaster : DefaultToaster { + override fun create(context: Context, message: CharSequence, duration: Int): Toaster { + return Toaster.Config( + message = message, + leftDrawableRes = R.drawable.ic_baseline_warning_24, + leftDrawableTint = Colors.ERROR, + stripTint = Colors.ERROR, + duration = duration, + ).make(context) + } +} \ No newline at end of file diff --git a/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/SuccessToaster.kt b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/SuccessToaster.kt new file mode 100644 index 0000000..1731669 --- /dev/null +++ b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/SuccessToaster.kt @@ -0,0 +1,22 @@ +package tech.developingdeveloper.toaster.defaults + +import android.content.Context +import tech.developingdeveloper.toaster.R +import tech.developingdeveloper.toaster.Toaster +import tech.developingdeveloper.toaster.utils.Colors + +internal object SuccessToaster : DefaultToaster { + override fun create( + context: Context, + message: CharSequence, + duration: Int, + ): Toaster { + return Toaster.Config( + message = message, + leftDrawableRes = R.drawable.ic_baseline_check_circle_24, + leftDrawableTint = Colors.SUCCESS, + stripTint = Colors.SUCCESS, + duration = duration, + ).make(context) + } +} diff --git a/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/ToasterMapProvider.kt b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/ToasterMapProvider.kt new file mode 100644 index 0000000..1c52c65 --- /dev/null +++ b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/ToasterMapProvider.kt @@ -0,0 +1,17 @@ +package tech.developingdeveloper.toaster.defaults + +import tech.developingdeveloper.toaster.DefaultToasterType +import java.util.EnumMap + +/** + * It is responsible for mapping [DefaultToasterType] to [DefaultToaster]. + */ +internal object ToasterMapProvider { + fun provide(): EnumMap = EnumMap( + mapOf( + DefaultToasterType.SUCCESS to SuccessToaster, + DefaultToasterType.WARNING to WarningToaster, + DefaultToasterType.ERROR to ErrorToaster, + ) + ) +} \ No newline at end of file diff --git a/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/WarningToaster.kt b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/WarningToaster.kt new file mode 100644 index 0000000..0cf48fa --- /dev/null +++ b/toaster/src/main/java/tech/developingdeveloper/toaster/defaults/WarningToaster.kt @@ -0,0 +1,18 @@ +package tech.developingdeveloper.toaster.defaults + +import android.content.Context +import tech.developingdeveloper.toaster.R +import tech.developingdeveloper.toaster.Toaster +import tech.developingdeveloper.toaster.utils.Colors + +internal object WarningToaster : DefaultToaster { + override fun create(context: Context, message: CharSequence, duration: Int): Toaster { + return Toaster.Config( + message = message, + leftDrawableRes = R.drawable.ic_baseline_warning_24, + leftDrawableTint = Colors.WARNING, + stripTint = Colors.WARNING, + duration = duration, + ).make(context) + } +} diff --git a/toaster/src/main/java/tech/developingdeveloper/toaster/utils/ViewUtils.kt b/toaster/src/main/java/tech/developingdeveloper/toaster/utils/ViewUtils.kt new file mode 100644 index 0000000..2410dc5 --- /dev/null +++ b/toaster/src/main/java/tech/developingdeveloper/toaster/utils/ViewUtils.kt @@ -0,0 +1,7 @@ +package tech.developingdeveloper.toaster.utils + +import android.view.View + +internal fun View.visibleIf(condition: Boolean?) { + this.visibility = if (condition == true) View.VISIBLE else View.GONE +} \ No newline at end of file