feat: 替换新的兼容性更高的Toast工具类

This commit is contained in:
Quyunshuo
2021-03-23 20:27:04 +08:00
parent f4ce4742e6
commit c0169bd3f0
3 changed files with 1043 additions and 104 deletions

View File

@@ -1,82 +0,0 @@
package com.quyunshuo.base.ktx
import android.content.Context
import android.view.Gravity
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
/**
* @Author: QuYunShuo
* @Time: 2020/9/17
* @Class: ToastKtx
* @Remark: Toast相关的扩展方法
*/
/**
* Toast
* @param text CharSequence 类型文本
*/
fun Context.toast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, text, duration).show()
}
/**
* Toast
* @param resId String 类型资源id
*/
fun Context.toast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, resId, duration).show()
}
/**
* 居中Toast
* @param text CharSequence 类型文本
*/
fun Context.centerToast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
val toast = Toast.makeText(this, text, duration)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
}
/**
* 居中Toast
* @param resId String 类型资源id
*/
fun Context.centerToast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
val toast = Toast.makeText(this, resId, duration)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
}
/**
* Toast
* @param text CharSequence 类型文本
*/
fun Fragment.toast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
context?.toast(text, duration)
}
/**
* Toast
* @param resId String 类型资源id
*/
fun Fragment.toast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
context?.toast(resId, duration)
}
/**
* 居中Toast
* @param text CharSequence 类型文本
*/
fun Fragment.centerToast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
context?.centerToast(text, duration)
}
/**
* 居中Toast
* @param resId String 类型资源id
*/
fun Fragment.centerToast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
context?.centerToast(resId, duration)
}

File diff suppressed because it is too large Load Diff

View File

@@ -20,9 +20,9 @@ import kotlinx.coroutines.flow.flowOn
* 以顶层函数存在的常用工具方法
* startPolling() -> 开启一个轮询
* sendEvent() -> 发送普通EventBus事件
* toastShow() -> Toast
* isNetworkAvailable() -> 检查是否连接网络
* aRouterJump() -> 阿里路由不带参数跳转
* toast() -> 封装ToastUtils
*/
/**************************************************************************************************/
/**
@@ -50,27 +50,6 @@ suspend fun startPolling(intervals: Long, block: () -> Unit) {
*/
fun sendEvent(event: Any) = EventBusUtils.postEvent(event)
/**************************************************************************************************/
private var mToast: Toast? = null
/**
* Toast
* Android 9.0之上 已做优化
*/
fun toastShow(text: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Toast.makeText(BaseApplication.context, text, duration).show()
} else {
if (mToast != null) {
mToast?.setText(text)
mToast?.show()
} else {
mToast = Toast.makeText(BaseApplication.context, text, duration)
mToast?.show()
}
}
}
/**************************************************************************************************/
/**
* 判断是否连接网络
@@ -101,4 +80,23 @@ fun isNetworkAvailable(): Boolean {
*/
fun aRouterJump(routerUrl: String) {
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)
}