Last active
September 26, 2017 06:37
-
-
Save untungs/fa47ad554cab67a4bcff88461d5d5927 to your computer and use it in GitHub Desktop.
Kotlin Extensions
This file contains 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
fun Activity.drawBehindStatusBar() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or | |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | |
} | |
} |
This file contains 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
fun Context.color(@ColorRes colorRes: Int): Int | |
= ContextCompat.getColor(this, colorRes) | |
fun Context.drawable(@DrawableRes drawableRes: Int): Drawable? | |
= AppCompatResources.getDrawable(this, drawableRes) | |
fun Context.customAnimation(@AnimRes enterResId: Int, @AnimRes exitResId: Int): Bundle | |
= ActivityOptionsCompat.makeCustomAnimation(this, enterResId, exitResId).toBundle(); | |
fun RecyclerView.ViewHolder.string(@StringRes stringRes: Int, vararg formatArgs: Any): String | |
= itemView.context.getString(stringRes, *formatArgs) | |
fun RecyclerView.ViewHolder.color(@ColorRes colorRes: Int): Int | |
= ContextCompat.getColor(itemView.context, colorRes) | |
fun RecyclerView.ViewHolder.drawable(@DrawableRes drawableRes: Int): Drawable? | |
= AppCompatResources.getDrawable(itemView.context, drawableRes) |
This file contains 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
// from Kotlin for Android Developers (the book) | |
object DelegatesExt { | |
fun <T> preference(context: Context, name: String, default: T) = Preferences(context, name, default) | |
} | |
class Preferences<T>(context: Context, val name: String, val default: T) { | |
val prefs: SharedPreferences by lazy { context.getSharedPreferences("default", Context.MODE_PRIVATE) } | |
@Suppress("UNCHECKED_CAST") | |
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = with(prefs) { | |
val res: Any = when (default) { | |
is String -> getString(name, default) | |
is Boolean -> getBoolean(name, default) | |
is Int -> getInt(name, default) | |
is Float -> getFloat(name, default) | |
is Long -> getLong(name, default) | |
else -> throw IllegalArgumentException("This type can't be saved into Preferences") | |
} | |
res as T | |
} | |
@SuppressLint("CommitPrefEdits") | |
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) = with(prefs.edit()) { | |
when (value) { | |
is String -> putString(name, value) | |
is Boolean -> putBoolean(name, value) | |
is Int -> putInt(name, value) | |
is Float -> putFloat(name, value) | |
is Long -> putLong(name, value) | |
else -> throw IllegalArgumentException("This type can't be saved into Preferences") | |
}.apply() | |
} | |
} |
This file contains 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
fun String.isNumeric(): Boolean = try { | |
this.toLong() | |
true | |
} catch(e: NumberFormatException) { | |
false | |
} | |
fun String.asHtml(): Spanned { | |
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
Html.fromHtml(this, Html.FROM_HTML_MODE_COMPACT) | |
} else { | |
Html.fromHtml(this) | |
} | |
} | |
fun Long.toRupiah(): String { | |
val formatter = NumberFormat.getCurrencyInstance(Locale("id")) as DecimalFormat | |
with(formatter) { | |
decimalFormatSymbols = decimalFormatSymbols.apply { currencySymbol = "Rp " } | |
maximumFractionDigits = 0 | |
} | |
return formatter.format(this) | |
} | |
// use kotlin's coerceIn function instead | |
inline fun Int.clampWithin(range: () -> Pair<Int, Int>): Int { | |
val (min, max) = range() | |
return Math.min(Math.max(this, min), max) | |
} |
This file contains 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
inline fun CompositeDisposable.switch(disposable: Disposable?, f: () -> Disposable): Disposable { | |
disposable?.let { this.remove(it) } | |
return f().apply { this@switch += this } | |
} |
This file contains 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
inline fun View.visibleIf(block: () -> Boolean) { | |
this.visibility = if (block()) View.VISIBLE else View.GONE | |
} | |
fun View.closeKeyboard() { | |
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager | |
imm.hideSoftInputFromWindow(windowToken, 0) | |
} | |
val View.transition: Pair<View, String> | |
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) | |
get()= Pair(this, transitionName) | |
fun ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): View { | |
return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment