MessageHelper.kt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package com.narutohuo.xindazhou.common.ui
  2. import android.view.View
  3. import android.widget.Toast
  4. import androidx.fragment.app.Fragment
  5. import com.google.android.material.snackbar.Snackbar
  6. /**
  7. * 消息提示管理器
  8. *
  9. * 统一封装 Toast 和 Snackbar,提供便捷的消息提示方式
  10. *
  11. * 使用方式:
  12. * ```kotlin
  13. * // Toast 提示
  14. * MessageHelper.showToast("操作成功")
  15. * MessageHelper.showSuccess("保存成功")
  16. * MessageHelper.showError("操作失败")
  17. *
  18. * // Snackbar 提示
  19. * MessageHelper.showSnackbar(view, "操作成功")
  20. * MessageHelper.showSnackbar(view, "操作失败", "重试") { /* 重试操作 */ }
  21. * ```
  22. */
  23. object MessageHelper {
  24. /**
  25. * 是否使用 Snackbar(默认使用 Toast)
  26. * 可以在 Application 中设置为 true,统一使用 Snackbar
  27. */
  28. var useSnackbar: Boolean = false
  29. /**
  30. * Toast 显示时长
  31. */
  32. var toastDuration: Int = Toast.LENGTH_SHORT
  33. /**
  34. * Snackbar 显示时长(毫秒)
  35. */
  36. var snackbarDuration: Int = 2000
  37. /**
  38. * 显示 Toast 消息
  39. *
  40. * @param view View 上下文(Activity 或 Fragment 的 View)
  41. * @param message 消息内容
  42. */
  43. fun showToast(view: View, message: String) {
  44. if (useSnackbar) {
  45. showSnackbar(view, message)
  46. } else {
  47. val context = view.context
  48. Toast.makeText(context, message, toastDuration).show()
  49. }
  50. }
  51. /**
  52. * 显示 Toast 消息(Activity 扩展函数)
  53. */
  54. fun androidx.appcompat.app.AppCompatActivity.showToast(message: String) {
  55. if (useSnackbar) {
  56. val rootView = window.decorView.findViewById<View>(android.R.id.content)
  57. showSnackbar(rootView, message)
  58. } else {
  59. Toast.makeText(this, message, toastDuration).show()
  60. }
  61. }
  62. /**
  63. * 显示 Toast 消息(Fragment 扩展函数)
  64. */
  65. fun Fragment.showToast(message: String) {
  66. if (useSnackbar) {
  67. view?.let { showSnackbar(it, message) }
  68. } else {
  69. Toast.makeText(requireContext(), message, toastDuration).show()
  70. }
  71. }
  72. /**
  73. * 显示成功消息
  74. *
  75. * @param view View 上下文
  76. * @param message 消息内容
  77. */
  78. fun showSuccess(view: View, message: String) {
  79. showToast(view, message)
  80. }
  81. /**
  82. * 显示成功消息(Activity 扩展函数)
  83. */
  84. fun androidx.appcompat.app.AppCompatActivity.showSuccess(message: String) {
  85. showToast(message)
  86. }
  87. /**
  88. * 显示成功消息(Fragment 扩展函数)
  89. */
  90. fun Fragment.showSuccess(message: String) {
  91. showToast(message)
  92. }
  93. /**
  94. * 显示错误消息
  95. *
  96. * @param view View 上下文
  97. * @param message 消息内容
  98. */
  99. fun showError(view: View, message: String) {
  100. showToast(view, message)
  101. }
  102. /**
  103. * 显示错误消息(Activity 扩展函数)
  104. */
  105. fun androidx.appcompat.app.AppCompatActivity.showError(message: String) {
  106. showToast(message)
  107. }
  108. /**
  109. * 显示错误消息(Fragment 扩展函数)
  110. */
  111. fun Fragment.showError(message: String) {
  112. showToast(message)
  113. }
  114. /**
  115. * 显示 Snackbar 消息
  116. *
  117. * @param view 父 View
  118. * @param message 消息内容
  119. * @param actionText 操作按钮文本(可选)
  120. * @param action 操作按钮点击回调(可选)
  121. */
  122. fun showSnackbar(
  123. view: View,
  124. message: String,
  125. actionText: String? = null,
  126. action: (() -> Unit)? = null
  127. ) {
  128. val snackbar = Snackbar.make(view, message, snackbarDuration)
  129. actionText?.let { text ->
  130. action?.let { callback ->
  131. snackbar.setAction(text) {
  132. callback()
  133. }
  134. }
  135. }
  136. snackbar.show()
  137. }
  138. /**
  139. * 显示 Snackbar 消息(Activity 扩展函数)
  140. */
  141. fun androidx.appcompat.app.AppCompatActivity.showSnackbar(
  142. message: String,
  143. actionText: String? = null,
  144. action: (() -> Unit)? = null
  145. ) {
  146. val rootView = window.decorView.findViewById<View>(android.R.id.content)
  147. showSnackbar(rootView, message, actionText, action)
  148. }
  149. /**
  150. * 显示 Snackbar 消息(Fragment 扩展函数)
  151. */
  152. fun Fragment.showSnackbar(
  153. message: String,
  154. actionText: String? = null,
  155. action: (() -> Unit)? = null
  156. ) {
  157. view?.let {
  158. showSnackbar(it, message, actionText, action)
  159. }
  160. }
  161. }