CommonExt.kt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.cooleshow.base.ext
  2. import android.content.Context
  3. import android.view.View
  4. import android.widget.Button
  5. import android.widget.EditText
  6. import android.widget.ImageView
  7. import android.widget.Toast
  8. import androidx.fragment.app.Fragment
  9. import com.cooleshow.base.data.protocol.BaseResp
  10. import com.cooleshow.base.rx.BaseFunc
  11. import com.cooleshow.base.rx.BaseFuncBoolean
  12. import com.cooleshow.base.rx.BaseSubscriber
  13. import com.cooleshow.base.utils.GlideUtils
  14. import com.cooleshow.base.widgets.DefaultTextWatcher
  15. import com.kennyc.view.MultiStateView
  16. import com.trello.rxlifecycle4.LifecycleProvider
  17. import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
  18. import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers.mainThread
  19. import io.reactivex.rxjava3.core.Observable
  20. import io.reactivex.rxjava3.schedulers.Schedulers
  21. //Kotlin通用扩展
  22. /*
  23. 扩展Observable执行
  24. */
  25. fun <T> Observable<T>.excute(subscriber: BaseSubscriber<T>, lifecycleProvider: LifecycleProvider<*>) {
  26. this.subscribeOn(Schedulers.io())
  27. .observeOn(AndroidSchedulers.mainThread())
  28. .compose(lifecycleProvider.bindToLifecycle())
  29. .subscribe(subscriber)
  30. }
  31. /*
  32. 扩展数据转换
  33. */
  34. fun <T> Observable<BaseResp<T>>.convert():Observable<T>{
  35. return this.flatMap(BaseFunc())
  36. }
  37. /*
  38. 扩展Boolean类型数据转换
  39. */
  40. fun <T> Observable<BaseResp<T>>.convertBoolean():Observable<Boolean>{
  41. return this.flatMap(BaseFuncBoolean())
  42. }
  43. /*
  44. 扩展点击事件
  45. */
  46. fun View.onClick(listener:View.OnClickListener):View{
  47. setOnClickListener(listener)
  48. return this
  49. }
  50. /*
  51. 扩展点击事件,参数为方法
  52. */
  53. fun View.onClick(method:() -> Unit):View{
  54. setOnClickListener { method() }
  55. return this
  56. }
  57. /*
  58. 扩展Button可用性
  59. */
  60. fun Button.enable(et:EditText,method: () -> Boolean){
  61. val btn = this
  62. et.addTextChangedListener(object : DefaultTextWatcher(){
  63. override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
  64. btn.isEnabled = method()
  65. }
  66. })
  67. }
  68. /*
  69. ImageView加载网络图片
  70. */
  71. fun ImageView.loadUrl(url: String) {
  72. GlideUtils.loadUrlImage(context, url, this)
  73. }
  74. /*
  75. 多状态视图开始加载
  76. */
  77. //fun MultiStateView.startLoading(){
  78. // viewState = MultiStateView.VIEW_STATE_LOADING
  79. // val loadingView = getView(MultiStateView.VIEW_STATE_LOADING)
  80. // val animBackground = loadingView!!.find<View>(R.id.loading_anim_view).background
  81. // (animBackground as AnimationDrawable).start()
  82. //}
  83. /*
  84. 扩展视图可见性
  85. */
  86. fun View.setVisible(visible:Boolean){
  87. this.visibility = if (visible) View.VISIBLE else View.GONE
  88. }
  89. fun Context.showToast(content: String): Toast {
  90. val toast = Toast.makeText(this.applicationContext, content, Toast.LENGTH_SHORT)
  91. toast.show()
  92. return toast
  93. }
  94. fun Context.showLongToast(content: String): Toast {
  95. val toast = Toast.makeText(this.applicationContext, content, Toast.LENGTH_LONG)
  96. toast.show()
  97. return toast
  98. }
  99. fun Fragment.showToast(content: String): Toast {
  100. val toast = Toast.makeText(this.activity?.applicationContext, content, Toast.LENGTH_SHORT)
  101. toast.show()
  102. return toast
  103. }
  104. fun Fragment.showLongToast(content: String): Toast {
  105. val toast = Toast.makeText(this.activity?.applicationContext, content, Toast.LENGTH_LONG)
  106. toast.show()
  107. return toast
  108. }