That fuck shit the fascists are using
1package org.tm.archive.util
2
3import android.graphics.Canvas
4import android.view.View
5import android.view.ViewGroup
6import android.widget.TextView
7import androidx.annotation.DrawableRes
8import androidx.constraintlayout.widget.ConstraintLayout
9import androidx.constraintlayout.widget.ConstraintSet
10import androidx.core.view.doOnNextLayout
11import androidx.fragment.app.Fragment
12import androidx.fragment.app.findFragment
13import androidx.lifecycle.Lifecycle
14
15var View.visible: Boolean
16 get() {
17 return this.visibility == View.VISIBLE
18 }
19 set(value) {
20 this.visibility = if (value) View.VISIBLE else View.GONE
21 }
22
23fun View.padding(left: Int = paddingLeft, top: Int = paddingTop, right: Int = paddingRight, bottom: Int = paddingBottom) {
24 setPadding(left, top, right, bottom)
25}
26
27fun ConstraintLayout.changeConstraints(change: ConstraintSet.() -> Unit) {
28 val set = ConstraintSet()
29 set.clone(this)
30 set.change()
31 set.applyTo(this)
32}
33
34inline fun View.doOnEachLayout(crossinline action: (view: View) -> Unit): View.OnLayoutChangeListener {
35 val listener = View.OnLayoutChangeListener { view, _, _, _, _, _, _, _, _ -> action(view) }
36 addOnLayoutChangeListener(listener)
37 return listener
38}
39
40/**
41 * OnLayout gets called prior to the view *actually* being laid out. This
42 * method will wait until the next layout and then post the action to happen
43 * afterwards.
44 */
45inline fun View.doAfterNextLayout(crossinline action: () -> Unit) {
46 doOnNextLayout {
47 post { action() }
48 }
49}
50
51fun TextView.setRelativeDrawables(
52 @DrawableRes start: Int = 0,
53 @DrawableRes top: Int = 0,
54 @DrawableRes bottom: Int = 0,
55 @DrawableRes end: Int = 0
56) {
57 setCompoundDrawablesRelativeWithIntrinsicBounds(
58 start,
59 top,
60 end,
61 bottom
62 )
63}
64
65/**
66 * Get a lifecycle associated with this view. Care must be taken to ensure
67 * if activity fallback occurs that the context of the view is correct.
68 */
69fun View.getLifecycle(): Lifecycle? {
70 return try {
71 findFragment<Fragment>().viewLifecycleOwner.lifecycle
72 } catch (e: IllegalStateException) {
73 ViewUtil.getActivityLifecycle(this)
74 }
75}
76
77fun View.layoutIn(parent: View) {
78 val widthSpec = View.MeasureSpec.makeMeasureSpec(parent.width, View.MeasureSpec.EXACTLY)
79 val heightSpec = View.MeasureSpec.makeMeasureSpec(parent.height, View.MeasureSpec.UNSPECIFIED)
80 val childWidth = ViewGroup.getChildMeasureSpec(widthSpec, parent.paddingLeft + parent.paddingRight, layoutParams.width)
81 val childHeight = ViewGroup.getChildMeasureSpec(heightSpec, parent.paddingTop + parent.paddingBottom, layoutParams.height)
82 measure(childWidth, childHeight)
83 layout(0, 0, measuredWidth, measuredHeight)
84}
85
86fun View.drawAsTopItemDecoration(canvas: Canvas, parent: View, child: View, offset: Int = 0) {
87 canvas.save()
88 val left = parent.left
89 val top = child.y.toInt() - height - offset
90 canvas.translate(left.toFloat(), top.toFloat())
91 draw(canvas)
92 canvas.restore()
93}