Skip to content

Instantly share code, notes, and snippets.

@o0o0oo00
Last active August 10, 2021 08:03
Show Gist options
  • Save o0o0oo00/ab04f1bbfec888f6dd9fa251fd85f150 to your computer and use it in GitHub Desktop.
Save o0o0oo00/ab04f1bbfec888f6dd9fa251fd85f150 to your computer and use it in GitHub Desktop.
万事万物皆可Observable
import android.content.Context
import android.graphics.Bitmap
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.duodian.qugame.base.GlideApp
import io.reactivex.Observable
import io.reactivex.ObservableEmitter
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.BiFunction
import java.util.concurrent.TimeUnit
/**
* @author: zhaochunyu
* @description: 万事万物皆可Observable
* @date: 2020/9/21
*/
// 防止双击
fun View.notDoubleClick(time: Long = 600): Observable<View> {
var emitter: ObservableEmitter<View>? = null
val observable = Observable.create<View> {
emitter = it
}
setOnClickListener {
emitter?.onNext(this)
}
return observable.throttleFirst(time, TimeUnit.MILLISECONDS) // 600 ms 内只发射第一条数据
}
// EditText输入监听包装成Observable
fun EditText.getTextChangedObservable(): Observable<String> {
var emitter: ObservableEmitter<String>? = null
val observable = Observable.create<String> {
emitter = it
}
val textWatcher = object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
s?.also {
emitter?.onNext(s.toString())
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
}
val actionListener = TextView.OnEditorActionListener { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
val im = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (im.isActive) {
rootView?.findFocus()?.windowToken?.let { im.hideSoftInputFromWindow(it, 0) }
}
[email protected]()
emitter?.onNext([email protected]())
true
} else {
false
}
}
this.addTextChangedListener(textWatcher)
this.setOnEditorActionListener(actionListener)
return observable.doFinally {
[email protected](textWatcher)
[email protected](null)
}
}
// 搜索框优化,减少请求次数
fun EditText.searchTextWatcher(): Observable<String> {
return this.getTextChangedObservable()
.debounce(400, TimeUnit.MILLISECONDS) // 两次发射数据间隔小于指定时间,就会丢弃前一次数据,知道指定时间内没有新数据时,才会发射最后一次数据
.observeOn(AndroidSchedulers.mainThread())
}
fun Context.downloadBitmapObservable(url: Any?): Observable<Bitmap> {
var emitter: ObservableEmitter<Bitmap>? = null
val result = Observable.create<Bitmap> {
emitter = it
}
GlideApp.with(this)
.asBitmap()
.load(url)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(object : SimpleTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
emitter?.onNext(resource)
}
})
return result
}
fun <T> Observable<T>.condition(boolean: Boolean, doCondition: Observable<T>.() -> Observable<T>): Observable<T> {
return if (boolean) {
this.doCondition()
} else {
this
}
}
fun <T1, T2> pairBiFunction(): BiFunction<T1, T2, Pair<T1, T2>> {
return BiFunction { t1, t2 ->
Pair(t1, t2)
}
}
fun <T> Observer<T>.debounceObserver(): Observer<T> {
return this
}
fun <T> MutableLiveData<T>.toObservable(lifecycleOwner: LifecycleOwner): Observable<T> {
var emitter: ObservableEmitter<T>? = null
val result = Observable.create<T> {
emitter = it
}
this.observe(lifecycleOwner, Observer {
emitter?.onNext(it)
})
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment