-
-
Save sergchil/9c996aad722c772fb4bda3aaa052d9b8 to your computer and use it in GitHub Desktop.
Kotlin extension functions
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 pl.kpob.utils.extensions | |
import android.app.Activity | |
import android.content.Context | |
import android.graphics.Color | |
import android.support.v4.content.ContextCompat | |
import android.view.WindowManager | |
import flow.Flow | |
import org.jetbrains.anko.AlertDialogBuilder | |
import pl.sisms.gminformix.utils.extensions.supportsLollipop | |
/** | |
* Created by kpob on 3/18/16. | |
*/ | |
fun Context.flow() = Flow.get(this) | |
fun Context.color(id: Int) : Int = ContextCompat.getColor(this, id) | |
fun Context.alert( | |
message: String, | |
title: String? = null, | |
init: (AlertDialogBuilder.() -> Unit)? = null) = | |
AlertDialogBuilder(this).apply { | |
setTheme(R.style.dialog) | |
if (title != null) title(title) | |
message(message) | |
if (init != null) init() | |
} | |
fun Context.alert( | |
message: Int, | |
title: Int? = null, | |
init: (AlertDialogBuilder.() -> Unit)? = null) = | |
AlertDialogBuilder(this).apply { | |
setTheme(R.style.dialog) | |
if (title != null) title(title) | |
message(message) | |
if (init != null) init() | |
} | |
fun Activity.setStatusBarColor(colorResId: Int) { | |
supportsLollipop { | |
with(window) { | |
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) | |
clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) | |
statusBarColor = resources.getColor(colorResId) | |
} | |
} | |
} |
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 pl.kpob.utils.extensions | |
import android.animation.Animator | |
import android.animation.ObjectAnimator | |
import android.content.Context | |
import android.content.Intent | |
import android.os.Build | |
import android.provider.Settings | |
import android.view.View | |
import android.view.animation.AlphaAnimation | |
import android.view.animation.Animation | |
import com.crashlytics.android.Crashlytics | |
import java.util.* | |
import java.util.concurrent.TimeUnit | |
/** | |
* Created by kpob on 1/27/16. | |
*/ | |
fun Int.isOdd() = this.mod(2) == 1 | |
fun Int.isEven() = this.mod(2) == 0 | |
fun Int.format(digits: Int) = String.format("%0${digits}d%n", this) | |
fun Long.toMinutes() = TimeUnit.MILLISECONDS.toMinutes(this) | |
fun Long.toSeconds() = TimeUnit.MILLISECONDS.toSeconds(this) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(this)) | |
inline fun supportsLollipop(func: () -> Unit) = | |
supportsVersion(Build.VERSION_CODES.LOLLIPOP, func) | |
inline fun supportsVersion(ver: Int, func: () -> Unit) { | |
if(Build.VERSION.SDK_INT >= ver) { | |
func.invoke() | |
} | |
} | |
inline fun inReleaseMode(func: () -> Unit) { | |
if(BuildConfig.BUILD_TYPE.equals("release")) { | |
func() | |
} | |
} | |
inline fun inDebugMode(func: () -> Unit) { | |
if(BuildConfig.BUILD_TYPE.equals("debug")) { | |
func() | |
} | |
} | |
fun logException(e: Throwable) = ifLoggable { Crashlytics.logException(e) } | |
fun getDeviceId(context: Context): String = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID); | |
inline fun ObjectAnimator.onStart(crossinline func: () -> Unit) { | |
addListener(object : Animator.AnimatorListener { | |
override fun onAnimationRepeat(animation: Animator?) {} | |
override fun onAnimationEnd(animation: Animator?) {} | |
override fun onAnimationCancel(animation: Animator?) {} | |
override fun onAnimationStart(animation: Animator?) { func() } | |
}) | |
} | |
inline fun ObjectAnimator.onEnd(crossinline func: () -> Unit) { | |
addListener(object : Animator.AnimatorListener { | |
override fun onAnimationRepeat(animation: Animator?) {} | |
override fun onAnimationEnd(animation: Animator?) { func() } | |
override fun onAnimationCancel(animation: Animator?) {} | |
override fun onAnimationStart(animation: Animator?) {} | |
}) | |
} | |
fun Intent.hasExtras(extras: List<String>) : Boolean = extras.all { hasExtra(it) } |
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 pl.kpob.utils.extensions | |
import android.animation.ObjectAnimator | |
import android.content.Context | |
import android.content.res.Resources | |
import android.support.v7.widget.RecyclerView | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.ViewTreeObserver | |
import android.view.animation.AlphaAnimation | |
import android.view.animation.Animation | |
import android.view.inputmethod.InputMethodManager | |
import android.widget.ImageView | |
import android.widget.LinearLayout | |
import com.squareup.picasso.Picasso | |
import org.jetbrains.anko.* | |
/** | |
* Created by kpob on 3/18/16. | |
*/ | |
fun View.isVisible() = visibility == View.VISIBLE | |
fun View.show() { visibility = View.VISIBLE } | |
fun View.hide() { visibility = View.GONE } | |
fun View.getString(res: Int) = resources.getString(res) | |
inline fun View.onViewClick(id: Int, crossinline func: () -> Unit) = find<View>(id).onClick { func() } | |
fun View.createDivider() : View = View(context).apply { | |
layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dip(1)) | |
backgroundColor = resources.getColor(R.color.divider) | |
} | |
fun View.fadeIn(duration: Long, f:() -> Unit) { | |
ObjectAnimator.ofFloat(this, "alpha", 0f, 1f).apply { | |
setDuration(duration) | |
onStart { f() } | |
start() | |
} | |
} | |
fun View.fadeOut(duration: Long, f:() -> Unit) { | |
ObjectAnimator.ofFloat(this, "alpha", 1f, 0f).apply { | |
setDuration(duration) | |
onEnd { f() } | |
start() | |
} | |
} | |
fun View.hideKeyboard() = context.hideKeyboard(arrayOf(this)) | |
fun Context.hideKeyboard(views: Array<View>) : Boolean { | |
for (view in views) { | |
if(inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)) { | |
return true | |
} | |
} | |
return false | |
} | |
fun View.showKeyboard() = context.showKeyboard(this) | |
fun Context.showKeyboard(view: View) : Unit { | |
inputMethodManager.showSoftInputFromInputMethod(view.windowToken, 0) | |
} | |
fun onViews(views: List<View>, func: View.() -> Unit) { | |
views.map { it.func() } | |
} | |
fun hideViews(views: List<View>) { | |
onViews(views) { hide() } | |
} | |
fun showViews(views: List<View>) { | |
onViews(views) { show() } | |
} | |
fun conditionalShowViews(views: List<View>, predicate: () -> Boolean) { | |
if(predicate()) showViews(views) else hideViews(views) | |
} | |
fun View.slideExit() { | |
if (translationY == 0f) animate().translationY(-height.toFloat()) | |
} | |
fun View.slideEnter() { | |
if (translationY < 0f) animate().translationY(0f) | |
} | |
operator fun ViewGroup.get(position: Int) : View { | |
return getChildAt(position) | |
} | |
fun ImageView.loadUrl(url: String, placeholderId: Int = 0) { | |
if(url.isNotBlank()) { | |
var request = Picasso.with(this.context).load(url) | |
if(placeholderId != 0) { | |
request.placeholder(placeholderId).into(this) | |
} else { | |
request.into(this) | |
} | |
} | |
} | |
fun inflateLayout(parent: ViewGroup?, layout: Int) : View = | |
LayoutInflater.from(parent!!.context).inflate(layout, parent, false) | |
val RecyclerView.ViewHolder.ctx : Context | |
get() = itemView.context | |
val RecyclerView.ViewHolder.res : Resources | |
get() = itemView.context.resources | |
fun AlertDialogBuilder.singleChoice(items: Array<String>, checkedItem: Int, f: (Int) -> Unit) { | |
builder.setSingleChoiceItems(items, checkedItem, { dialogInterface, i -> f(i); dismiss() }) | |
} | |
fun View.waitForMeasure(func: (v: View, w: Int, h: Int) -> Unit) { | |
if (width > 0 && height > 0) { | |
func(this, width, height) | |
return | |
} | |
val listener = object : ViewTreeObserver.OnPreDrawListener { | |
override fun onPreDraw(): Boolean { | |
val observer = viewTreeObserver | |
if (observer.isAlive) { | |
observer.removeOnPreDrawListener(this) | |
} | |
func(this@waitForMeasure, width, height) | |
return true | |
} | |
} | |
viewTreeObserver.addOnPreDrawListener(listener) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment