refactor: 重构 ToastUtils
This commit is contained in:
@ -0,0 +1,8 @@
|
|||||||
|
package com.quyunshuo.androidbaseframemvvm.base.utils
|
||||||
|
|
||||||
|
import androidx.annotation.StringRes
|
||||||
|
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication.Companion.application as app
|
||||||
|
|
||||||
|
fun getString(@StringRes stringRes: Int): String {
|
||||||
|
return app.getString(stringRes)
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,46 @@
|
|||||||
|
package com.quyunshuo.androidbaseframemvvm.base.utils
|
||||||
|
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.annotation.StringRes
|
||||||
|
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
|
||||||
|
|
||||||
|
private val mToastHandler by lazy { Handler(Looper.getMainLooper()) }
|
||||||
|
|
||||||
|
private var mToast: Toast? = null
|
||||||
|
|
||||||
|
@JvmOverloads
|
||||||
|
fun toast(text: String, duration: Int = Toast.LENGTH_SHORT) {
|
||||||
|
postToast(text, duration)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmOverloads
|
||||||
|
fun toast(@StringRes id: Int, duration: Int = Toast.LENGTH_SHORT) {
|
||||||
|
postToast(getString(id), duration)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun postToast(text: String, duration: Int) {
|
||||||
|
mToastHandler.post {
|
||||||
|
setToast(text, duration)
|
||||||
|
mToast?.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setToast(text: String, duration: Int) {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
|
||||||
|
if (mToast == null) {
|
||||||
|
mToast = Toast.makeText(BaseApplication.context, text, duration)
|
||||||
|
} else {
|
||||||
|
mToast?.duration = duration
|
||||||
|
mToast?.setText(text)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (mToast != null) {
|
||||||
|
mToast?.cancel()
|
||||||
|
mToast = null
|
||||||
|
}
|
||||||
|
mToast = Toast.makeText(BaseApplication.context, text, duration)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,6 @@
|
|||||||
package com.quyunshuo.androidbaseframemvvm.base.utils
|
package com.quyunshuo.androidbaseframemvvm.base.utils
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.Toast
|
|
||||||
import com.alibaba.android.arouter.launcher.ARouter
|
import com.alibaba.android.arouter.launcher.ARouter
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
@ -41,23 +40,4 @@ fun sendEvent(event: Any) = EventBusUtils.postEvent(event)
|
|||||||
*/
|
*/
|
||||||
fun aRouterJump(routerUrl: String) {
|
fun aRouterJump(routerUrl: String) {
|
||||||
ARouter.getInstance().build(routerUrl).navigation()
|
ARouter.getInstance().build(routerUrl).navigation()
|
||||||
}
|
|
||||||
|
|
||||||
/**************************************************************************************************/
|
|
||||||
/**
|
|
||||||
* toast
|
|
||||||
* @param msg String 文案
|
|
||||||
* @param duration Int 时间
|
|
||||||
*/
|
|
||||||
fun toast(msg: String, duration: Int = Toast.LENGTH_SHORT) {
|
|
||||||
ToastUtils.showToast(msg, duration)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* toast
|
|
||||||
* @param msgId Int String资源ID
|
|
||||||
* @param duration Int 时间
|
|
||||||
*/
|
|
||||||
fun toast(msgId: Int, duration: Int = Toast.LENGTH_SHORT) {
|
|
||||||
ToastUtils.showToast(msgId, duration)
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user