diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml new file mode 100644 index 0000000..4bd008d --- /dev/null +++ b/.idea/deploymentTargetDropDown.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 0894859..8b4d3c8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,9 +37,10 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.1' + implementation project(path: ':toast') testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' - implementation 'com.github.emrekotun:CpmToast:1.0.1' + //implementation 'com.github.emrekotun:CpmToast:1.0.1' } \ 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 d3cf154..5674c2d 100644 --- a/app/src/main/java/com/emrekotun/cpmtoast/MainActivity.kt +++ b/app/src/main/java/com/emrekotun/cpmtoast/MainActivity.kt @@ -3,6 +3,7 @@ package com.emrekotun.cpmtoast import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import com.emrekotun.toast.CpmToast +import com.emrekotun.toast.CpmToast.Companion.toastSuccess class MainActivity : AppCompatActivity() { @@ -10,12 +11,7 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - CpmToast.createColorToast( - this, - "Success !", - CpmToast.TOAST_SUCCESS, - CpmToast.GRAVITY_TOP, - CpmToast.LONG_DURATION - ) + toastSuccess("Hello Word !",CpmToast.LONG_DURATION) + } } \ No newline at end of file diff --git a/toast/src/main/java/com/emrekotun/toast/CpmToast.kt b/toast/src/main/java/com/emrekotun/toast/CpmToast.kt index f811b0c..739c3e6 100644 --- a/toast/src/main/java/com/emrekotun/toast/CpmToast.kt +++ b/toast/src/main/java/com/emrekotun/toast/CpmToast.kt @@ -18,17 +18,15 @@ 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 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 + const val LONG_DURATION= 5000L + const val SHORT_DURATION= 2000L private lateinit var layoutInflater: LayoutInflater @@ -38,12 +36,6 @@ class CpmToast { 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 @@ -51,60 +43,13 @@ class CpmToast { 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( + private fun createColorToast( context: Activity, message: String, style: String, - position: Int, duration: Long ) { layoutInflater = LayoutInflater.from(context) @@ -125,10 +70,6 @@ class CpmToast { ContextCompat.getColor(context, successToastColor) ) - // Pulse Animation for Icon - /*val pulseAnimation = AnimationUtils.loadAnimation(context, R.anim.pulse) - ivToast.startAnimation(pulseAnimation)*/ - // round background color setBackgroundAndFilter( successToastColor, @@ -145,7 +86,7 @@ class CpmToast { startTimer(duration, toast) // Setting Toast Gravity - setGravity(position, toast) + setGravity(toast) // Setting layout to toast @Suppress("DEPRECATION") @@ -161,9 +102,6 @@ class CpmToast { 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( @@ -180,7 +118,7 @@ class CpmToast { startTimer(duration, toast) // Setting Toast Gravity - setGravity(position, toast) + setGravity(toast) // Setting layout to toast toast.view = layout @@ -195,9 +133,6 @@ class CpmToast { 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( @@ -215,7 +150,7 @@ class CpmToast { startTimer(duration, toast) // Setting Toast Gravity - setGravity(position, toast) + setGravity(toast) // Setting layout to toast toast.view = layout @@ -230,9 +165,6 @@ class CpmToast { 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( @@ -250,7 +182,7 @@ class CpmToast { startTimer(duration, toast) // Setting Toast Gravity - setGravity(position, toast) + setGravity(toast) // Setting layout to toast toast.view = layout @@ -259,6 +191,41 @@ class CpmToast { } } + fun Activity.toastSuccess(message: String,duration: Long= SHORT_DURATION) { + createColorToast( + this, + message, + TOAST_SUCCESS, + duration + ) + } + fun Activity.toastError(message: String,duration: Long= SHORT_DURATION) { + createColorToast( + this, + message, + TOAST_ERROR, + duration + ) + } + + fun Activity.toastWarning(message: String,duration: Long= SHORT_DURATION) { + createColorToast( + this, + message, + TOAST_WARNING, + duration + ) + } + + fun Activity.toastInfo(message: String,duration: Long= SHORT_DURATION) { + createColorToast( + this, + message, + TOAST_INFO, + duration + ) + } + private fun startTimer(duration: Long, toast: Toast) { val timer = object : CountDownTimer(duration, 1000) { override fun onTick(millisUntilFinished: Long) { @@ -280,8 +247,8 @@ class CpmToast { layout.text = message } - private fun setGravity(position: Int, toast: Toast) { - toast.setGravity(position, 100, 100) + private fun setGravity(myToast: Toast) { + myToast.setGravity(50, 100, 100) } private fun setBackgroundAndFilter(