123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- package com.cooleshow.base.ext
- import android.content.Context
- import android.view.View
- import android.widget.Button
- import android.widget.EditText
- import android.widget.ImageView
- import android.widget.Toast
- import androidx.fragment.app.Fragment
- import com.cooleshow.base.data.protocol.BaseResp
- import com.cooleshow.base.rx.BaseFunc
- import com.cooleshow.base.rx.BaseFuncBoolean
- import com.cooleshow.base.rx.BaseSubscriber
- import com.cooleshow.base.utils.GlideUtils
- import com.cooleshow.base.widgets.DefaultTextWatcher
- import com.kennyc.view.MultiStateView
- import com.trello.rxlifecycle4.LifecycleProvider
- import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
- import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers.mainThread
- import io.reactivex.rxjava3.core.Observable
- import io.reactivex.rxjava3.schedulers.Schedulers
- //Kotlin通用扩展
- /*
- 扩展Observable执行
- */
- fun <T> Observable<T>.excute(subscriber: BaseSubscriber<T>, lifecycleProvider: LifecycleProvider<*>) {
- this.subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .compose(lifecycleProvider.bindToLifecycle())
- .subscribe(subscriber)
- }
- /*
- 扩展数据转换
- */
- fun <T> Observable<BaseResp<T>>.convert():Observable<T>{
- return this.flatMap(BaseFunc())
- }
- /*
- 扩展Boolean类型数据转换
- */
- fun <T> Observable<BaseResp<T>>.convertBoolean():Observable<Boolean>{
- return this.flatMap(BaseFuncBoolean())
- }
- /*
- 扩展点击事件
- */
- fun View.onClick(listener:View.OnClickListener):View{
- setOnClickListener(listener)
- return this
- }
- /*
- 扩展点击事件,参数为方法
- */
- fun View.onClick(method:() -> Unit):View{
- setOnClickListener { method() }
- return this
- }
- /*
- 扩展Button可用性
- */
- fun Button.enable(et:EditText,method: () -> Boolean){
- val btn = this
- et.addTextChangedListener(object : DefaultTextWatcher(){
- override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
- btn.isEnabled = method()
- }
- })
- }
- /*
- ImageView加载网络图片
- */
- fun ImageView.loadUrl(url: String) {
- GlideUtils.loadUrlImage(context, url, this)
- }
- /*
- 多状态视图开始加载
- */
- //fun MultiStateView.startLoading(){
- // viewState = MultiStateView.VIEW_STATE_LOADING
- // val loadingView = getView(MultiStateView.VIEW_STATE_LOADING)
- // val animBackground = loadingView!!.find<View>(R.id.loading_anim_view).background
- // (animBackground as AnimationDrawable).start()
- //}
- /*
- 扩展视图可见性
- */
- fun View.setVisible(visible:Boolean){
- this.visibility = if (visible) View.VISIBLE else View.GONE
- }
- fun Context.showToast(content: String): Toast {
- val toast = Toast.makeText(this.applicationContext, content, Toast.LENGTH_SHORT)
- toast.show()
- return toast
- }
- fun Context.showLongToast(content: String): Toast {
- val toast = Toast.makeText(this.applicationContext, content, Toast.LENGTH_LONG)
- toast.show()
- return toast
- }
- fun Fragment.showToast(content: String): Toast {
- val toast = Toast.makeText(this.activity?.applicationContext, content, Toast.LENGTH_SHORT)
- toast.show()
- return toast
- }
- fun Fragment.showLongToast(content: String): Toast {
- val toast = Toast.makeText(this.activity?.applicationContext, content, Toast.LENGTH_LONG)
- toast.show()
- return toast
- }
|