Created
January 3, 2020 15:42
-
-
Save PollyGlot/ce23cba2ab9dd8aea5bdc3d5ca3de2e9 to your computer and use it in GitHub Desktop.
A list of extension functions I created for a Kinomap project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kinomap.kinomapcommon.util | |
import android.content.SharedPreferences | |
import android.content.res.Resources | |
import android.graphics.drawable.Drawable | |
import android.os.Build | |
import android.view.View | |
import androidx.annotation.DrawableRes | |
import androidx.annotation.PluralsRes | |
import androidx.fragment.app.FragmentManager | |
import androidx.fragment.app.FragmentTransaction | |
import androidx.core.content.res.ResourcesCompat | |
import androidx.recyclerview.widget.LinearSmoothScroller | |
import androidx.recyclerview.widget.RecyclerView | |
import android.widget.Button | |
import android.widget.TextView | |
import androidx.core.content.ContextCompat | |
import com.kinomap.kinomapcommon.R | |
inline fun consume(f: () -> Unit): Boolean { | |
f() | |
return true | |
} | |
inline fun androidx.fragment.app.FragmentManager.inTransaction(func: androidx.fragment.app.FragmentTransaction.() -> androidx.fragment.app.FragmentTransaction) { | |
beginTransaction().func().commit() | |
} | |
fun Resources.drawable(@DrawableRes drawable: Int, theme: Resources.Theme? = null): Drawable { | |
return ResourcesCompat.getDrawable(this, drawable, theme)!! | |
} | |
fun Button.setDrawable(start: Int, top: Int, end: Int, bottom: Int) { | |
if (Build.VERSION.SDK_INT >= 17) | |
setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom) | |
else | |
setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom) | |
} | |
fun TextView.setDrawable(start: Int, top: Int, end: Int, bottom: Int) { | |
if (Build.VERSION.SDK_INT >= 17) | |
setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom) | |
else | |
setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom) | |
} | |
fun TextView.setDrawableLeftWithBounds(drawableId: Int, width: Int, height: Int) { | |
val img: Drawable = ContextCompat.getDrawable(context, drawableId)!! | |
img.setBounds(0, 0, width, height) | |
if (Build.VERSION.SDK_INT >= 17) | |
setCompoundDrawablesRelativeWithIntrinsicBounds(img, null, null, null) | |
else | |
setCompoundDrawablesWithIntrinsicBounds(img, null, null, null) | |
} | |
fun androidx.recyclerview.widget.RecyclerView.smoothSnapToPosition(position: Int, snapMode: Int = androidx.recyclerview.widget.LinearSmoothScroller.SNAP_TO_START) { | |
val smoothScroller = object : androidx.recyclerview.widget.LinearSmoothScroller(this.context) { | |
override fun getVerticalSnapPreference(): Int { | |
return snapMode | |
} | |
override fun getHorizontalSnapPreference(): Int { | |
return snapMode | |
} | |
} | |
smoothScroller.targetPosition = position | |
layoutManager?.startSmoothScroll(smoothScroller) | |
} | |
fun SharedPreferences.Editor.putDouble(key: String, double: Double) = | |
putLong(key, java.lang.Double.doubleToRawLongBits(double)) | |
fun SharedPreferences.getDouble(key: String, default: Double) = | |
java.lang.Double.longBitsToDouble(getLong(key, java.lang.Double.doubleToRawLongBits(default))) | |
fun View.setSafeOnClickListener(onSafeClick: (View) -> Unit) { | |
val safeClickListener = SafeClickListener { | |
onSafeClick(it) | |
} | |
setOnClickListener(safeClickListener) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment