From 3e2c53dbad6a739e2226d5c12bebf155d0656d69 Mon Sep 17 00:00:00 2001 From: Emre KOTUN Date: Thu, 28 Oct 2021 17:26:14 +0300 Subject: [PATCH] bugs fix --- .idea/misc.xml | 15 + app/build.gradle | 2 +- .../com/emrekotun/cpmtoast/MainActivity.kt | 10 +- settings.gradle | 1 + .../main/java/com/emrekotun/toast/CpmToast.kt | 297 +++++++++++++++++- .../main/res/drawable/ic_check_bg_filled.xml | 10 + .../main/res/drawable/ic_close_bg_filled.xml | 10 + .../res/drawable/ic_error_sign_filled.xml | 10 + toast/src/main/res/drawable/ic_info_sign.xml | 19 ++ .../main/res/drawable/ic_info_sign_filled.xml | 10 + .../drawable/toast_color_view_background.xml | 8 + .../res/drawable/toast_round_background.xml | 6 + toast/src/main/res/layout/toast_layout.xml | 49 +++ toast/src/main/res/values/colors.xml | 9 + 14 files changed, 452 insertions(+), 4 deletions(-) create mode 100644 toast/src/main/res/drawable/ic_check_bg_filled.xml create mode 100644 toast/src/main/res/drawable/ic_close_bg_filled.xml create mode 100644 toast/src/main/res/drawable/ic_error_sign_filled.xml create mode 100644 toast/src/main/res/drawable/ic_info_sign.xml create mode 100644 toast/src/main/res/drawable/ic_info_sign_filled.xml create mode 100644 toast/src/main/res/drawable/toast_color_view_background.xml create mode 100644 toast/src/main/res/drawable/toast_round_background.xml create mode 100644 toast/src/main/res/layout/toast_layout.xml create mode 100644 toast/src/main/res/values/colors.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 9422e84..cc5279e 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,20 @@ + + + diff --git a/app/build.gradle b/app/build.gradle index af1f006..0355c9a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,5 +41,5 @@ dependencies { androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' - implementation project(':toast') + implementation 'com.github.emrekotun:CpmToast:1.0.0' } \ No newline at end of file diff --git a/app/src/main/java/com/emrekotun/cpmtoast/MainActivity.kt b/app/src/main/java/com/emrekotun/cpmtoast/MainActivity.kt index c2a3244..46e537a 100644 --- a/app/src/main/java/com/emrekotun/cpmtoast/MainActivity.kt +++ b/app/src/main/java/com/emrekotun/cpmtoast/MainActivity.kt @@ -2,13 +2,19 @@ package com.emrekotun.cpmtoast import androidx.appcompat.app.AppCompatActivity import android.os.Bundle -import com.emrekotun.toast.CpmToast + class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - CpmToast.EXTRA + /* CpmToast.createColorToast( + this, + message, + CpmToast.TOAST_ERROR, + CpmToast.GRAVITY_TOP, + CpmToast.LONG_DURATION + )*/ } } \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 4ab05e0..0324030 100644 --- a/settings.gradle +++ b/settings.gradle @@ -3,6 +3,7 @@ dependencyResolutionManagement { repositories { google() mavenCentral() + maven { url 'https://jitpack.io' } jcenter() // Warning: this repository is going to shut down soon } } diff --git a/toast/src/main/java/com/emrekotun/toast/CpmToast.kt b/toast/src/main/java/com/emrekotun/toast/CpmToast.kt index 6e15845..f811b0c 100644 --- a/toast/src/main/java/com/emrekotun/toast/CpmToast.kt +++ b/toast/src/main/java/com/emrekotun/toast/CpmToast.kt @@ -1,9 +1,304 @@ package com.emrekotun.toast +import android.app.Activity +import android.content.Context +import android.graphics.Color +import android.graphics.PorterDuff +import android.graphics.PorterDuffColorFilter +import android.graphics.drawable.Drawable +import android.os.CountDownTimer +import android.view.LayoutInflater +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.appcompat.content.res.AppCompatResources +import androidx.core.content.ContextCompat +import androidx.core.graphics.drawable.DrawableCompat + // Created by Emre KOTUN on 28.10.2021. class CpmToast { companion object { - const val EXTRA:String="" + + const val LONG_DURATION = 5000L // 5 seconds + const val TOAST_SUCCESS = "SUCCESS" + const val TOAST_ERROR = "FAILED" + const val TOAST_WARNING = "WARNING" + const val TOAST_INFO = "INFO" + const val GRAVITY_TOP = 50 + + private lateinit var layoutInflater: LayoutInflater + + private var successToastColor: Int = R.color.lightSuccess + private var errorToastColor: Int = R.color.lightError + private var warningToastColor: Int = R.color.lightWarning + private var infoToastColor: Int = R.color.lightPrimary + private var deleteToastColor: Int = R.color.lightPrimary + + /*private var successBackgroundToastColor: Int = R.color.success_bg_color + private var errorBackgroundToastColor: Int = R.color.error_bg_color + private var warningBackgroundToastColor: Int = R.color.warning_bg_color + private var infoBackgroundToastColor: Int = R.color.info_bg_color + private var deleteBackgroundToastColor: Int = R.color.delete_bg_color*/ + + @Suppress("unused") + fun resetToastColors() { + successToastColor = R.color.lightSuccess + errorToastColor = R.color.lightError + warningToastColor = R.color.lightWarning + infoToastColor = R.color.lightPrimary + deleteToastColor = R.color.lightPrimary + + /* successBackgroundToastColor = R.color.success_bg_color + errorBackgroundToastColor = R.color.error_bg_color + warningBackgroundToastColor = R.color.warning_bg_color + infoBackgroundToastColor = R.color.info_bg_color + deleteBackgroundToastColor = R.color.delete_bg_color*/ + } + + fun setSuccessColor(color: Int) { + successToastColor = color + } + + /* fun setSuccessBackgroundColor(color: Int) { + successBackgroundToastColor = color + }*/ + + fun setErrorColor(color: Int) { + errorToastColor = color + } + + /* fun setErrorBackgroundColor(color: Int) { + errorBackgroundToastColor = color + }*/ + + fun setWarningColor(color: Int) { + warningToastColor = color + } + + /* fun setWarningBackgroundColor(color: Int) { + warningBackgroundToastColor = color + }*/ + + fun setInfoColor(color: Int) { + infoToastColor = color + } + + /* fun setInfoBackgroundColor(color: Int) { + infoBackgroundToastColor = color + }*/ + + fun setDeleteColor(color: Int) { + deleteToastColor = color + } + + /*fun setDeleteBackgroundColor(color: Int) { + deleteBackgroundToastColor = color + }*/ + + // all color toast CTA + fun createColorToast( + context: Activity, + message: String, + style: String, + position: Int, + duration: Long + ) { + layoutInflater = LayoutInflater.from(context) + val layout = layoutInflater.inflate( + R.layout.toast_layout, + (context).findViewById(R.id.color_toast_view) + ) + val ivToast: ImageView = layout.findViewById(R.id.color_toast_image) + val tvToastMsg: TextView = layout.findViewById(R.id.color_toast_description) + when (style) { + // Function for Toast Success + TOAST_SUCCESS -> { + ivToast.setImageDrawable( + context.drawable(R.drawable.ic_check_bg_filled) + ) + DrawableCompat.setTint( + DrawableCompat.wrap(ivToast.drawable), + ContextCompat.getColor(context, successToastColor) + ) + + // Pulse Animation for Icon + /*val pulseAnimation = AnimationUtils.loadAnimation(context, R.anim.pulse) + ivToast.startAnimation(pulseAnimation)*/ + + // round background color + setBackgroundAndFilter( + successToastColor, + layout, + context + ) + + // Setting up the color for title & Message text + + setDescriptionDetails(message, tvToastMsg) + + // init toast + val toast = Toast(context.applicationContext) + startTimer(duration, toast) + + // Setting Toast Gravity + setGravity(position, toast) + + // Setting layout to toast + @Suppress("DEPRECATION") + toast.view = layout + toast.show() + } + // CTA for Toast Error + TOAST_ERROR -> { + ivToast.setImageDrawable( + context.drawable(R.drawable.ic_close_bg_filled) + ) + DrawableCompat.setTint( + DrawableCompat.wrap(ivToast.drawable), + ContextCompat.getColor(context, errorToastColor) + ) + // Pulse Animation for Icon + /* val pulseAnimation = AnimationUtils.loadAnimation(context, R.anim.pulse) + ivToast.startAnimation(pulseAnimation)*/ + + // round background color + setBackgroundAndFilter( + errorToastColor, + layout, + context + ) + + // Setting up the color for title & Message text + setDescriptionDetails(message, tvToastMsg) + + // init toast + val toast = Toast(context.applicationContext) + startTimer(duration, toast) + + // Setting Toast Gravity + setGravity(position, toast) + + // Setting layout to toast + toast.view = layout + toast.show() + } + // CTA for Toast Warning + TOAST_WARNING -> { + ivToast.setImageDrawable( + context.drawable(R.drawable.ic_error_sign_filled) + ) + DrawableCompat.setTint( + DrawableCompat.wrap(ivToast.drawable), + ContextCompat.getColor(context, warningToastColor) + ) + // Pulse Animation for Icon + /*val pulseAnimation = AnimationUtils.loadAnimation(context, R.anim.pulse) + ivToast.startAnimation(pulseAnimation)*/ + + // round background color + setBackgroundAndFilter( + warningToastColor, + layout, + context + ) + + // Setting up the color for title & Message text + + setDescriptionDetails(message, tvToastMsg) + + // init toast + val toast = Toast(context.applicationContext) + startTimer(duration, toast) + + // Setting Toast Gravity + setGravity(position, toast) + + // Setting layout to toast + toast.view = layout + toast.show() + } + // CTA for Toast Info + TOAST_INFO -> { + ivToast.setImageDrawable( + context.drawable(R.drawable.ic_info_sign_filled) + ) + DrawableCompat.setTint( + DrawableCompat.wrap(ivToast.drawable), + ContextCompat.getColor(context, infoToastColor) + ) + // Pulse Animation for Icon + /* val pulseAnimation = AnimationUtils.loadAnimation(context, R.anim.pulse) + ivToast.startAnimation(pulseAnimation)*/ + + // round background color + setBackgroundAndFilter( + infoToastColor, + layout, + context + ) + + // Setting up the color for title & Message text + + setDescriptionDetails(message, tvToastMsg) + + // init toast + val toast = Toast(context.applicationContext) + startTimer(duration, toast) + + // Setting Toast Gravity + setGravity(position, toast) + + // Setting layout to toast + toast.view = layout + toast.show() + } + } + } + + private fun startTimer(duration: Long, toast: Toast) { + val timer = object : CountDownTimer(duration, 1000) { + override fun onTick(millisUntilFinished: Long) { + // do nothing + } + + override fun onFinish() { + toast.cancel() + } + } + timer.start() + } + + private fun setDescriptionDetails( + message: String, + layout: TextView + ) { + layout.setTextColor(Color.WHITE) + layout.text = message + } + + private fun setGravity(position: Int, toast: Toast) { + toast.setGravity(position, 100, 100) + } + + private fun setBackgroundAndFilter( + @ColorRes colorFilter: Int, + layout: View, + context: Context + ) { + val drawable = context.drawable(R.drawable.toast_round_background) + drawable?.colorFilter = PorterDuffColorFilter( + ContextCompat.getColor(context, colorFilter), + PorterDuff.Mode.MULTIPLY + ) + layout.background = drawable + } } +} + +fun Context.drawable(@DrawableRes drawableResId: Int): Drawable? { + return AppCompatResources.getDrawable(this, drawableResId) } \ No newline at end of file diff --git a/toast/src/main/res/drawable/ic_check_bg_filled.xml b/toast/src/main/res/drawable/ic_check_bg_filled.xml new file mode 100644 index 0000000..5848040 --- /dev/null +++ b/toast/src/main/res/drawable/ic_check_bg_filled.xml @@ -0,0 +1,10 @@ + + + diff --git a/toast/src/main/res/drawable/ic_close_bg_filled.xml b/toast/src/main/res/drawable/ic_close_bg_filled.xml new file mode 100644 index 0000000..fe885f4 --- /dev/null +++ b/toast/src/main/res/drawable/ic_close_bg_filled.xml @@ -0,0 +1,10 @@ + + + diff --git a/toast/src/main/res/drawable/ic_error_sign_filled.xml b/toast/src/main/res/drawable/ic_error_sign_filled.xml new file mode 100644 index 0000000..9486f5f --- /dev/null +++ b/toast/src/main/res/drawable/ic_error_sign_filled.xml @@ -0,0 +1,10 @@ + + + diff --git a/toast/src/main/res/drawable/ic_info_sign.xml b/toast/src/main/res/drawable/ic_info_sign.xml new file mode 100644 index 0000000..a216e7a --- /dev/null +++ b/toast/src/main/res/drawable/ic_info_sign.xml @@ -0,0 +1,19 @@ + + + + + + diff --git a/toast/src/main/res/drawable/ic_info_sign_filled.xml b/toast/src/main/res/drawable/ic_info_sign_filled.xml new file mode 100644 index 0000000..50367b0 --- /dev/null +++ b/toast/src/main/res/drawable/ic_info_sign_filled.xml @@ -0,0 +1,10 @@ + + + diff --git a/toast/src/main/res/drawable/toast_color_view_background.xml b/toast/src/main/res/drawable/toast_color_view_background.xml new file mode 100644 index 0000000..9b36843 --- /dev/null +++ b/toast/src/main/res/drawable/toast_color_view_background.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/toast/src/main/res/drawable/toast_round_background.xml b/toast/src/main/res/drawable/toast_round_background.xml new file mode 100644 index 0000000..02f2c1f --- /dev/null +++ b/toast/src/main/res/drawable/toast_round_background.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/toast/src/main/res/layout/toast_layout.xml b/toast/src/main/res/layout/toast_layout.xml new file mode 100644 index 0000000..9740180 --- /dev/null +++ b/toast/src/main/res/layout/toast_layout.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/toast/src/main/res/values/colors.xml b/toast/src/main/res/values/colors.xml new file mode 100644 index 0000000..ff791da --- /dev/null +++ b/toast/src/main/res/values/colors.xml @@ -0,0 +1,9 @@ + + + #406C80 + #20A1DA + #D99B21 + #D95757 + #17996E + #FFFFFF + \ No newline at end of file