2017-02-04 19:07:06 +01:00
|
|
|
@file:Suppress("NOTHING_TO_INLINE")
|
|
|
|
|
2016-03-19 15:39:19 +01:00
|
|
|
package eu.kanade.tachiyomi.util
|
|
|
|
|
2016-04-10 00:59:12 +02:00
|
|
|
import android.graphics.Color
|
2016-03-19 15:39:19 +01:00
|
|
|
import android.graphics.Point
|
2017-09-23 13:11:39 +02:00
|
|
|
import android.graphics.Typeface
|
2016-04-10 00:59:12 +02:00
|
|
|
import android.support.design.widget.Snackbar
|
2016-03-19 15:39:19 +01:00
|
|
|
import android.view.View
|
2016-04-10 00:59:12 +02:00
|
|
|
import android.widget.TextView
|
2017-09-23 13:11:39 +02:00
|
|
|
import com.amulyakhare.textdrawable.TextDrawable
|
|
|
|
import com.amulyakhare.textdrawable.util.ColorGenerator
|
2016-03-19 15:39:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns coordinates of view.
|
|
|
|
* Used for animation
|
|
|
|
*
|
|
|
|
* @return coordinates of view
|
|
|
|
*/
|
2016-04-10 00:59:12 +02:00
|
|
|
fun View.getCoordinates() = Point((left + right) / 2, (top + bottom) / 2)
|
2016-03-19 15:39:19 +01:00
|
|
|
|
2016-04-10 00:59:12 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-01-08 18:12:19 +01:00
|
|
|
inline fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit): Snackbar {
|
2016-04-10 00:59:12 +02:00
|
|
|
val snack = Snackbar.make(this, message, length)
|
2017-09-23 17:14:04 +02:00
|
|
|
val textView: TextView = snack.view.findViewById(android.support.design.R.id.snackbar_text)
|
2016-04-10 00:59:12 +02:00
|
|
|
textView.setTextColor(Color.WHITE)
|
|
|
|
snack.f()
|
|
|
|
snack.show()
|
2017-01-08 18:12:19 +01:00
|
|
|
return snack
|
2017-02-04 19:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline fun View.visible() {
|
|
|
|
visibility = View.VISIBLE
|
|
|
|
}
|
|
|
|
|
|
|
|
inline fun View.invisible() {
|
|
|
|
visibility = View.INVISIBLE
|
|
|
|
}
|
|
|
|
|
|
|
|
inline fun View.gone() {
|
|
|
|
visibility = View.GONE
|
|
|
|
}
|
2017-09-23 13:11:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = Math.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))
|
|
|
|
}
|