|
| 1 | +# Android-Kotlin-Extensions |
| 2 | +Useful Kotlin extension functions for Android |
| 3 | + |
| 4 | +## Core |
| 5 | +```kotlin |
| 6 | +inline fun <reified T : Activity> Activity.startActivity() { |
| 7 | + val intent = Intent() |
| 8 | + intent.setClass(this, T::class.java) |
| 9 | + startActivity(intent) |
| 10 | +} |
| 11 | +``` |
| 12 | +```kotlin |
| 13 | +val Int.DP |
| 14 | + get() = TypedValue.applyDimension( |
| 15 | + TypedValue.COMPLEX_UNIT_DIP, |
| 16 | + toFloat(), |
| 17 | + Resources.getSystem().displayMetrics |
| 18 | + ).toInt() |
| 19 | + |
| 20 | +val Float.DP |
| 21 | + get() = TypedValue.applyDimension( |
| 22 | + TypedValue.COMPLEX_UNIT_DIP, |
| 23 | + this, |
| 24 | + Resources.getSystem().displayMetrics |
| 25 | + ) |
| 26 | +``` |
| 27 | +```kotlin |
| 28 | +val Context.windowManager |
| 29 | + get() = getSystemService(Context.WINDOW_SERVICE) as WindowManager |
| 30 | + |
| 31 | +val Context.connectivityManager |
| 32 | + get() = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager |
| 33 | +``` |
| 34 | +```kotlin |
| 35 | +inline fun belowApi(api: Int, included: Boolean = false, block: () -> Unit) { |
| 36 | + if (Build.VERSION.SDK_INT < if (included) api + 1 else api) { |
| 37 | + block() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +inline fun aboveApi(api: Int, included: Boolean = false, block: () -> Unit) { |
| 42 | + if (Build.VERSION.SDK_INT > if (included) api - 1 else api) { |
| 43 | + block() |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | +[Show more extensions](https://github.com/AgustaRC/Android-Kotlin-Extensions/tree/master/android-core/src/main/java/github/agustarc/android/extensions/core) |
| 48 | + |
| 49 | + |
| 50 | +## LiveData |
| 51 | +```kotlin |
| 52 | +fun <X, Y> LiveData<X>.map(body: (X) -> Y): LiveData<Y> { |
| 53 | + return Transformations.map(this, body) |
| 54 | +} |
| 55 | + |
| 56 | +fun <X, Y> LiveData<X>.switchMap(body: (X) -> LiveData<Y>): LiveData<Y> { |
| 57 | + return Transformations.switchMap(this, body) |
| 58 | +} |
| 59 | +``` |
| 60 | +```kotlin |
| 61 | +inline fun <T> LiveData<T>.takeUntil(crossinline predicate: (T?) -> Boolean): LiveData<T> { |
| 62 | + val mediator = MediatorLiveData<T>() |
| 63 | + mediator.addSource(this) { |
| 64 | + if (predicate(it)) { |
| 65 | + mediator.value = it |
| 66 | + } else { |
| 67 | + mediator.removeSource(this) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return mediator |
| 72 | +} |
| 73 | +``` |
| 74 | +```kotlin |
| 75 | +fun <A, B, Result> LiveData<A>.combineLatest( |
| 76 | + other: LiveData<B>, |
| 77 | + combiner: (A, B) -> Result |
| 78 | +): LiveData<Result> { |
| 79 | + val mediator = MediatorLiveData<Result>() |
| 80 | + mediator.addSource(this) { a -> |
| 81 | + val b = other.value |
| 82 | + if (b != null) { |
| 83 | + mediator.value = combiner(a, b) |
| 84 | + } |
| 85 | + } |
| 86 | + mediator.addSource(other) { b -> |
| 87 | + val a = this@combineLatest.value |
| 88 | + if (a != null) { |
| 89 | + mediator.value = combiner(a, b) |
| 90 | + } |
| 91 | + } |
| 92 | + return mediator |
| 93 | +} |
| 94 | +``` |
| 95 | +[Show more extensions](https://github.com/AgustaRC/Android-Kotlin-Extensions/tree/master/android-livedata/src/main/java/github/agustarc/android/extensions/livedata) |
| 96 | + |
| 97 | +## View |
| 98 | +```kotlin |
| 99 | +inline fun <T : View> T.afterMeasured(crossinline block: T.() -> Unit) { |
| 100 | + viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { |
| 101 | + override fun onGlobalLayout() { |
| 102 | + if (measuredWidth > 0 && measuredHeight > 0) { |
| 103 | + viewTreeObserver.removeOnGlobalLayoutListener(this) |
| 104 | + block() |
| 105 | + } |
| 106 | + } |
| 107 | + }) |
| 108 | +} |
| 109 | +``` |
| 110 | +```kotlin |
| 111 | +fun View.isVisible() = visibility == View.VISIBLE |
| 112 | + |
| 113 | +fun View.isInvisible() = visibility == View.INVISIBLE |
| 114 | + |
| 115 | +fun View.isGone() = visibility == View.GONE |
| 116 | +``` |
| 117 | +```kotlin |
| 118 | +fun ViewGroup.inflate(@LayoutRes resId: Int): View = |
| 119 | + LayoutInflater.from(context).inflate(resId, this, false) |
| 120 | + |
| 121 | +val ViewGroup.children: List<View> |
| 122 | + get() = (0 until childCount).map { getChildAt(it) } |
| 123 | + |
| 124 | +operator fun ViewGroup.get(index: Int): View? = getChildAt(index) |
| 125 | + |
| 126 | +operator fun ViewGroup.plusAssign(child: View) = addView(child) |
| 127 | + |
| 128 | +operator fun ViewGroup.minusAssign(child: View) = removeView(child) |
| 129 | + |
| 130 | +operator fun ViewGroup.contains(child: View) = indexOfChild(child) != -1 |
| 131 | +``` |
| 132 | +[Show more extensions](https://github.com/AgustaRC/Android-Kotlin-Extensions/tree/master/android-view/src/main/java/github/agustarc/android/extensions/view) |
0 commit comments