Skip to content

Commit

Permalink
add ToastProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
yizems committed May 20, 2022
1 parent f209bae commit 5416533
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class DefaultCoroutineExceptionHandler(
) : AbstractCoroutineContextElement(CoroutineExceptionHandler), CoroutineExceptionHandler {

override fun handleException(context: CoroutineContext, exception: Throwable) {
// KLog.e(exception)
context.cancel()

val intercept = coroutineErrorCallback?.callback?.invoke(exception)
Expand Down Expand Up @@ -55,11 +54,10 @@ class DefaultCoroutineExceptionHandler(
if (!showHint) {
return
}
// TODO: 2021/10/26
if (exceptionMsg != null) {
// ToastCompat.makeText(ContextHolder.get(), exceptionMsg, ToastCompat.DURATION_LONG).show()
ToastProvider.showShort(exceptionMsg)
} else if (msgId != 0 && showHint) {
// ToastCompat.makeText(ContextHolder.get(), msgId, ToastCompat.DURATION_LONG).show()
ToastProvider.showShort(msgId)
}
}

Expand Down Expand Up @@ -99,4 +97,4 @@ class CoroutineErrorCallback(
val interceptToast: ((Throwable) -> Boolean?)? = null,
val interceptUpload: ((Throwable) -> Boolean?)? = null,
val callback: (Throwable) -> Any?,
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cn.yizems.util.ktx.android.coroutine

import androidx.annotation.StringRes

object ToastProvider : IToastProvider {

private var toastProvider: IToastProvider? = null

fun register(toastProvider: IToastProvider) {
ToastProvider.toastProvider = toastProvider
}

override fun showShort(text: String) {
toastProvider?.showShort(text)
}

override fun showShort(resId: Int) {
toastProvider?.showShort(resId)
}

override fun showLong(text: String) {
toastProvider?.showLong(text)
}

override fun showLong(resId: Int) {
toastProvider?.showLong(resId)
}
}

interface IToastProvider {
fun showShort(text: String)
fun showShort(@StringRes resId: Int)

fun showLong(text: String)
fun showLong(@StringRes resId: Int)
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rootProject.name = "UtilKtx"
rootProject.name = "KUtil"
include ':comm'
include ':okhttp'

Expand Down

0 comments on commit 5416533

Please sign in to comment.