2019-04-11 19:17:54 +00:00
|
|
|
package com.topjohnwu.magisk.utils
|
|
|
|
|
|
|
|
import android.view.View
|
2019-04-24 18:28:41 +00:00
|
|
|
import android.widget.TextView
|
2019-04-14 09:00:16 +00:00
|
|
|
import androidx.annotation.ColorInt
|
2019-04-12 22:14:37 +00:00
|
|
|
import androidx.annotation.DrawableRes
|
|
|
|
import androidx.appcompat.widget.AppCompatImageView
|
2019-04-11 19:17:54 +00:00
|
|
|
import androidx.appcompat.widget.Toolbar
|
|
|
|
import androidx.databinding.BindingAdapter
|
2019-04-22 12:11:41 +00:00
|
|
|
import androidx.databinding.InverseBindingAdapter
|
|
|
|
import androidx.databinding.InverseBindingListener
|
2019-04-16 17:00:32 +00:00
|
|
|
import androidx.drawerlayout.widget.DrawerLayout
|
2019-04-24 18:28:41 +00:00
|
|
|
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
|
2019-05-02 14:23:20 +00:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2019-04-22 12:11:41 +00:00
|
|
|
import androidx.viewpager.widget.ViewPager
|
2019-04-16 17:00:32 +00:00
|
|
|
import com.google.android.material.navigation.NavigationView
|
2019-04-24 18:28:41 +00:00
|
|
|
import com.skoumal.teanity.extensions.subscribeK
|
2019-04-19 14:32:01 +00:00
|
|
|
import com.topjohnwu.magisk.R
|
|
|
|
import com.topjohnwu.magisk.model.entity.state.IndeterminateState
|
2019-04-24 18:28:41 +00:00
|
|
|
import io.reactivex.Observable
|
|
|
|
import io.reactivex.disposables.Disposable
|
|
|
|
import java.util.concurrent.TimeUnit
|
2019-04-11 19:17:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@BindingAdapter("onNavigationClick")
|
|
|
|
fun setOnNavigationClickedListener(view: Toolbar, listener: View.OnClickListener) {
|
|
|
|
view.setNavigationOnClickListener(listener)
|
|
|
|
}
|
2019-04-12 22:14:37 +00:00
|
|
|
|
2019-04-16 17:00:32 +00:00
|
|
|
@BindingAdapter("onNavigationClick")
|
|
|
|
fun setOnNavigationClickedListener(
|
|
|
|
view: NavigationView,
|
|
|
|
listener: NavigationView.OnNavigationItemSelectedListener
|
|
|
|
) {
|
|
|
|
view.setNavigationItemSelectedListener {
|
|
|
|
(view.parent as? DrawerLayout)?.closeDrawers()
|
|
|
|
listener.onNavigationItemSelected(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 22:14:37 +00:00
|
|
|
@BindingAdapter("srcCompat")
|
|
|
|
fun setImageResource(view: AppCompatImageView, @DrawableRes resId: Int) {
|
|
|
|
view.setImageResource(resId)
|
|
|
|
}
|
2019-04-14 09:00:16 +00:00
|
|
|
|
|
|
|
@BindingAdapter("app:tint")
|
|
|
|
fun setTint(view: AppCompatImageView, @ColorInt tint: Int) {
|
|
|
|
view.setColorFilter(tint)
|
|
|
|
}
|
2019-04-19 14:32:01 +00:00
|
|
|
|
|
|
|
@BindingAdapter("isChecked")
|
|
|
|
fun setChecked(view: AppCompatImageView, isChecked: Boolean) {
|
|
|
|
val state = when (isChecked) {
|
|
|
|
true -> IndeterminateState.CHECKED
|
|
|
|
else -> IndeterminateState.UNCHECKED
|
|
|
|
}
|
|
|
|
setChecked(view, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
@BindingAdapter("isChecked")
|
|
|
|
fun setChecked(view: AppCompatImageView, isChecked: IndeterminateState) {
|
|
|
|
view.setImageResource(
|
|
|
|
when (isChecked) {
|
|
|
|
IndeterminateState.INDETERMINATE -> R.drawable.ic_indeterminate
|
|
|
|
IndeterminateState.CHECKED -> R.drawable.ic_checked
|
|
|
|
IndeterminateState.UNCHECKED -> R.drawable.ic_unchecked
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2019-04-22 12:11:41 +00:00
|
|
|
|
|
|
|
@BindingAdapter("position")
|
|
|
|
fun setPosition(view: ViewPager, position: Int) {
|
|
|
|
view.currentItem = position
|
|
|
|
}
|
|
|
|
|
|
|
|
@InverseBindingAdapter(attribute = "position", event = "positionChanged")
|
|
|
|
fun getPosition(view: ViewPager) = view.currentItem
|
|
|
|
|
|
|
|
@BindingAdapter("positionChanged")
|
|
|
|
fun setPositionChangedListener(view: ViewPager, listener: InverseBindingListener) {
|
|
|
|
view.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
|
|
|
|
override fun onPageSelected(position: Int) = listener.onChange()
|
|
|
|
override fun onPageScrollStateChanged(state: Int) = listener.onChange()
|
|
|
|
override fun onPageScrolled(
|
|
|
|
position: Int,
|
|
|
|
positionOffset: Float,
|
|
|
|
positionOffsetPixels: Int
|
|
|
|
) = listener.onChange()
|
|
|
|
})
|
2019-04-24 18:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@BindingAdapter("invisibleScale")
|
|
|
|
fun setInvisibleWithScale(view: View, isInvisible: Boolean) {
|
|
|
|
view.animate()
|
|
|
|
.scaleX(if (isInvisible) 0f else 1f)
|
|
|
|
.scaleY(if (isInvisible) 0f else 1f)
|
|
|
|
.setInterpolator(FastOutSlowInInterpolator())
|
|
|
|
.start()
|
|
|
|
}
|
|
|
|
|
|
|
|
@BindingAdapter("movieBehavior", "movieBehaviorText")
|
|
|
|
fun setMovieBehavior(view: TextView, isMovieBehavior: Boolean, text: String) {
|
|
|
|
(view.tag as? Disposable)?.dispose()
|
|
|
|
if (isMovieBehavior) {
|
|
|
|
val observer = Observable
|
|
|
|
.interval(150, TimeUnit.MILLISECONDS)
|
|
|
|
.subscribeK {
|
|
|
|
view.text = text.replaceRandomWithSpecial()
|
|
|
|
}
|
|
|
|
view.tag = observer
|
|
|
|
} else {
|
|
|
|
view.text = text
|
|
|
|
}
|
2019-04-26 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 18:01:47 +00:00
|
|
|
/*@BindingAdapter("selection"*//*, "selectionAttrChanged", "adapter"*//*)
|
2019-04-26 19:23:25 +00:00
|
|
|
fun setSelectedItemPosition(view: Spinner, position: Int) {
|
|
|
|
view.setSelection(position)
|
|
|
|
}
|
|
|
|
|
|
|
|
@InverseBindingAdapter(
|
|
|
|
attribute = "android:selectedItemPosition",
|
|
|
|
event = "android:selectedItemPositionAttrChanged"
|
|
|
|
)
|
|
|
|
fun getSelectedItemPosition(view: Spinner) = view.selectedItemPosition
|
|
|
|
|
2019-05-23 18:01:47 +00:00
|
|
|
@BindingAdapter("selectedItemPositionAttrChanged")
|
2019-04-26 19:23:25 +00:00
|
|
|
fun setSelectedItemPositionListener(view: Spinner, listener: InverseBindingListener) {
|
|
|
|
view.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
|
|
|
override fun onNothingSelected(p0: AdapterView<*>?) {
|
|
|
|
listener.onChange()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
|
|
|
|
listener.onChange()
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 18:01:47 +00:00
|
|
|
}*/
|
2019-04-26 19:23:25 +00:00
|
|
|
|
|
|
|
@BindingAdapter("onTouch")
|
|
|
|
fun setOnTouchListener(view: View, listener: View.OnTouchListener) {
|
|
|
|
view.setOnTouchListener(listener)
|
2019-05-02 14:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@BindingAdapter("scrollToLast")
|
|
|
|
fun setScrollToLast(view: RecyclerView, shouldScrollToLast: Boolean) {
|
|
|
|
|
|
|
|
fun scrollToLast() = view.post {
|
|
|
|
view.scrollToPosition(view.adapter?.itemCount?.minus(1) ?: 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun wait(callback: () -> Unit) {
|
|
|
|
Observable.timer(1, TimeUnit.SECONDS).subscribeK { callback() }
|
|
|
|
}
|
|
|
|
|
|
|
|
fun RecyclerView.Adapter<*>.setListener() {
|
|
|
|
val observer = object : RecyclerView.AdapterDataObserver() {
|
|
|
|
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
|
|
|
scrollToLast()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
registerAdapterDataObserver(observer)
|
2019-05-23 14:50:31 +00:00
|
|
|
view.setTag(R.id.recyclerScrollListener, observer)
|
2019-05-02 14:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fun RecyclerView.Adapter<*>.removeListener() {
|
2019-05-23 14:50:31 +00:00
|
|
|
val observer =
|
|
|
|
view.getTag(R.id.recyclerScrollListener) as? RecyclerView.AdapterDataObserver ?: return
|
2019-05-02 14:23:20 +00:00
|
|
|
unregisterAdapterDataObserver(observer)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun trySetListener(): Unit = view.adapter?.setListener() ?: wait { trySetListener() }
|
|
|
|
|
|
|
|
if (shouldScrollToLast) {
|
|
|
|
trySetListener()
|
|
|
|
} else {
|
|
|
|
view.adapter?.removeListener()
|
|
|
|
}
|
2019-04-22 12:11:41 +00:00
|
|
|
}
|