| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- package com.narutohuo.xindazhou.common.ui
- import android.view.View
- import android.widget.Toast
- import androidx.fragment.app.Fragment
- import com.google.android.material.snackbar.Snackbar
- /**
- * 消息提示管理器
- *
- * 统一封装 Toast 和 Snackbar,提供便捷的消息提示方式
- *
- * 使用方式:
- * ```kotlin
- * // Toast 提示
- * MessageHelper.showToast("操作成功")
- * MessageHelper.showSuccess("保存成功")
- * MessageHelper.showError("操作失败")
- *
- * // Snackbar 提示
- * MessageHelper.showSnackbar(view, "操作成功")
- * MessageHelper.showSnackbar(view, "操作失败", "重试") { /* 重试操作 */ }
- * ```
- */
- object MessageHelper {
-
- /**
- * 是否使用 Snackbar(默认使用 Toast)
- * 可以在 Application 中设置为 true,统一使用 Snackbar
- */
- var useSnackbar: Boolean = false
-
- /**
- * Toast 显示时长
- */
- var toastDuration: Int = Toast.LENGTH_SHORT
-
- /**
- * Snackbar 显示时长(毫秒)
- */
- var snackbarDuration: Int = 2000
-
- /**
- * 显示 Toast 消息
- *
- * @param view View 上下文(Activity 或 Fragment 的 View)
- * @param message 消息内容
- */
- fun showToast(view: View, message: String) {
- if (useSnackbar) {
- showSnackbar(view, message)
- } else {
- val context = view.context
- Toast.makeText(context, message, toastDuration).show()
- }
- }
-
- /**
- * 显示 Toast 消息(Activity 扩展函数)
- */
- fun androidx.appcompat.app.AppCompatActivity.showToast(message: String) {
- if (useSnackbar) {
- val rootView = window.decorView.findViewById<View>(android.R.id.content)
- showSnackbar(rootView, message)
- } else {
- Toast.makeText(this, message, toastDuration).show()
- }
- }
-
- /**
- * 显示 Toast 消息(Fragment 扩展函数)
- */
- fun Fragment.showToast(message: String) {
- if (useSnackbar) {
- view?.let { showSnackbar(it, message) }
- } else {
- Toast.makeText(requireContext(), message, toastDuration).show()
- }
- }
-
- /**
- * 显示成功消息
- *
- * @param view View 上下文
- * @param message 消息内容
- */
- fun showSuccess(view: View, message: String) {
- showToast(view, message)
- }
-
- /**
- * 显示成功消息(Activity 扩展函数)
- */
- fun androidx.appcompat.app.AppCompatActivity.showSuccess(message: String) {
- showToast(message)
- }
-
- /**
- * 显示成功消息(Fragment 扩展函数)
- */
- fun Fragment.showSuccess(message: String) {
- showToast(message)
- }
-
- /**
- * 显示错误消息
- *
- * @param view View 上下文
- * @param message 消息内容
- */
- fun showError(view: View, message: String) {
- showToast(view, message)
- }
-
- /**
- * 显示错误消息(Activity 扩展函数)
- */
- fun androidx.appcompat.app.AppCompatActivity.showError(message: String) {
- showToast(message)
- }
-
- /**
- * 显示错误消息(Fragment 扩展函数)
- */
- fun Fragment.showError(message: String) {
- showToast(message)
- }
-
- /**
- * 显示 Snackbar 消息
- *
- * @param view 父 View
- * @param message 消息内容
- * @param actionText 操作按钮文本(可选)
- * @param action 操作按钮点击回调(可选)
- */
- fun showSnackbar(
- view: View,
- message: String,
- actionText: String? = null,
- action: (() -> Unit)? = null
- ) {
- val snackbar = Snackbar.make(view, message, snackbarDuration)
-
- actionText?.let { text ->
- action?.let { callback ->
- snackbar.setAction(text) {
- callback()
- }
- }
- }
-
- snackbar.show()
- }
-
- /**
- * 显示 Snackbar 消息(Activity 扩展函数)
- */
- fun androidx.appcompat.app.AppCompatActivity.showSnackbar(
- message: String,
- actionText: String? = null,
- action: (() -> Unit)? = null
- ) {
- val rootView = window.decorView.findViewById<View>(android.R.id.content)
- showSnackbar(rootView, message, actionText, action)
- }
-
- /**
- * 显示 Snackbar 消息(Fragment 扩展函数)
- */
- fun Fragment.showSnackbar(
- message: String,
- actionText: String? = null,
- action: (() -> Unit)? = null
- ) {
- view?.let {
- showSnackbar(it, message, actionText, action)
- }
- }
- }
|