Reorganize other util files

This commit is contained in:
arkon
2020-02-02 22:22:54 -05:00
parent faf8f1fbbc
commit 47f5ea881f
123 changed files with 218 additions and 192 deletions
@@ -0,0 +1,19 @@
package eu.kanade.tachiyomi.util.view
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
/**
* Set a vector on a [ImageView].
*
* @param drawable id of drawable resource
*/
fun ImageView.setVectorCompat(@DrawableRes drawable: Int, tint: Int? = null) {
val vector = VectorDrawableCompat.create(resources, drawable, context.theme)
if (tint != null) {
vector?.mutate()
vector?.setTint(tint)
}
setImageDrawable(vector)
}
@@ -0,0 +1,71 @@
@file:Suppress("NOTHING_TO_INLINE")
package eu.kanade.tachiyomi.util.view
import android.graphics.Color
import android.graphics.Point
import android.graphics.Typeface
import android.view.View
import android.widget.TextView
import com.amulyakhare.textdrawable.TextDrawable
import com.amulyakhare.textdrawable.util.ColorGenerator
import com.google.android.material.snackbar.Snackbar
import kotlin.math.min
/**
* Returns coordinates of view.
* Used for animation
*
* @return coordinates of view
*/
fun View.getCoordinates() = Point((left + right) / 2, (top + bottom) / 2)
/**
* Shows a snackbar in this view.
*
* @param message the message to show.
* @param length the duration of the snack.
* @param f a function to execute in the snack, allowing for example to define a custom action.
*/
inline fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit): Snackbar {
val snack = Snackbar.make(this, message, length)
val textView: TextView = snack.view.findViewById(com.google.android.material.R.id.snackbar_text)
textView.setTextColor(Color.WHITE)
snack.f()
snack.show()
return snack
}
inline fun View.visible() {
visibility = View.VISIBLE
}
inline fun View.invisible() {
visibility = View.INVISIBLE
}
inline fun View.gone() {
visibility = View.GONE
}
inline fun View.visibleIf(block: () -> Boolean) {
visibility = if (block()) View.VISIBLE else View.GONE
}
/**
* Returns a TextDrawable determined by input
*
* @param text text of [TextDrawable]
* @param random random color
*/
fun View.getRound(text: String, random : Boolean = true): TextDrawable {
val size = min(this.width, this.height)
return TextDrawable.builder()
.beginConfig()
.width(size)
.height(size)
.textColor(Color.WHITE)
.useFont(Typeface.DEFAULT)
.endConfig()
.buildRound(text, if (random) ColorGenerator.MATERIAL.randomColor else ColorGenerator.MATERIAL.getColor(text))
}
@@ -0,0 +1,15 @@
package eu.kanade.tachiyomi.util.view
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
/**
* Extension method to inflate a view directly from its parent.
* @param layout the layout to inflate.
* @param attachToRoot whether to attach the view to the root or not. Defaults to false.
*/
fun ViewGroup.inflate(@LayoutRes layout: Int, attachToRoot: Boolean = false): View {
return LayoutInflater.from(context).inflate(layout, this, attachToRoot)
}