Skip to content

Commit

Permalink
bugs fix and added extension functions
Browse files Browse the repository at this point in the history
  • Loading branch information
emrekotun34 committed Nov 1, 2021
1 parent c2dc706 commit b3cf754
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 85 deletions.
17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

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

3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
10 changes: 3 additions & 7 deletions app/src/main/java/com/emrekotun/cpmtoast/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ 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() {
override fun onCreate(savedInstanceState: Bundle?) {
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)

}
}
121 changes: 44 additions & 77 deletions toast/src/main/java/com/emrekotun/toast/CpmToast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -38,73 +36,20 @@ 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
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(
private fun createColorToast(
context: Activity,
message: String,
style: String,
position: Int,
duration: Long
) {
layoutInflater = LayoutInflater.from(context)
Expand All @@ -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,
Expand All @@ -145,7 +86,7 @@ class CpmToast {
startTimer(duration, toast)

// Setting Toast Gravity
setGravity(position, toast)
setGravity(toast)

// Setting layout to toast
@Suppress("DEPRECATION")
Expand All @@ -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(
Expand All @@ -180,7 +118,7 @@ class CpmToast {
startTimer(duration, toast)

// Setting Toast Gravity
setGravity(position, toast)
setGravity(toast)

// Setting layout to toast
toast.view = layout
Expand All @@ -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(
Expand All @@ -215,7 +150,7 @@ class CpmToast {
startTimer(duration, toast)

// Setting Toast Gravity
setGravity(position, toast)
setGravity(toast)

// Setting layout to toast
toast.view = layout
Expand All @@ -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(
Expand All @@ -250,7 +182,7 @@ class CpmToast {
startTimer(duration, toast)

// Setting Toast Gravity
setGravity(position, toast)
setGravity(toast)

// Setting layout to toast
toast.view = layout
Expand All @@ -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) {
Expand All @@ -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(
Expand Down

0 comments on commit b3cf754

Please sign in to comment.