Skip to content

Commit

Permalink
* fix ToastyUtils
Browse files Browse the repository at this point in the history
* add support Fragment
  • Loading branch information
rahbadev committed Sep 27, 2023
1 parent b3dc97d commit 784ccb3
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions BtechUtils/src/main/java/com/rhdev/btechutils/ToastyUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@ package com.rhdev.btechutils

import android.content.Context
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import com.rhdev.btechutils.ToastyUtils.show
import es.dmoral.toasty.Toasty


fun show(
context: Context,
toastyType: ToastyType,
@StringRes resId: Int,
duration: Int = Toasty.LENGTH_LONG
) {
show(context, toastyType, context.getString(resId), duration)
}
object ToastyUtils {
fun show(
context: Context,
toastyType: ToastyType,
@StringRes resId: Int,
duration: Int = Toasty.LENGTH_LONG
) {
show(context, toastyType, context.getString(resId), duration)
}

fun show(
context: Context,
toastyType: ToastyType,
text: String,
duration: Int = Toasty.LENGTH_LONG
) {
when (toastyType) {
ToastyType.ERROR -> Toasty.error(context, text, duration, true).show()
ToastyType.INFO -> Toasty.info(context, text, duration, true).show()
ToastyType.SUCCESS -> Toasty.success(context, text, duration, true).show()
ToastyType.WARNING -> Toasty.warning(context, text, duration, true).show()
ToastyType.NORMAL -> Toasty.normal(context, text, duration).show()
fun show(
context: Context,
toastyType: ToastyType,
text: String,
duration: Int = Toasty.LENGTH_LONG
) {
when (toastyType) {
ToastyType.ERROR -> Toasty.error(context, text, duration, true).show()
ToastyType.INFO -> Toasty.info(context, text, duration, true).show()
ToastyType.SUCCESS -> Toasty.success(context, text, duration, true).show()
ToastyType.WARNING -> Toasty.warning(context, text, duration, true).show()
ToastyType.NORMAL -> Toasty.normal(context, text, duration).show()
}
}
}

Expand All @@ -42,6 +46,19 @@ fun Context.toasty(
duration: Int = Toasty.LENGTH_LONG
) = show(this, type, text, duration)

fun Fragment.toasty(
@StringRes resId: Int,
type: ToastyType = ToastyType.NORMAL,
duration: Int = Toasty.LENGTH_LONG
) = show(requireContext(), type, resId, duration)


fun Fragment.toasty(
text: String,
type: ToastyType = ToastyType.NORMAL,
duration: Int = Toasty.LENGTH_LONG
) = show(requireContext(), type, text, duration)

enum class ToastyType {
ERROR, INFO, SUCCESS, WARNING, NORMAL
}

0 comments on commit 784ccb3

Please sign in to comment.