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-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-22 12:11:41 +00:00
|
|
|
}
|